@marver-design/marver 0.29.0 → 0.30.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/CHANGELOG.md +60 -0
- package/README.md +2 -1
- package/dist/{anchor-check-DhlpKl2K.mjs → anchor-check-Bv4o-R2T.mjs} +1 -1
- package/dist/{build-CWbrb2bl.mjs → build-zWGXSzly.mjs} +5 -4
- package/dist/cli.mjs +5 -5
- package/dist/{daemon-Sc_z8gT6.mjs → daemon-CChgWJNq.mjs} +2 -2
- package/dist/{dev-DWBKQhxf.mjs → dev-DOgIITpT.mjs} +8 -8
- package/dist/{perf-Denssrko.mjs → perf-6MOTHxay.mjs} +710 -99
- package/dist/perf-BCxAo0IS.mjs +1743 -0
- package/dist/{plugin-DqoZB-D6.mjs → plugin-CFWBcAba.mjs} +928 -539
- package/dist/{poster-CrsjqhY4.mjs → poster-DPPpQiva.mjs} +1 -1
- package/dist/{publish-bakes-DW-NDgU2.mjs → publish-bakes-_Zx8crP3.mjs} +2 -2
- package/dist/{recover-GtJ9UDAB.mjs → recover-mDUVsnPd.mjs} +1 -1
- package/dist/{shot-T04Ixrvq.mjs → shot-d4h_hJi9.mjs} +3 -3
- package/docs/app.md +194 -0
- package/docs/performance.md +100 -19
- package/package.json +1 -1
- package/src/client/frame-host/bridge.js +17 -8
- package/src/client/frame-host/inplace.ts +124 -0
- package/src/client/frame-host/inspect.d.ts +1 -0
- package/src/client/frame-host/inspect.js +13 -4
- package/src/client/frame-host/main.tsx +12 -3
- package/src/client/frame-host/messages.ts +85 -0
- package/src/client/frame-host/signals.d.ts +15 -0
- package/src/client/frame-host/signals.js +219 -0
- package/src/client/recorder-level.ts +41 -0
- package/src/client/shell/App.tsx +158 -88
- package/src/client/shell/BoardList.tsx +103 -48
- package/src/client/shell/Comments.tsx +66 -42
- package/src/client/shell/ContextMenu.tsx +3 -2
- package/src/client/shell/JamSurface.tsx +116 -71
- package/src/client/shell/LockedApp.tsx +19 -3
- package/src/client/shell/Play.tsx +94 -58
- package/src/client/shell/StatusPicker.tsx +19 -9
- package/src/client/shell/Toolbar.tsx +15 -7
- package/src/client/shell/api.ts +18 -4
- package/src/client/shell/app-bridge.ts +12 -6
- package/src/client/shell/app-perf.ts +31 -1
- package/src/client/shell/canvas/Canvas.tsx +17 -6
- package/src/client/shell/canvas/FrameNode.tsx +25 -12
- package/src/client/shell/canvas/Sticky.tsx +4 -2
- package/src/client/shell/canvas/ctl.ts +2 -0
- package/src/client/shell/canvas/ready-watch.ts +4 -0
- package/src/client/shell/canvas/sleep.ts +2 -0
- package/src/client/shell/canvas/updates.ts +13 -6
- package/src/client/shell/commands.ts +765 -0
- package/src/client/shell/comments-store.ts +42 -27
- package/src/client/shell/goto.ts +11 -6
- package/src/client/shell/jam-keys.ts +141 -75
- package/src/client/shell/jam-lane.tsx +19 -12
- package/src/client/shell/jam-panel.tsx +61 -48
- package/src/client/shell/jam-session.tsx +41 -37
- package/src/client/shell/jam-state.ts +9 -1
- package/src/client/shell/jam-surface-parts.tsx +8 -6
- package/src/client/shell/jam-ui.tsx +9 -6
- package/src/client/shell/jam-voice.ts +4 -2
- package/src/client/shell/main.tsx +7 -1
- package/src/client/shell/marks-ui.tsx +11 -0
- package/src/client/shell/perf.ts +51 -5
- package/src/client/shell/recorder-core.ts +4 -1
- package/src/client/shell/recorder-hmr.ts +36 -0
- package/src/client/shell/recorder-reloads.ts +264 -0
- package/src/client/shell/recorder-seams.ts +116 -0
- package/src/client/shell/recorder-self.ts +73 -0
- package/src/client/shell/recorder-signals-core.ts +139 -0
- package/src/client/shell/recorder-signals.ts +485 -0
- package/src/client/shell/recorder.ts +118 -43
- package/src/client/shell/store.ts +112 -32
- package/src/client/shell/whiteout-probes.ts +3 -1
- package/src/client/stage/main.tsx +10 -5
- package/src/shared/app-api.ts +28 -6
- package/src/shared/app-perf.ts +99 -6
- package/src/shared/app-report.ts +586 -0
- package/src/shared/perf-records.ts +445 -0
- package/src/shared/perf-run.ts +6 -2
- package/dist/perf-jam-CLkERSHo.mjs +0 -642
|
@@ -0,0 +1,1743 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-8H4AJuhK.mjs";
|
|
2
|
+
import { o as checkRealDirs } from "./boards-EH2onUXM.mjs";
|
|
3
|
+
import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, statSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { isAbsolute, join, relative } from "node:path";
|
|
5
|
+
import { appendFile } from "node:fs/promises";
|
|
6
|
+
//#region src/shared/perf-records.ts
|
|
7
|
+
/**
|
|
8
|
+
* Spec 30's records, in one place (R13): every kind the complete recorder adds or changes, on the canvas side
|
|
9
|
+
* (design/.local/perf/page.jsonl and reloads.jsonl, written by the dev server) and the `cmd` record the canvas and the
|
|
10
|
+
* Mac app share. The app's other kinds (`launch`, `window`, the white-out fields) are in app-perf.ts, beside the kinds
|
|
11
|
+
* they extend. Types, constants and a few pure helpers only - no imports - so the shell, the frame host, the server,
|
|
12
|
+
* the CLI and the app's main bundle all take it as it is.
|
|
13
|
+
*
|
|
14
|
+
* FROZEN before the spec 30 streams split. `v` stays 1 (#3): every change is a new kind or an optional field, and a
|
|
15
|
+
* field's meaning never changes. Owners (the stream that writes it; readers are the app-store stream's, R10, R11, R14):
|
|
16
|
+
* - edits (R1, R2): `change`, `update`/`update-ids`, `full-reload`, `invalidate`/`invalidate-ids`, the shell's reload
|
|
17
|
+
* lines (`shell-boot`, `ready`, `settled`, `invalidated`/`invalidated-nodes`, `inplace`, `watchdog`), `load`'s
|
|
18
|
+
* trigger, writer and candidates, `cut`'s partial times, the reload batch route and the `(page, seq)` dedupe;
|
|
19
|
+
* - signals (R3-R6): `theme`, `mode`, `error`'s new sources and `modes`, `warnings`, `hmr`, `gesture`'s new fields,
|
|
20
|
+
* `renders`;
|
|
21
|
+
* - commands (R12): `cmd` (page and app), `by` wherever an effect names its command;
|
|
22
|
+
* - app-store (R7-R11, R14, R15): `self`, `dropped`'s `retention-cap`, the segments, the recorder level's run.
|
|
23
|
+
*
|
|
24
|
+
* Privacy (#4): the recorder's own hooks record names, key combinations, ids, counts and times only - never an input's
|
|
25
|
+
* value, the clipboard, a body or a query string. Error and console messages are kept as the code produced them.
|
|
26
|
+
*/
|
|
27
|
+
/** `core` turns off everything spec 30 adds and keeps 0.29's load tracking; `full` (the default) records it all. */
|
|
28
|
+
const RECORDER_LEVELS = ["core", "full"];
|
|
29
|
+
/** The query parameter that sets it (`?mvrecorder=core`): on the shell's URL - `marver perf run --recorder core` puts
|
|
30
|
+
* it there - and carried by `withWs()` onto frame and stage URLs, so a frame's hooks keep the page's level. */
|
|
31
|
+
const RECORDER_PARAM = "mvrecorder";
|
|
32
|
+
/** A page, a load, a command or a transition (`t`): 12 hex (recorder-core.ts `newId()`). */
|
|
33
|
+
const ID12 = /^[0-9a-f]{12}$/;
|
|
34
|
+
/** A refused refresh has no identity of its own (#8): its `update` line says so. */
|
|
35
|
+
const UNKNOWN_CHANGE = "unknown";
|
|
36
|
+
/** R1: ids per reload line before the rest go to `-ids` parts (#10, #12, #13); candidates kept (#8, #11); the
|
|
37
|
+
* `hmrTs → change` history (#8); a refused refresh's look-back (#8); an inplace's wait for the next commit (#15), and
|
|
38
|
+
* how long a round's watch stays up with no `vite:afterUpdate` (an accept callback threw, #15). */
|
|
39
|
+
const RELOAD_BOUNDS = {
|
|
40
|
+
idsPerLine: 256,
|
|
41
|
+
candidates: 8,
|
|
42
|
+
hmrHistory: 512,
|
|
43
|
+
lookBackMs: 1e4,
|
|
44
|
+
commitWaitMs: 2e3,
|
|
45
|
+
inplaceRoundMs: 3e4
|
|
46
|
+
};
|
|
47
|
+
/** #7: the server's best-effort `(page, seq)` window - every reader dedupes again. */
|
|
48
|
+
const DEDUPE = {
|
|
49
|
+
windowMs: 6e5,
|
|
50
|
+
perPage: 4096,
|
|
51
|
+
pages: 64
|
|
52
|
+
};
|
|
53
|
+
/** #18: the hashes a board write keeps, for the watcher's `sh:board` to match. */
|
|
54
|
+
const WRITE_MATCH_MS = 1e4;
|
|
55
|
+
/** #28: console warnings are counted per page per minute, the `top` nodes by count. */
|
|
56
|
+
const WARNINGS = {
|
|
57
|
+
periodMs: 6e4,
|
|
58
|
+
top: 8
|
|
59
|
+
};
|
|
60
|
+
/** #35: the Long Animation Frames kept per gesture, and the script's `fn` length. */
|
|
61
|
+
const LOAF = {
|
|
62
|
+
kept: 3,
|
|
63
|
+
fn: 64
|
|
64
|
+
};
|
|
65
|
+
/** #36: one `renders` record per minute, its heaviest rows (the rest folded into `others`). */
|
|
66
|
+
const RENDERS = {
|
|
67
|
+
periodMs: 6e4,
|
|
68
|
+
rows: 64
|
|
69
|
+
};
|
|
70
|
+
/** Each file's segment size (a live file is also closed 24 h after it was opened) and its safety cap (#43). */
|
|
71
|
+
const SEGMENTS = {
|
|
72
|
+
"page.jsonl": {
|
|
73
|
+
segmentBytes: 8 * 2 ** 20,
|
|
74
|
+
capBytes: 128 * 2 ** 20
|
|
75
|
+
},
|
|
76
|
+
"reloads.jsonl": {
|
|
77
|
+
segmentBytes: 8 * 2 ** 20,
|
|
78
|
+
capBytes: 48 * 2 ** 20
|
|
79
|
+
},
|
|
80
|
+
"jam.jsonl": {
|
|
81
|
+
segmentBytes: 2 * 2 ** 20,
|
|
82
|
+
capBytes: 8 * 2 ** 20
|
|
83
|
+
},
|
|
84
|
+
"runs.jsonl": {
|
|
85
|
+
segmentBytes: 2 * 2 ** 20,
|
|
86
|
+
capBytes: 8 * 2 ** 20
|
|
87
|
+
},
|
|
88
|
+
"app.jsonl": {
|
|
89
|
+
segmentBytes: 4 * 2 ** 20,
|
|
90
|
+
capBytes: 16 * 2 ** 20
|
|
91
|
+
},
|
|
92
|
+
"app-dev.jsonl": {
|
|
93
|
+
segmentBytes: 4 * 2 ** 20,
|
|
94
|
+
capBytes: 16 * 2 ** 20
|
|
95
|
+
},
|
|
96
|
+
"samples.jsonl": {
|
|
97
|
+
segmentBytes: 8 * 2 ** 20,
|
|
98
|
+
capBytes: 128 * 2 ** 20
|
|
99
|
+
},
|
|
100
|
+
"samples-dev.jsonl": {
|
|
101
|
+
segmentBytes: 8 * 2 ** 20,
|
|
102
|
+
capBytes: 128 * 2 ** 20
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
/** A closed segment's name (#42): `<name>.<openedAt>-<bound>.jsonl`, both epoch ms; `bound` is a conservative newest
|
|
106
|
+
* time (the later of its close and the largest `ts` written into it; an adopted file's mtime + 24 h). */
|
|
107
|
+
const segmentName = (file, openedAt, bound) => `${file.slice(0, -6)}.${Math.round(openedAt)}-${Math.round(bound)}.jsonl`;
|
|
108
|
+
/** A segment of `file`, read back from its name; null for any other name (the live file included). */
|
|
109
|
+
function parseSegmentName(file, name) {
|
|
110
|
+
const base = file.slice(0, -6).replace(/[.-]/g, (c) => `\\${c}`);
|
|
111
|
+
const m = new RegExp(`^${base}\\.(\\d{10,16})-(\\d{10,16})\\.jsonl$`).exec(name);
|
|
112
|
+
return m ? {
|
|
113
|
+
openedAt: Number(m[1]),
|
|
114
|
+
bound: Number(m[2])
|
|
115
|
+
} : null;
|
|
116
|
+
}
|
|
117
|
+
/** Every kind a page sends (recorder-core.ts `KINDS` and the server's `PAGE_KINDS` read this list). */
|
|
118
|
+
const PAGE_KINDS$1 = [
|
|
119
|
+
"load",
|
|
120
|
+
"commit",
|
|
121
|
+
"visible",
|
|
122
|
+
"frame",
|
|
123
|
+
"move",
|
|
124
|
+
"quiet",
|
|
125
|
+
"settled",
|
|
126
|
+
"cut",
|
|
127
|
+
"gesture",
|
|
128
|
+
"error",
|
|
129
|
+
"dropped",
|
|
130
|
+
"renders",
|
|
131
|
+
"warnings",
|
|
132
|
+
"theme",
|
|
133
|
+
"mode",
|
|
134
|
+
"hmr",
|
|
135
|
+
"cmd",
|
|
136
|
+
"self"
|
|
137
|
+
];
|
|
138
|
+
/** #17: why `boot()` read a board again; a switch keeps `cause: 'switch'` and has none. */
|
|
139
|
+
const LOAD_TRIGGERS = [
|
|
140
|
+
"first",
|
|
141
|
+
"disk",
|
|
142
|
+
"save-conflict",
|
|
143
|
+
"app-shown"
|
|
144
|
+
];
|
|
145
|
+
/** #18: who wrote the board file a disk reload read - never claimed as proven. */
|
|
146
|
+
const WRITER_ATTRIBUTION = [
|
|
147
|
+
"candidate",
|
|
148
|
+
"ambiguous",
|
|
149
|
+
"unknown"
|
|
150
|
+
];
|
|
151
|
+
/** Where an `error` came from (#28-#32, kept: shell, frame, stage). */
|
|
152
|
+
const ERROR_SOURCES = [
|
|
153
|
+
"shell",
|
|
154
|
+
"frame",
|
|
155
|
+
"stage",
|
|
156
|
+
"console",
|
|
157
|
+
"vite",
|
|
158
|
+
"api",
|
|
159
|
+
"resource"
|
|
160
|
+
];
|
|
161
|
+
/** #32: the elements whose load error is recorded. */
|
|
162
|
+
const RESOURCE_TAGS = [
|
|
163
|
+
"img",
|
|
164
|
+
"script",
|
|
165
|
+
"link",
|
|
166
|
+
"video",
|
|
167
|
+
"audio",
|
|
168
|
+
"source"
|
|
169
|
+
];
|
|
170
|
+
/** #21-#23: a theme switch, by transition. */
|
|
171
|
+
const THEME_SCOPES = [
|
|
172
|
+
"view",
|
|
173
|
+
"selection",
|
|
174
|
+
"play"
|
|
175
|
+
];
|
|
176
|
+
const TRANSITION_OUTCOMES = [
|
|
177
|
+
"done",
|
|
178
|
+
"timeout",
|
|
179
|
+
"superseded"
|
|
180
|
+
];
|
|
181
|
+
/** #24: a state that changes what input does until left. */
|
|
182
|
+
const MODES = [
|
|
183
|
+
"laser",
|
|
184
|
+
"inspect",
|
|
185
|
+
"comments",
|
|
186
|
+
"interact",
|
|
187
|
+
"play",
|
|
188
|
+
"slides",
|
|
189
|
+
"hide-ui",
|
|
190
|
+
"jam-session",
|
|
191
|
+
"jam-capture",
|
|
192
|
+
"jam-review",
|
|
193
|
+
"jam-lane"
|
|
194
|
+
];
|
|
195
|
+
/** #58: the recorder's own cost over the last 5 minutes. */
|
|
196
|
+
const SELF_HOOKS = [
|
|
197
|
+
"console",
|
|
198
|
+
"theme",
|
|
199
|
+
"mode",
|
|
200
|
+
"cmd",
|
|
201
|
+
"renders",
|
|
202
|
+
"send"
|
|
203
|
+
];
|
|
204
|
+
/** Why records were dropped (#5, #33, #43, #48): `errors` past a load's 50 (as today), a source's own rate or a
|
|
205
|
+
* frame's budget (`sh:dropped`), command starts past their rate, transitions past theirs, the safety cap. */
|
|
206
|
+
const DROP_REASONS$1 = [
|
|
207
|
+
"errors",
|
|
208
|
+
"shell",
|
|
209
|
+
"frame",
|
|
210
|
+
"stage",
|
|
211
|
+
"console",
|
|
212
|
+
"vite",
|
|
213
|
+
"api",
|
|
214
|
+
"resource",
|
|
215
|
+
"cmd",
|
|
216
|
+
"theme",
|
|
217
|
+
"mode",
|
|
218
|
+
"retention-cap"
|
|
219
|
+
];
|
|
220
|
+
/** #50: how the input arrived. */
|
|
221
|
+
const CMD_VIA = [
|
|
222
|
+
"key",
|
|
223
|
+
"click",
|
|
224
|
+
"menu",
|
|
225
|
+
"context",
|
|
226
|
+
"drag",
|
|
227
|
+
"dblclick",
|
|
228
|
+
"relay"
|
|
229
|
+
];
|
|
230
|
+
/** #50: the surface it arrived on; `app` is a relay from an app that does not say which menu (#51). */
|
|
231
|
+
const CMD_IN = [
|
|
232
|
+
"shell",
|
|
233
|
+
"frame",
|
|
234
|
+
"stage",
|
|
235
|
+
"strip",
|
|
236
|
+
"home",
|
|
237
|
+
"settings",
|
|
238
|
+
"tabpage",
|
|
239
|
+
"pill",
|
|
240
|
+
"menu",
|
|
241
|
+
"tab-menu",
|
|
242
|
+
"main",
|
|
243
|
+
"app"
|
|
244
|
+
];
|
|
245
|
+
/** #49: what a command came to. `unobserved` says only that no signal came by its deadline. */
|
|
246
|
+
const CMD_OUTCOMES = [
|
|
247
|
+
"done",
|
|
248
|
+
"failed",
|
|
249
|
+
"noop",
|
|
250
|
+
"cancelled",
|
|
251
|
+
"unobserved"
|
|
252
|
+
];
|
|
253
|
+
/** #48: a start and an end, or one `once` record for a command decided in its input's own turn. */
|
|
254
|
+
const CMD_PHASES = [
|
|
255
|
+
"start",
|
|
256
|
+
"end",
|
|
257
|
+
"once"
|
|
258
|
+
];
|
|
259
|
+
/** #18: the headers every shell request that can write a board file sends - its page, and the commands folded into
|
|
260
|
+
* it (`encodeCmds`). The server keeps them with the hash it wrote, for `WRITE_MATCH_MS`. */
|
|
261
|
+
const WRITER_HEADERS = {
|
|
262
|
+
page: "x-mv-page",
|
|
263
|
+
cmds: "x-mv-cmds"
|
|
264
|
+
};
|
|
265
|
+
/** `x-mv-cmds` read back; null for anything else (an empty header is no commands). */
|
|
266
|
+
function decodeCmds(h) {
|
|
267
|
+
if (typeof h !== "string" || h.length > 200) return null;
|
|
268
|
+
const m = /^((?:[0-9a-f]{12})(?:,[0-9a-f]{12}){0,7})?(?:;n=(\d{1,6}))?$/.exec(h);
|
|
269
|
+
if (!m) return null;
|
|
270
|
+
const ids = m[1] ? m[1].split(",") : [];
|
|
271
|
+
const n = m[2] === void 0 ? ids.length : Number(m[2]);
|
|
272
|
+
return n < ids.length ? null : {
|
|
273
|
+
ids,
|
|
274
|
+
n
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/** Lines the shell sends through its reload Outbox (#16): each names its page and seq. */
|
|
278
|
+
const SHELL_RELOAD_KINDS = [
|
|
279
|
+
"shell-boot",
|
|
280
|
+
"ready",
|
|
281
|
+
"settled",
|
|
282
|
+
"invalidated",
|
|
283
|
+
"invalidated-nodes",
|
|
284
|
+
"inplace",
|
|
285
|
+
"watchdog"
|
|
286
|
+
];
|
|
287
|
+
/** Why `ready()` held a node back (canvas/updates.ts:29-33), and `asleep` (sleep.ts). */
|
|
288
|
+
const HELD_BY = [
|
|
289
|
+
"offscreen",
|
|
290
|
+
"leased",
|
|
291
|
+
"play",
|
|
292
|
+
"loading",
|
|
293
|
+
"missing"
|
|
294
|
+
];
|
|
295
|
+
//#endregion
|
|
296
|
+
//#region src/shared/app-perf.ts
|
|
297
|
+
/** The file each flavour writes, under `<MARVER_HOME>/perf/`. */
|
|
298
|
+
const APP_LOG = {
|
|
299
|
+
release: "app.jsonl",
|
|
300
|
+
dev: "app-dev.jsonl"
|
|
301
|
+
};
|
|
302
|
+
/** Spec 30 R9 #44: where each flavour's `sample` and `agx` lines go from app 1.0.1, rows unchanged. One `seq` per
|
|
303
|
+
* launch runs across both files; readers merge by `app` and `seq`. */
|
|
304
|
+
const APP_SAMPLES_LOG = {
|
|
305
|
+
release: "samples.jsonl",
|
|
306
|
+
dev: "samples-dev.jsonl"
|
|
307
|
+
};
|
|
308
|
+
const KINDS$1 = /* @__PURE__ */ new Set([
|
|
309
|
+
"start",
|
|
310
|
+
"sample",
|
|
311
|
+
"agx",
|
|
312
|
+
"gone",
|
|
313
|
+
"child-gone",
|
|
314
|
+
"gpu-restart",
|
|
315
|
+
"unresponsive",
|
|
316
|
+
"responsive",
|
|
317
|
+
"gpu",
|
|
318
|
+
"power",
|
|
319
|
+
"tab",
|
|
320
|
+
"shown",
|
|
321
|
+
"pressure",
|
|
322
|
+
"unload",
|
|
323
|
+
"unload-skip",
|
|
324
|
+
"unload-effect",
|
|
325
|
+
"reopen",
|
|
326
|
+
"cover",
|
|
327
|
+
"flash",
|
|
328
|
+
"whiteout-candidate",
|
|
329
|
+
"dropped",
|
|
330
|
+
"main-error",
|
|
331
|
+
"update",
|
|
332
|
+
"launch",
|
|
333
|
+
"cmd",
|
|
334
|
+
"window"
|
|
335
|
+
]);
|
|
336
|
+
const isInt = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0;
|
|
337
|
+
/**
|
|
338
|
+
* One line of app.jsonl, or null (A3.5): the envelope checked - `v: 1`, a finite `ts`, a known `kind`, an `app` id,
|
|
339
|
+
* a whole `seq`, a `flavour`, a `machine` hash or null. The body is the writer's and is passed on as it is: a
|
|
340
|
+
* reader takes the fields it knows. A line without the envelope (app 0.1's main-error lines) is null.
|
|
341
|
+
*/
|
|
342
|
+
function parseAppLine(line) {
|
|
343
|
+
let r;
|
|
344
|
+
try {
|
|
345
|
+
r = JSON.parse(line);
|
|
346
|
+
} catch {
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
if (!r || typeof r !== "object" || Array.isArray(r)) return null;
|
|
350
|
+
const o = r;
|
|
351
|
+
if (o.v !== 1 || typeof o.ts !== "number" || !Number.isFinite(o.ts)) return null;
|
|
352
|
+
if (typeof o.kind !== "string" || !KINDS$1.has(o.kind)) return null;
|
|
353
|
+
if (typeof o.app !== "string" || !/^[0-9a-f]{12}$/.test(o.app) || !isInt(o.seq)) return null;
|
|
354
|
+
if (o.flavour !== "release" && o.flavour !== "dev") return null;
|
|
355
|
+
if (o.machine !== null && (typeof o.machine !== "string" || !/^[0-9a-f]{16}$/.test(o.machine))) return null;
|
|
356
|
+
return o;
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/server/perf-segments.ts
|
|
360
|
+
/**
|
|
361
|
+
* Segments (spec 30 R9 #42-#45): every perf log is a live file plus closed segments, deleted whole by age. The
|
|
362
|
+
* primitives the writers share - the dev server's (perf.ts: page, reloads, runs), Jam's (perf-jam.ts) and the Mac
|
|
363
|
+
* app's (app/src/main/perflog.ts: app and samples) - and the one reader every reader goes through (the CLI's views,
|
|
364
|
+
* `--all`, the app's report). Node only; each writer keeps its own state and its own queue, and calls these in it.
|
|
365
|
+
*
|
|
366
|
+
* - A segment is `<name>.<openedAt>-<bound>.jsonl` (perf-records.ts `segmentName`): `bound` a conservative newest time,
|
|
367
|
+
* the later of its close and the largest `ts` written into it, since line times are event times and arrive out of
|
|
368
|
+
* order - neither a segment's first line nor the next segment's says how new its lines are.
|
|
369
|
+
* - A live file the writer did not open itself (adopted after a restart, or a 0.29 file at the first 0.30 start, #45)
|
|
370
|
+
* has no tracked maximum: its `openedAt` is its first line's `ts` (one line read) and its bound its mtime + 24 h at
|
|
371
|
+
* the close (the server's cleaner keeps a page's clock within 24 h of its own, perf.ts `cleanPageRecord`).
|
|
372
|
+
* - Closing is a rename (O(1)); retention an unlink of a segment whose bound is past the days kept, never a read or a
|
|
373
|
+
* rewrite; the safety cap (#43) deletes the oldest segments while the file's total passes it, and says how many
|
|
374
|
+
* lines went (that rare case reads the segment it deletes to count them: a record never lies).
|
|
375
|
+
*/
|
|
376
|
+
const DAY = 864e5;
|
|
377
|
+
/** #42: an adopted file's bound - its mtime plus the clock skew a line's `ts` may carry. */
|
|
378
|
+
const ADOPTED_SKEW_MS = DAY;
|
|
379
|
+
/** A closed segment of `file` in `dir`, oldest first (by `openedAt`, then name). A link or anything not a regular file is no segment. */
|
|
380
|
+
function segmentsOf(dir, file) {
|
|
381
|
+
let names;
|
|
382
|
+
try {
|
|
383
|
+
names = readdirSync(dir);
|
|
384
|
+
} catch {
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
const out = [];
|
|
388
|
+
for (const name of names) {
|
|
389
|
+
const p = parseSegmentName(file, name);
|
|
390
|
+
if (!p) continue;
|
|
391
|
+
const path = join(dir, name);
|
|
392
|
+
try {
|
|
393
|
+
const st = lstatSync(path);
|
|
394
|
+
if (st.isFile()) out.push({
|
|
395
|
+
name,
|
|
396
|
+
path,
|
|
397
|
+
...p,
|
|
398
|
+
size: st.size
|
|
399
|
+
});
|
|
400
|
+
} catch {}
|
|
401
|
+
}
|
|
402
|
+
return out.sort((a, b) => a.openedAt - b.openedAt || a.name.localeCompare(b.name));
|
|
403
|
+
}
|
|
404
|
+
/** A line's `ts`: the first `"ts":` (every writer puts the envelope's first), else the parsed line's; null when it has none. */
|
|
405
|
+
function lineTs(line) {
|
|
406
|
+
const m = /"ts":(\d{10,16})(?:[.,}\]])/.exec(line);
|
|
407
|
+
if (m) return Number(m[1]);
|
|
408
|
+
try {
|
|
409
|
+
const o = JSON.parse(line);
|
|
410
|
+
return typeof o?.ts === "number" && Number.isFinite(o.ts) ? o.ts : null;
|
|
411
|
+
} catch {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
/** The first whole line's `ts`, reading at most 64 KB (#45: one line read). */
|
|
416
|
+
function firstLineTs(path) {
|
|
417
|
+
let fd;
|
|
418
|
+
try {
|
|
419
|
+
fd = openSync(path, "r");
|
|
420
|
+
const b = Buffer.alloc(65536);
|
|
421
|
+
const n = readSync(fd, b, 0, b.length, 0);
|
|
422
|
+
const text = b.subarray(0, n).toString("utf8");
|
|
423
|
+
const nl = text.indexOf("\n");
|
|
424
|
+
return nl > 0 ? lineTs(text.slice(0, nl)) : null;
|
|
425
|
+
} catch {
|
|
426
|
+
return null;
|
|
427
|
+
} finally {
|
|
428
|
+
if (fd !== void 0) try {
|
|
429
|
+
closeSync(fd);
|
|
430
|
+
} catch {}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/** The live file as this writer finds it the first time: adopted when it exists (openedAt its first line's ts, else
|
|
434
|
+
* its mtime), null when there is none. Throws for a path that is not a regular file. */
|
|
435
|
+
function adoptLive(path) {
|
|
436
|
+
let st;
|
|
437
|
+
try {
|
|
438
|
+
st = lstatSync(path);
|
|
439
|
+
} catch {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
if (!st.isFile()) throw new Error("not a regular file");
|
|
443
|
+
const first = firstLineTs(path);
|
|
444
|
+
return {
|
|
445
|
+
openedAt: first !== null && first >= 1e10 ? Math.min(first, st.mtimeMs) : st.mtimeMs,
|
|
446
|
+
maxTs: 0,
|
|
447
|
+
adopted: true,
|
|
448
|
+
size: st.size,
|
|
449
|
+
ino: st.ino
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
/** The live file is due to close: opened 24 h ago or past its segment size. */
|
|
453
|
+
const dueToClose = (file, s, now) => s.size > 0 && (now - s.openedAt >= 864e5 || s.size >= SEGMENTS[file].segmentBytes);
|
|
454
|
+
/** The bound a live file closes with (#42): the later of the close and its largest ts; an adopted one's - no tracked
|
|
455
|
+
* maximum for what came before this writer - its mtime + 24 h (#45), which bounds every line in it. */
|
|
456
|
+
function boundOf(path, s, now) {
|
|
457
|
+
if (!s.adopted) return Math.ceil(Math.max(now, s.maxTs));
|
|
458
|
+
let m;
|
|
459
|
+
try {
|
|
460
|
+
m = statSync(path).mtimeMs;
|
|
461
|
+
} catch {
|
|
462
|
+
m = now;
|
|
463
|
+
}
|
|
464
|
+
return Math.ceil(Math.max(s.maxTs, m + ADOPTED_SKEW_MS));
|
|
465
|
+
}
|
|
466
|
+
/** Closes the live file `file` in `dir` as a segment: a rename, never a copy. The segment's path, or null when there
|
|
467
|
+
* was nothing to close. A name already taken (two closes in one ms) takes a later bound - still conservative. */
|
|
468
|
+
function closeLive(dir, file, s, now) {
|
|
469
|
+
const live = join(dir, file);
|
|
470
|
+
if (!existsSync(live)) return null;
|
|
471
|
+
let bound = boundOf(live, s, now);
|
|
472
|
+
const openedAt = Math.min(Math.floor(s.openedAt), bound);
|
|
473
|
+
let to = join(dir, segmentName(file, openedAt, bound));
|
|
474
|
+
for (let i = 0; existsSync(to) && i < 1e3; i++) to = join(dir, segmentName(file, openedAt, ++bound));
|
|
475
|
+
renameSync(live, to);
|
|
476
|
+
return to;
|
|
477
|
+
}
|
|
478
|
+
/** Retention (#42): every closed segment of `file` whose bound is more than `days` old, unlinked - never read. How many went. */
|
|
479
|
+
function pruneAged(dir, file, now, days = 14) {
|
|
480
|
+
let n = 0;
|
|
481
|
+
for (const s of segmentsOf(dir, file)) {
|
|
482
|
+
if (s.bound >= now - days * DAY) continue;
|
|
483
|
+
try {
|
|
484
|
+
unlinkSync(s.path);
|
|
485
|
+
n++;
|
|
486
|
+
} catch {}
|
|
487
|
+
}
|
|
488
|
+
return n;
|
|
489
|
+
}
|
|
490
|
+
/** Lines in a file: its newline bytes (a torn last line counts as one). */
|
|
491
|
+
function countLines(path) {
|
|
492
|
+
try {
|
|
493
|
+
const b = readFileSync(path);
|
|
494
|
+
let n = 0;
|
|
495
|
+
for (let i = b.indexOf(10); i >= 0; i = b.indexOf(10, i + 1)) n++;
|
|
496
|
+
return b.length && b[b.length - 1] !== 10 ? n + 1 : n;
|
|
497
|
+
} catch {
|
|
498
|
+
return 0;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/** The safety cap (#43): while the file's segments and live file pass `cap` bytes, its oldest segment goes. The
|
|
502
|
+
* segments and lines that went (zero when it held). `subject`: the segments the cap counts (the app's leaves 1.0.0's
|
|
503
|
+
* to retention). */
|
|
504
|
+
function enforceCap(dir, file, liveSize, cap = SEGMENTS[file].capBytes, subject = () => true) {
|
|
505
|
+
const segs = segmentsOf(dir, file).filter(subject);
|
|
506
|
+
let total = liveSize + segs.reduce((n, s) => n + s.size, 0);
|
|
507
|
+
const out = {
|
|
508
|
+
segments: 0,
|
|
509
|
+
lines: 0
|
|
510
|
+
};
|
|
511
|
+
for (const s of segs) {
|
|
512
|
+
if (total <= cap) break;
|
|
513
|
+
const lines = countLines(s.path);
|
|
514
|
+
try {
|
|
515
|
+
unlinkSync(s.path);
|
|
516
|
+
} catch {
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
total -= s.size;
|
|
520
|
+
out.segments++;
|
|
521
|
+
out.lines += lines;
|
|
522
|
+
}
|
|
523
|
+
return out;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Every line of `file` in `dir`: its segments in `openedAt` order, then the live file (#42). A segment whose bound is
|
|
527
|
+
* before `since` holds nothing from then and is not read. A live file closed while it was read is followed into its
|
|
528
|
+
* segment by its inode, from where the read ended, so nothing is skipped or read twice.
|
|
529
|
+
*/
|
|
530
|
+
function readSegmented(dir, file, since = 0) {
|
|
531
|
+
const out = [];
|
|
532
|
+
const before = segmentsOf(dir, file);
|
|
533
|
+
const seen = new Set(before.map((s) => s.name));
|
|
534
|
+
const push = (text) => {
|
|
535
|
+
for (const l of text.split("\n")) if (l) out.push(l);
|
|
536
|
+
};
|
|
537
|
+
for (const s of before) if (s.bound >= since) try {
|
|
538
|
+
push(readFileSync(s.path, "utf8"));
|
|
539
|
+
} catch {}
|
|
540
|
+
let ino = -1, read = 0;
|
|
541
|
+
const live = join(dir, file);
|
|
542
|
+
let fd;
|
|
543
|
+
try {
|
|
544
|
+
if (lstatSync(live).isFile()) {
|
|
545
|
+
fd = openSync(live, "r");
|
|
546
|
+
ino = fstatSync(fd).ino;
|
|
547
|
+
const b = readAll(fd, 0);
|
|
548
|
+
read = b.length;
|
|
549
|
+
push(b.toString("utf8"));
|
|
550
|
+
}
|
|
551
|
+
} catch {} finally {
|
|
552
|
+
if (fd !== void 0) try {
|
|
553
|
+
closeSync(fd);
|
|
554
|
+
} catch {}
|
|
555
|
+
}
|
|
556
|
+
for (const s of segmentsOf(dir, file)) {
|
|
557
|
+
if (seen.has(s.name)) continue;
|
|
558
|
+
try {
|
|
559
|
+
if (statSync(s.path).ino === ino) {
|
|
560
|
+
const f = openSync(s.path, "r");
|
|
561
|
+
try {
|
|
562
|
+
push(readAll(f, read).toString("utf8"));
|
|
563
|
+
} finally {
|
|
564
|
+
closeSync(f);
|
|
565
|
+
}
|
|
566
|
+
} else if (s.bound >= since) push(readFileSync(s.path, "utf8"));
|
|
567
|
+
} catch {}
|
|
568
|
+
}
|
|
569
|
+
return out;
|
|
570
|
+
}
|
|
571
|
+
function readAll(fd, from) {
|
|
572
|
+
const chunks = [];
|
|
573
|
+
let pos = from;
|
|
574
|
+
for (;;) {
|
|
575
|
+
const b = Buffer.alloc(1 << 20);
|
|
576
|
+
const n = readSync(fd, b, 0, b.length, pos);
|
|
577
|
+
if (n <= 0) break;
|
|
578
|
+
chunks.push(b.subarray(0, n));
|
|
579
|
+
pos += n;
|
|
580
|
+
}
|
|
581
|
+
return Buffer.concat(chunks);
|
|
582
|
+
}
|
|
583
|
+
/** Every line from `since` as an object with a numeric `ts` (malformed lines skipped). */
|
|
584
|
+
function readSegmentedRecords(dir, file, since = 0) {
|
|
585
|
+
return readSegmented(dir, file, since).flatMap((l) => {
|
|
586
|
+
try {
|
|
587
|
+
const e = JSON.parse(l);
|
|
588
|
+
return e && typeof e === "object" && typeof e.ts === "number" && e.ts >= since ? [e] : [];
|
|
589
|
+
} catch {
|
|
590
|
+
return [];
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Every file an app flavour's records are in (#42, #44): its app file's segments in `openedAt` order then its live
|
|
596
|
+
* file, then its samples file's the same - the paths that exist now. A segment whose bound is before `since` holds
|
|
597
|
+
* nothing from then. The report, the CLI's views and the machine-profile pass read these.
|
|
598
|
+
*/
|
|
599
|
+
function appLogFiles(dir, flavour, since = 0) {
|
|
600
|
+
const out = [];
|
|
601
|
+
for (const name of [APP_LOG[flavour], APP_SAMPLES_LOG[flavour]]) {
|
|
602
|
+
for (const s of segmentsOf(dir, name)) if (s.bound >= since) out.push(s.path);
|
|
603
|
+
if (existsSync(join(dir, name))) out.push(join(dir, name));
|
|
604
|
+
}
|
|
605
|
+
return out;
|
|
606
|
+
}
|
|
607
|
+
//#endregion
|
|
608
|
+
//#region src/server/perf-jam.ts
|
|
609
|
+
/**
|
|
610
|
+
* Jam timings (0.26.1, spec 27a Part D): one JSON line per event in design/.local/perf/jam.jsonl - local, never
|
|
611
|
+
* committed, never published, never sent. `npx marver perf --jam` reads it back.
|
|
612
|
+
*
|
|
613
|
+
* Numbers and fixed codes only. Every event is rebuilt from an allowlist: opaque ids, frame ids, enums, bounded
|
|
614
|
+
* integers - no words, notes, prompts, replies, titles, names, timelines or free-text errors (a failure is a code).
|
|
615
|
+
* Writes are buffered and appended by one serialized writer off the hot path; past 1,000 events or 1 MB waiting,
|
|
616
|
+
* new events are dropped and counted (the count written when the writer catches up). A write that fails changes
|
|
617
|
+
* nothing for the operation it measured.
|
|
618
|
+
*
|
|
619
|
+
* Spec 30 R9 (#42, #43): a live file plus closed segments, as perf.ts's logs - 2 MB or 24 h a segment, 14 days, an
|
|
620
|
+
* 8 MB safety cap - and every close, prune and cap runs in this writer's own queue, between two appends: the dev
|
|
621
|
+
* server's start queues its upkeep here (`tidyJam`), and the queue runs it again hourly. Nothing is rewritten.
|
|
622
|
+
*/
|
|
623
|
+
var perf_jam_exports = /* @__PURE__ */ __exportAll({
|
|
624
|
+
FAILURES: () => FAILURES,
|
|
625
|
+
KINDS: () => KINDS,
|
|
626
|
+
RUN_OUTCOMES: () => RUN_OUTCOMES,
|
|
627
|
+
SHOT_OUTCOMES: () => SHOT_OUTCOMES,
|
|
628
|
+
cleanJamEvent: () => cleanJamEvent,
|
|
629
|
+
jamLog: () => jamLog,
|
|
630
|
+
jamSeams: () => jamSeams,
|
|
631
|
+
logJam: () => logJam,
|
|
632
|
+
readJam: () => readJam,
|
|
633
|
+
tidyJam: () => tidyJam
|
|
634
|
+
});
|
|
635
|
+
const jamLog = (root) => join(root, "design", ".local", "perf", "jam.jsonl");
|
|
636
|
+
const FILE$1 = "jam.jsonl";
|
|
637
|
+
const PRUNE_EVERY_MS$1 = 36e5;
|
|
638
|
+
/** A test seam: how a batch of lines is appended (to hold an append open). Never set outside tests. */
|
|
639
|
+
const jamSeams = {};
|
|
640
|
+
const clock$1 = () => (jamSeams.now ?? Date.now)();
|
|
641
|
+
const MAX_EVENTS = 1e3;
|
|
642
|
+
const MAX_BYTES = 1e6;
|
|
643
|
+
const CAPTURE = /^cap-\d{10,16}-[a-z0-9]{4,12}$/;
|
|
644
|
+
const BATCH = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
645
|
+
const FRAME$1 = /^[a-z0-9][\w-]{0,79}(\/[a-z0-9][\w-]{0,79}){1,3}$/i;
|
|
646
|
+
const capId = (v) => typeof v === "string" && CAPTURE.test(v) ? v : void 0;
|
|
647
|
+
const batchId = (v) => typeof v === "string" && BATCH.test(v) ? v : void 0;
|
|
648
|
+
const frameId = (v) => typeof v === "string" && FRAME$1.test(v) ? v : void 0;
|
|
649
|
+
const ms$1 = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v < 1e9 ? Math.round(v) : void 0;
|
|
650
|
+
const count$1 = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e6 ? v : void 0;
|
|
651
|
+
const oneOf$1 = (v, set) => typeof v === "string" && set.includes(v) ? v : void 0;
|
|
652
|
+
const msList = (v, max = 50) => Array.isArray(v) ? v.slice(0, max).map(ms$1).filter((x) => x !== void 0) : void 0;
|
|
653
|
+
const prune$1 = (o) => Object.fromEntries(Object.entries(o).filter(([, x]) => x !== void 0 && !(Array.isArray(x) && !x.length)));
|
|
654
|
+
const RUN_OUTCOMES = [
|
|
655
|
+
"review",
|
|
656
|
+
"done",
|
|
657
|
+
"asks",
|
|
658
|
+
"failed"
|
|
659
|
+
];
|
|
660
|
+
const FAILURES = [
|
|
661
|
+
"agent",
|
|
662
|
+
"changed",
|
|
663
|
+
"delivery",
|
|
664
|
+
"stopped",
|
|
665
|
+
"other"
|
|
666
|
+
];
|
|
667
|
+
const SHOT_OUTCOMES = [
|
|
668
|
+
"ok",
|
|
669
|
+
"unchecked",
|
|
670
|
+
"stale",
|
|
671
|
+
"timeout",
|
|
672
|
+
"error"
|
|
673
|
+
];
|
|
674
|
+
const KINDS = [
|
|
675
|
+
"capture",
|
|
676
|
+
"comment",
|
|
677
|
+
"wrapup"
|
|
678
|
+
];
|
|
679
|
+
/** An event rebuilt from the allowlist, or null. */
|
|
680
|
+
function cleanJamEvent(raw) {
|
|
681
|
+
switch (raw.kind) {
|
|
682
|
+
case "capture": {
|
|
683
|
+
const p = raw.pictures && typeof raw.pictures === "object" ? raw.pictures : {};
|
|
684
|
+
const capture = capId(raw.capture);
|
|
685
|
+
if (!capture) return null;
|
|
686
|
+
const l = p.live && typeof p.live === "object" ? p.live : null;
|
|
687
|
+
const live = l ? prune$1({
|
|
688
|
+
count: count$1(l.count),
|
|
689
|
+
failed: count$1(l.failed),
|
|
690
|
+
fellBack: count$1(l.fellBack)
|
|
691
|
+
}) : void 0;
|
|
692
|
+
return prune$1({
|
|
693
|
+
kind: "capture",
|
|
694
|
+
capture,
|
|
695
|
+
sealMs: ms$1(raw.sealMs),
|
|
696
|
+
wordsWaitMs: ms$1(raw.wordsWaitMs),
|
|
697
|
+
pictures: prune$1({
|
|
698
|
+
count: count$1(p.count),
|
|
699
|
+
ms: msList(p.ms),
|
|
700
|
+
late: count$1(p.late),
|
|
701
|
+
failed: count$1(p.failed),
|
|
702
|
+
live: live && Object.keys(live).length ? live : void 0
|
|
703
|
+
}),
|
|
704
|
+
interrupted: raw.interrupted === true ? true : void 0
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
case "upload": {
|
|
708
|
+
const capture = capId(raw.capture);
|
|
709
|
+
return capture ? prune$1({
|
|
710
|
+
kind: "upload",
|
|
711
|
+
capture,
|
|
712
|
+
ms: ms$1(raw.ms),
|
|
713
|
+
tries: count$1(raw.tries)
|
|
714
|
+
}) : null;
|
|
715
|
+
}
|
|
716
|
+
case "name": {
|
|
717
|
+
const capture = capId(raw.capture);
|
|
718
|
+
return capture ? prune$1({
|
|
719
|
+
kind: "name",
|
|
720
|
+
capture,
|
|
721
|
+
ms: ms$1(raw.ms),
|
|
722
|
+
ok: raw.ok === true
|
|
723
|
+
}) : null;
|
|
724
|
+
}
|
|
725
|
+
case "words": return prune$1({
|
|
726
|
+
kind: "words",
|
|
727
|
+
audioMs: ms$1(raw.audioMs),
|
|
728
|
+
waitMs: ms$1(raw.waitMs),
|
|
729
|
+
decodeMs: ms$1(raw.decodeMs),
|
|
730
|
+
backlogMs: ms$1(raw.backlogMs),
|
|
731
|
+
modelLoadMs: ms$1(raw.modelLoadMs)
|
|
732
|
+
});
|
|
733
|
+
case "run": {
|
|
734
|
+
const batch = batchId(raw.batch);
|
|
735
|
+
if (!batch) return null;
|
|
736
|
+
const members = Array.isArray(raw.members) ? raw.members.slice(0, 50).map((m) => prune$1({
|
|
737
|
+
kind: oneOf$1(m?.kind, KINDS),
|
|
738
|
+
waitMs: ms$1(m?.waitMs),
|
|
739
|
+
outcome: oneOf$1(m?.outcome, RUN_OUTCOMES)
|
|
740
|
+
})) : void 0;
|
|
741
|
+
return prune$1({
|
|
742
|
+
kind: "run",
|
|
743
|
+
batch,
|
|
744
|
+
members,
|
|
745
|
+
beforeMs: ms$1(raw.beforeMs),
|
|
746
|
+
agentMs: msList(raw.agentMs, 10),
|
|
747
|
+
afterMs: ms$1(raw.afterMs),
|
|
748
|
+
deliverMs: ms$1(raw.deliverMs),
|
|
749
|
+
attempts: count$1(raw.attempts),
|
|
750
|
+
failure: oneOf$1(raw.failure, FAILURES),
|
|
751
|
+
redelivered: raw.redelivered === true ? true : void 0
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
case "shot": return prune$1({
|
|
755
|
+
kind: "shot",
|
|
756
|
+
frame: frameId(raw.frame),
|
|
757
|
+
scale: count$1(raw.scale),
|
|
758
|
+
ms: ms$1(raw.ms),
|
|
759
|
+
recaptures: count$1(raw.recaptures),
|
|
760
|
+
outcome: oneOf$1(raw.outcome, SHOT_OUTCOMES)
|
|
761
|
+
});
|
|
762
|
+
default: return null;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
const writers$1 = /* @__PURE__ */ new Map();
|
|
766
|
+
/** Captures already logged, per root: a resent capture event is written once. */
|
|
767
|
+
const seenCaptures = /* @__PURE__ */ new Map();
|
|
768
|
+
/** Log one Jam event (best effort, never in the way): rebuilt from the allowlist, buffered, appended in order. */
|
|
769
|
+
function logJam(root, raw) {
|
|
770
|
+
try {
|
|
771
|
+
const ev = cleanJamEvent(raw);
|
|
772
|
+
if (!ev) return;
|
|
773
|
+
if (ev.kind === "capture") {
|
|
774
|
+
const seen = seenCaptures.get(root) ?? /* @__PURE__ */ new Set();
|
|
775
|
+
seenCaptures.set(root, seen);
|
|
776
|
+
if (seen.has(ev.capture)) return;
|
|
777
|
+
seen.add(ev.capture);
|
|
778
|
+
if (seen.size > 5e3) seen.delete(seen.values().next().value);
|
|
779
|
+
}
|
|
780
|
+
const line = JSON.stringify({
|
|
781
|
+
v: 1,
|
|
782
|
+
ts: Date.now(),
|
|
783
|
+
...ev
|
|
784
|
+
}) + "\n";
|
|
785
|
+
const w = writerOf(root);
|
|
786
|
+
if (w.queue.length + w.inFlight >= MAX_EVENTS || w.bytes + line.length > MAX_BYTES) {
|
|
787
|
+
w.dropped++;
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
w.queue.push(line);
|
|
791
|
+
w.bytes += line.length;
|
|
792
|
+
kick(root, w);
|
|
793
|
+
} catch {}
|
|
794
|
+
}
|
|
795
|
+
function writerOf(root) {
|
|
796
|
+
let w = writers$1.get(root);
|
|
797
|
+
if (!w) writers$1.set(root, w = {
|
|
798
|
+
queue: [],
|
|
799
|
+
bytes: 0,
|
|
800
|
+
inFlight: 0,
|
|
801
|
+
dropped: 0,
|
|
802
|
+
busy: false,
|
|
803
|
+
writes: 0,
|
|
804
|
+
live: null,
|
|
805
|
+
upkeep: false,
|
|
806
|
+
prunedAt: 0
|
|
807
|
+
});
|
|
808
|
+
return w;
|
|
809
|
+
}
|
|
810
|
+
function kick(root, w) {
|
|
811
|
+
if (!w.busy) {
|
|
812
|
+
w.busy = true;
|
|
813
|
+
w.draining = new Promise((done) => setImmediate(() => void drain(root, w).finally(done)));
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
/** The dev server's start (perf.ts `tidyPerfDir`): the log's upkeep queued in its own writer - a live file opened 24 h
|
|
817
|
+
* ago closed (a 0.29 file a segment, #45), segments past 14 days unlinked, the safety cap. */
|
|
818
|
+
function tidyJam(root, now = clock$1()) {
|
|
819
|
+
try {
|
|
820
|
+
const w = writerOf(root);
|
|
821
|
+
w.upkeep = true;
|
|
822
|
+
w.prunedAt = now;
|
|
823
|
+
kick(root, w);
|
|
824
|
+
} catch {}
|
|
825
|
+
}
|
|
826
|
+
/** The live file's state: this writer's, else adopted from the disk; a file replaced behind its back is adopted again. */
|
|
827
|
+
function liveOf$1(file, w) {
|
|
828
|
+
let st;
|
|
829
|
+
try {
|
|
830
|
+
st = lstatSync(file);
|
|
831
|
+
} catch {
|
|
832
|
+
w.live = null;
|
|
833
|
+
return null;
|
|
834
|
+
}
|
|
835
|
+
if (!st.isFile()) throw new Error("not a regular file");
|
|
836
|
+
if (w.live && w.live.ino === st.ino) {
|
|
837
|
+
w.live.size = st.size;
|
|
838
|
+
return w.live;
|
|
839
|
+
}
|
|
840
|
+
return w.live = adoptLive(file);
|
|
841
|
+
}
|
|
842
|
+
/** Closes a due live file; with `prune`, unlinks the segments past 14 days; after either, the safety cap - its counted
|
|
843
|
+
* `dropped` line returned to go out with the next append. Synchronous: between two appends of this queue. */
|
|
844
|
+
function upkeep$1(file, w, prune) {
|
|
845
|
+
const dir = join(file, ".."), now = clock$1();
|
|
846
|
+
try {
|
|
847
|
+
const s = liveOf$1(file, w);
|
|
848
|
+
let closed = false;
|
|
849
|
+
if (s && dueToClose(FILE$1, s, now)) {
|
|
850
|
+
closeLive(dir, FILE$1, s, now);
|
|
851
|
+
w.live = null;
|
|
852
|
+
closed = true;
|
|
853
|
+
}
|
|
854
|
+
if (prune) pruneAged(dir, FILE$1, now);
|
|
855
|
+
if (!prune && !closed) return null;
|
|
856
|
+
const went = enforceCap(dir, FILE$1, w.live?.size ?? 0);
|
|
857
|
+
return went.segments ? JSON.stringify({
|
|
858
|
+
v: 1,
|
|
859
|
+
ts: now,
|
|
860
|
+
kind: "dropped",
|
|
861
|
+
count: went.lines,
|
|
862
|
+
reason: "retention-cap"
|
|
863
|
+
}) + "\n" : null;
|
|
864
|
+
} catch {
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
async function drain(root, w) {
|
|
869
|
+
const file = jamLog(root);
|
|
870
|
+
try {
|
|
871
|
+
mkdirSync(join(file, ".."), {
|
|
872
|
+
recursive: true,
|
|
873
|
+
mode: 448
|
|
874
|
+
});
|
|
875
|
+
} catch {}
|
|
876
|
+
/** the cap's line after a close past the segment size: it goes first in the next append */
|
|
877
|
+
let carry = null;
|
|
878
|
+
while (w.queue.length || w.dropped || w.upkeep || carry) {
|
|
879
|
+
const prune = w.upkeep || clock$1() - w.prunedAt >= PRUNE_EVERY_MS$1;
|
|
880
|
+
if (prune) w.prunedAt = clock$1();
|
|
881
|
+
w.upkeep = false;
|
|
882
|
+
const capped = upkeep$1(file, w, prune);
|
|
883
|
+
const lines = w.queue.splice(0);
|
|
884
|
+
const held = lines.reduce((n, l) => n + l.length, 0);
|
|
885
|
+
w.inFlight = lines.length;
|
|
886
|
+
if (capped) lines.unshift(capped);
|
|
887
|
+
if (carry) {
|
|
888
|
+
lines.unshift(carry);
|
|
889
|
+
carry = null;
|
|
890
|
+
}
|
|
891
|
+
if (w.dropped) {
|
|
892
|
+
lines.push(JSON.stringify({
|
|
893
|
+
v: 1,
|
|
894
|
+
ts: Date.now(),
|
|
895
|
+
kind: "dropped",
|
|
896
|
+
count: w.dropped
|
|
897
|
+
}) + "\n");
|
|
898
|
+
w.dropped = 0;
|
|
899
|
+
}
|
|
900
|
+
if (lines.length) try {
|
|
901
|
+
const before = liveOf$1(file, w);
|
|
902
|
+
await (jamSeams.append ?? ((f, d) => appendFile(f, d, { mode: 384 })))(file, lines.join(""));
|
|
903
|
+
w.writes++;
|
|
904
|
+
let max = 0;
|
|
905
|
+
for (const l of lines) {
|
|
906
|
+
const t = lineTs(l);
|
|
907
|
+
if (t !== null && t > max) max = t;
|
|
908
|
+
}
|
|
909
|
+
const st = lstatSync(file);
|
|
910
|
+
if (before && before.ino === st.ino && w.live === before) {
|
|
911
|
+
before.size = st.size;
|
|
912
|
+
before.maxTs = Math.max(before.maxTs, max);
|
|
913
|
+
} else w.live = {
|
|
914
|
+
openedAt: clock$1(),
|
|
915
|
+
maxTs: max,
|
|
916
|
+
adopted: false,
|
|
917
|
+
size: st.size,
|
|
918
|
+
ino: st.ino
|
|
919
|
+
};
|
|
920
|
+
carry = upkeep$1(file, w, false);
|
|
921
|
+
} catch {}
|
|
922
|
+
w.inFlight = 0;
|
|
923
|
+
w.bytes = Math.max(0, w.bytes - held);
|
|
924
|
+
}
|
|
925
|
+
w.busy = false;
|
|
926
|
+
}
|
|
927
|
+
/** The log read back from `since` (epoch ms) - its segments in `openedAt` order, then the live file: malformed lines
|
|
928
|
+
* skipped. `first` is the oldest line kept. */
|
|
929
|
+
function readJam(root, since = 0) {
|
|
930
|
+
try {
|
|
931
|
+
const all = readSegmented(join(jamLog(root), ".."), FILE$1).flatMap((l) => {
|
|
932
|
+
try {
|
|
933
|
+
const e = JSON.parse(l);
|
|
934
|
+
return e && typeof e.ts === "number" ? [e] : [];
|
|
935
|
+
} catch {
|
|
936
|
+
return [];
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
return {
|
|
940
|
+
events: all.filter((e) => e.ts >= since),
|
|
941
|
+
...all.length ? { first: all.reduce((m, e) => Math.min(m, e.ts), Infinity) } : {}
|
|
942
|
+
};
|
|
943
|
+
} catch {
|
|
944
|
+
return { events: [] };
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
//#endregion
|
|
948
|
+
//#region src/server/perf.ts
|
|
949
|
+
/**
|
|
950
|
+
* The perf logs, one JSON line per event in design/.local/perf/ - local, never committed, the owner's alone (the
|
|
951
|
+
* directory 700, every file in it 600). `npx marver perf` reads them back.
|
|
952
|
+
* - reloads.jsonl, the reload log (spec 26 Part 1): the server's view (an edit and the frames it reached, a full
|
|
953
|
+
* reload sent or dropped) and the page's (each document that actually booted, and how long it took);
|
|
954
|
+
* - page.jsonl, the recorder's page half (spec 28 A1.7): what each board load did, from the page - its request and
|
|
955
|
+
* commit, each frame's outcome, every move and why, quiet, the last move and settled - plus gestures and
|
|
956
|
+
* errors. Each record is rebuilt from its kind's allowlist; `npx marver perf --loads` reads them back.
|
|
957
|
+
*
|
|
958
|
+
* Spec 30: every log is written and read through `perfLogs` (#1, #42) - one writer per file: a live file plus closed
|
|
959
|
+
* segments, retention by age and the safety cap (perf-segments.ts); the records' shapes and bounds are
|
|
960
|
+
* src/shared/perf-records.ts's. Each stream fills its own kinds' cleaners in `BODY`.
|
|
961
|
+
*/
|
|
962
|
+
const perfDir = (root) => join(root, "design", ".local", "perf");
|
|
963
|
+
/** design/, design/.local and its perf/ must be real directories inside the project: a log is never written through a link. */
|
|
964
|
+
const unsafe = (root) => checkRealDirs(root, [
|
|
965
|
+
[join(root, "design"), "design"],
|
|
966
|
+
[join(root, "design", ".local"), "design/.local"],
|
|
967
|
+
[perfDir(root), "design/.local/perf"]
|
|
968
|
+
]);
|
|
969
|
+
const FILE = {
|
|
970
|
+
page: "page.jsonl",
|
|
971
|
+
reloads: "reloads.jsonl",
|
|
972
|
+
runs: "runs.jsonl"
|
|
973
|
+
};
|
|
974
|
+
const LOGS = Object.keys(FILE);
|
|
975
|
+
/**
|
|
976
|
+
* The writer's state: each live file it knows (when it was opened, the largest `ts` written into it, its size) and
|
|
977
|
+
* when each directory was last pruned. Synchronous, so every close, prune and cap runs between two appends - the
|
|
978
|
+
* writer's own queue (#42). One writer per root: the dev server.
|
|
979
|
+
*/
|
|
980
|
+
const lives = /* @__PURE__ */ new Map();
|
|
981
|
+
const prunedAt = /* @__PURE__ */ new Map();
|
|
982
|
+
/** Test seam: the writer's clock (retention and the 24 h close read it), and its state forgotten (a restart). */
|
|
983
|
+
const perfLogSeams = { reset() {
|
|
984
|
+
lives.clear();
|
|
985
|
+
prunedAt.clear();
|
|
986
|
+
} };
|
|
987
|
+
const clock = () => (perfLogSeams.now ?? Date.now)();
|
|
988
|
+
/** The live file's state: what this writer tracked, else adopted from the disk (#42, #45), else none. A file replaced
|
|
989
|
+
* behind the writer's back (another inode) is adopted again. */
|
|
990
|
+
function liveOf(path) {
|
|
991
|
+
const known = lives.get(path);
|
|
992
|
+
let st;
|
|
993
|
+
try {
|
|
994
|
+
st = lstatSync(path);
|
|
995
|
+
} catch {
|
|
996
|
+
lives.delete(path);
|
|
997
|
+
return null;
|
|
998
|
+
}
|
|
999
|
+
if (!st.isFile()) throw new Error("not a regular file");
|
|
1000
|
+
if (known && known.ino === st.ino) {
|
|
1001
|
+
known.size = st.size;
|
|
1002
|
+
return known;
|
|
1003
|
+
}
|
|
1004
|
+
const s = adoptLive(path);
|
|
1005
|
+
if (s) lives.set(path, s);
|
|
1006
|
+
else lives.delete(path);
|
|
1007
|
+
return s;
|
|
1008
|
+
}
|
|
1009
|
+
/** One log's upkeep (#42, #43): a live file opened 24 h ago or past its size closed; segments past the days kept
|
|
1010
|
+
* unlinked; the oldest gone while the file passes its cap, then a counted `dropped` line. Never throws. */
|
|
1011
|
+
function upkeep(root, log, now, prune) {
|
|
1012
|
+
const dir = perfDir(root), file = FILE[log], path = join(dir, file);
|
|
1013
|
+
try {
|
|
1014
|
+
const s = liveOf(path);
|
|
1015
|
+
let closed = false;
|
|
1016
|
+
if (s && dueToClose(file, s, now)) {
|
|
1017
|
+
closeLive(dir, file, s, now);
|
|
1018
|
+
lives.delete(path);
|
|
1019
|
+
closed = true;
|
|
1020
|
+
}
|
|
1021
|
+
if (prune) pruneAged(dir, file, now);
|
|
1022
|
+
if (prune || closed) {
|
|
1023
|
+
const went = enforceCap(dir, file, lives.get(path)?.size ?? 0);
|
|
1024
|
+
if (went.segments) write(root, log, [JSON.stringify(droppedLine(log, went.lines, now))], now);
|
|
1025
|
+
}
|
|
1026
|
+
} catch {}
|
|
1027
|
+
}
|
|
1028
|
+
/** The safety cap's line (#43): page.jsonl's names no page (it is the server's), runs.jsonl's no run. */
|
|
1029
|
+
const droppedLine = (log, n, now) => log === "reloads" ? {
|
|
1030
|
+
ts: now,
|
|
1031
|
+
kind: "dropped",
|
|
1032
|
+
n,
|
|
1033
|
+
reason: "retention-cap"
|
|
1034
|
+
} : {
|
|
1035
|
+
v: 1,
|
|
1036
|
+
ts: now,
|
|
1037
|
+
kind: "dropped",
|
|
1038
|
+
n,
|
|
1039
|
+
reason: "retention-cap"
|
|
1040
|
+
};
|
|
1041
|
+
/** The append itself: the directory 700 and the file 600 from their creation, never through a link. */
|
|
1042
|
+
function write(root, log, lines, now) {
|
|
1043
|
+
const dir = perfDir(root), path = join(dir, FILE[log]);
|
|
1044
|
+
const s = liveOf(path);
|
|
1045
|
+
const text = lines.join("\n") + "\n";
|
|
1046
|
+
appendFileSync(path, text, { mode: 384 });
|
|
1047
|
+
let max = 0;
|
|
1048
|
+
for (const l of lines) {
|
|
1049
|
+
const t = lineTs(l);
|
|
1050
|
+
if (t !== null && t > max) max = t;
|
|
1051
|
+
}
|
|
1052
|
+
const st = lstatSync(path);
|
|
1053
|
+
if (s && s.ino === st.ino) {
|
|
1054
|
+
s.size = st.size;
|
|
1055
|
+
s.maxTs = Math.max(s.maxTs, max);
|
|
1056
|
+
} else lives.set(path, {
|
|
1057
|
+
openedAt: now,
|
|
1058
|
+
maxTs: max,
|
|
1059
|
+
adopted: false,
|
|
1060
|
+
size: st.size,
|
|
1061
|
+
ino: st.ino
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
/** One append to a perf log (#42): a due close first, retention hourly, then the lines; past its segment size the live
|
|
1065
|
+
* file closes at once. Throws when the append failed - callers swallow, or answer 503. */
|
|
1066
|
+
function appendLog(root, log, lines) {
|
|
1067
|
+
const why = unsafe(root);
|
|
1068
|
+
if (why) throw new Error(why);
|
|
1069
|
+
mkdirSync(join(root, "design", ".local"), { recursive: true });
|
|
1070
|
+
mkdirSync(perfDir(root), {
|
|
1071
|
+
recursive: true,
|
|
1072
|
+
mode: 448
|
|
1073
|
+
});
|
|
1074
|
+
const now = clock();
|
|
1075
|
+
const dir = perfDir(root);
|
|
1076
|
+
if (now - (prunedAt.get(dir) ?? 0) >= 36e5) {
|
|
1077
|
+
prunedAt.set(dir, now);
|
|
1078
|
+
for (const l of LOGS) upkeep(root, l, now, true);
|
|
1079
|
+
} else upkeep(root, log, now, false);
|
|
1080
|
+
write(root, log, lines, now);
|
|
1081
|
+
upkeep(root, log, now, false);
|
|
1082
|
+
}
|
|
1083
|
+
const perfLogs = {
|
|
1084
|
+
append(root, log, lines) {
|
|
1085
|
+
if (lines.length) appendLog(root, log, lines);
|
|
1086
|
+
},
|
|
1087
|
+
read(root, log, since = 0) {
|
|
1088
|
+
return readSegmentedRecords(perfDir(root), FILE[log], since);
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
/** Lines of one event, one append and one time (spec 30 #10, #12): a list and its parts stay together. Never retried. */
|
|
1092
|
+
function logReloadLines(root, entries) {
|
|
1093
|
+
const ts = Date.now();
|
|
1094
|
+
try {
|
|
1095
|
+
perfLogs.append(root, "reloads", entries.map((e) => JSON.stringify({
|
|
1096
|
+
ts,
|
|
1097
|
+
...e
|
|
1098
|
+
})));
|
|
1099
|
+
} catch {}
|
|
1100
|
+
}
|
|
1101
|
+
const readReloads = (root, since = 0) => perfLogs.read(root, "reloads", since);
|
|
1102
|
+
/** The page's records from `since` (epoch ms): malformed lines skipped. */
|
|
1103
|
+
const readPage = (root, since = 0) => perfLogs.read(root, "page", since);
|
|
1104
|
+
/** What one request may carry. */
|
|
1105
|
+
const PAGE_BODY_MAX = 524288;
|
|
1106
|
+
/** 0.29's kinds and spec 30's, one list with the page's (perf-records.ts) */
|
|
1107
|
+
const PAGE_KINDS = PAGE_KINDS$1;
|
|
1108
|
+
const LOAD_CAUSES = [
|
|
1109
|
+
"boot",
|
|
1110
|
+
"switch",
|
|
1111
|
+
"disk-reload"
|
|
1112
|
+
];
|
|
1113
|
+
const CUT_REASONS = [
|
|
1114
|
+
"switch",
|
|
1115
|
+
"reload",
|
|
1116
|
+
"hidden",
|
|
1117
|
+
"timeout",
|
|
1118
|
+
"superseded",
|
|
1119
|
+
"error",
|
|
1120
|
+
"edited",
|
|
1121
|
+
"unsaved"
|
|
1122
|
+
];
|
|
1123
|
+
const OUTCOMES = [
|
|
1124
|
+
"settled",
|
|
1125
|
+
"ready",
|
|
1126
|
+
"missing",
|
|
1127
|
+
"error",
|
|
1128
|
+
"never-ready",
|
|
1129
|
+
"cut"
|
|
1130
|
+
];
|
|
1131
|
+
const GESTURES = [
|
|
1132
|
+
"pan",
|
|
1133
|
+
"zoom",
|
|
1134
|
+
"layout"
|
|
1135
|
+
];
|
|
1136
|
+
/** why records were dropped, when it is not the queue's bound or a failed send: errors past a load's 50, and spec 30's
|
|
1137
|
+
* sources, commands, transitions and the safety cap (perf-records.ts) */
|
|
1138
|
+
const DROP_REASONS = DROP_REASONS$1;
|
|
1139
|
+
const ID = /^[0-9a-f]{12}$/;
|
|
1140
|
+
const WS = /^[0-9a-f]{16}$/;
|
|
1141
|
+
const CAUSE = /^[a-z][a-z0-9:+-]{0,63}$/;
|
|
1142
|
+
const BOARD = /^(?:~unplaced|[a-z0-9][a-z0-9-]{0,63})$/;
|
|
1143
|
+
const str = (v, n = 200) => typeof v === "string" && v ? v.slice(0, n) : void 0;
|
|
1144
|
+
const id = (v) => typeof v === "string" && ID.test(v) ? v : void 0;
|
|
1145
|
+
const count = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e7 ? v : void 0;
|
|
1146
|
+
const ms = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v < 1e9 ? Math.round(v) : void 0;
|
|
1147
|
+
const tenth = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v < 1e7 ? Math.round(v * 10) / 10 : void 0;
|
|
1148
|
+
/** the display's refresh rate the Mac app told the shell (spec 28 A3.8 #9): 1-1000 Hz */
|
|
1149
|
+
const hz = (v) => typeof v === "number" && Number.isFinite(v) && v >= 1 && v <= 1e3 ? Math.round(v) : void 0;
|
|
1150
|
+
const delta = (v) => typeof v === "number" && Number.isFinite(v) && Math.abs(v) < 1e7 ? Math.round(v) : void 0;
|
|
1151
|
+
const flag = (v) => v === true ? true : void 0;
|
|
1152
|
+
const oneOf = (v, set) => typeof v === "string" && set.includes(v) ? v : void 0;
|
|
1153
|
+
const obj = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : {};
|
|
1154
|
+
const prune = (o) => Object.fromEntries(Object.entries(o).filter(([, x]) => x !== void 0));
|
|
1155
|
+
const cause = (v) => typeof v === "string" && CAUSE.test(v) ? v : void 0;
|
|
1156
|
+
/** #18: a load's candidate writers: at most 8, each its page and at most 8 command ids, and their count. */
|
|
1157
|
+
const writers = (v) => {
|
|
1158
|
+
if (!Array.isArray(v)) return void 0;
|
|
1159
|
+
const out = v.slice(0, RELOAD_BOUNDS.candidates).flatMap((x) => {
|
|
1160
|
+
const c = obj(x), page = id(c.page), cmds = Array.isArray(c.cmds) ? c.cmds.slice(0, RELOAD_BOUNDS.candidates).flatMap((y) => id(y) ?? []) : [];
|
|
1161
|
+
return page ? [{
|
|
1162
|
+
page,
|
|
1163
|
+
cmds,
|
|
1164
|
+
n: Math.max(count(c.n) ?? 0, cmds.length)
|
|
1165
|
+
}] : [];
|
|
1166
|
+
});
|
|
1167
|
+
return out.length ? out : void 0;
|
|
1168
|
+
};
|
|
1169
|
+
const fixed = (v, re) => typeof v === "string" && re.test(v) ? v : void 0;
|
|
1170
|
+
const ROUTE_RE = /^[a-z0-9:._/-]{1,64}$/i;
|
|
1171
|
+
const METHOD_RE = /^[A-Z]{3,7}$/;
|
|
1172
|
+
const THEME_RE = /^[\w][\w .-]{0,63}$/;
|
|
1173
|
+
const status = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e3 ? v : void 0;
|
|
1174
|
+
const epoch = (v) => typeof v === "number" && Number.isFinite(v) && v > 0xe8d4a51000 && v < 0x5af3107a4000 ? Math.round(v) : void 0;
|
|
1175
|
+
/** #29: a file inside the repo, relative to it; anything else as given - this Mac's own path, in the owner's own file. */
|
|
1176
|
+
const repoFile = (v, root) => {
|
|
1177
|
+
const f = str(v);
|
|
1178
|
+
if (!f || !root || !isAbsolute(f)) return f;
|
|
1179
|
+
const rel = relative(root, f);
|
|
1180
|
+
return rel && !rel.startsWith("..") && !isAbsolute(rel) ? rel : f;
|
|
1181
|
+
};
|
|
1182
|
+
/** #34 */
|
|
1183
|
+
const scaleOf = (v) => {
|
|
1184
|
+
const o = obj(v), num = (x) => typeof x === "number" && Number.isFinite(x) && x > 0 && x < 1e4 ? Math.round(x * 1e3) / 1e3 : void 0;
|
|
1185
|
+
const from = num(o.from), to = num(o.to), min = num(o.min), max = num(o.max);
|
|
1186
|
+
return from !== void 0 && to !== void 0 && min !== void 0 && max !== void 0 ? {
|
|
1187
|
+
from,
|
|
1188
|
+
to,
|
|
1189
|
+
min,
|
|
1190
|
+
max
|
|
1191
|
+
} : void 0;
|
|
1192
|
+
};
|
|
1193
|
+
/** #35: at most 3; a script's `src` a path (`/...`), never a query */
|
|
1194
|
+
const loafs = (v) => {
|
|
1195
|
+
if (!Array.isArray(v)) return void 0;
|
|
1196
|
+
const out = v.slice(0, LOAF.kept).flatMap((x) => {
|
|
1197
|
+
const o = obj(x), m = ms(o.ms);
|
|
1198
|
+
if (m === void 0) return [];
|
|
1199
|
+
const c = obj(o.script), src = typeof c.src === "string" && c.src.startsWith("/") ? c.src.split("?")[0].slice(0, 200) : void 0;
|
|
1200
|
+
const script = o.script && ms(c.ms) !== void 0 ? prune({
|
|
1201
|
+
src,
|
|
1202
|
+
frame: str(c.frame),
|
|
1203
|
+
fn: str(c.fn, LOAF.fn),
|
|
1204
|
+
invoker: str(c.invoker, LOAF.fn),
|
|
1205
|
+
ms: ms(c.ms),
|
|
1206
|
+
where: str(c.where, 32)
|
|
1207
|
+
}) : void 0;
|
|
1208
|
+
return [prune({
|
|
1209
|
+
ms: m,
|
|
1210
|
+
blockMs: ms(o.blockMs),
|
|
1211
|
+
script
|
|
1212
|
+
})];
|
|
1213
|
+
});
|
|
1214
|
+
return out.length ? out : void 0;
|
|
1215
|
+
};
|
|
1216
|
+
/** A command's name or a code (its `why`, a step), as commands.ts writes them. */
|
|
1217
|
+
const NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
1218
|
+
/** `descriptor()`'s combination (jam-keys.ts:33): modifiers, then one key - one character or a named key. */
|
|
1219
|
+
const KEYS = /^(?:Ctrl\+)?(?:Meta\+)?(?:Alt\+)?(?:Shift\+)?(?:[^\s]|[A-Z][A-Za-z0-9]{1,15})$/;
|
|
1220
|
+
/** A frame id (`scene/name`) or a node key. */
|
|
1221
|
+
const FRAME = /^[A-Za-z0-9_~.:/-]{1,160}$/;
|
|
1222
|
+
const cmdName = (v) => typeof v === "string" && NAME.test(v) ? v : void 0;
|
|
1223
|
+
/** `arg`: a fixed identifier (a setting's key, a Jam switch, a device, `2x`) - never a value (#4). */
|
|
1224
|
+
const ARG = /^[a-z0-9][a-z0-9-]{0,31}$/;
|
|
1225
|
+
function cleanCmd(r) {
|
|
1226
|
+
const phase = oneOf(r.phase, CMD_PHASES), cid = id(r.id), c = cmdName(r.cmd);
|
|
1227
|
+
if (!phase || !cid || !c) return null;
|
|
1228
|
+
const via = oneOf(r.via, CMD_VIA), where = oneOf(r.in, CMD_IN);
|
|
1229
|
+
if (phase !== "end" && (!via || !where)) return null;
|
|
1230
|
+
const input = phase === "end" ? {} : {
|
|
1231
|
+
via,
|
|
1232
|
+
in: where,
|
|
1233
|
+
keys: typeof r.keys === "string" && KEYS.test(r.keys) ? r.keys : void 0,
|
|
1234
|
+
arg: typeof r.arg === "string" && ARG.test(r.arg) ? r.arg : void 0,
|
|
1235
|
+
board: typeof r.board === "string" && BOARD.test(r.board) ? r.board : void 0,
|
|
1236
|
+
frames: count(r.frames),
|
|
1237
|
+
frame: typeof r.frame === "string" && FRAME.test(r.frame) ? r.frame : void 0
|
|
1238
|
+
};
|
|
1239
|
+
if (phase === "start") return {
|
|
1240
|
+
phase,
|
|
1241
|
+
id: cid,
|
|
1242
|
+
cmd: c,
|
|
1243
|
+
...input
|
|
1244
|
+
};
|
|
1245
|
+
const outcome = oneOf(r.outcome, CMD_OUTCOMES);
|
|
1246
|
+
if (!outcome) return null;
|
|
1247
|
+
const steps = Array.isArray(r.steps) ? r.steps.slice(0, 16).flatMap((x) => {
|
|
1248
|
+
const o = obj(x), n = cmdName(o.name), m = ms(o.ms);
|
|
1249
|
+
return n && m !== void 0 ? [{
|
|
1250
|
+
name: n,
|
|
1251
|
+
ms: m
|
|
1252
|
+
}] : [];
|
|
1253
|
+
}) : [];
|
|
1254
|
+
return {
|
|
1255
|
+
phase,
|
|
1256
|
+
id: cid,
|
|
1257
|
+
cmd: c,
|
|
1258
|
+
...input,
|
|
1259
|
+
outcome,
|
|
1260
|
+
why: cmdName(r.why),
|
|
1261
|
+
ms: ms(r.ms) ?? 0,
|
|
1262
|
+
steps: steps.length ? steps : void 0,
|
|
1263
|
+
late: flag(r.late)
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
/** Each kind's fields, rebuilt from what the page sent - anything else is left out; null = the record is dropped.
|
|
1267
|
+
* `root`, the canvas's own folder, for the one field that names a file (#29). */
|
|
1268
|
+
const BODY = {
|
|
1269
|
+
load: (r) => {
|
|
1270
|
+
const board = typeof r.board === "string" && BOARD.test(r.board) ? r.board : void 0, c = oneOf(r.cause, LOAD_CAUSES), request = id(r.request);
|
|
1271
|
+
return board && c && request ? {
|
|
1272
|
+
board,
|
|
1273
|
+
cause: c,
|
|
1274
|
+
request,
|
|
1275
|
+
sinceNav: ms(r.sinceNav),
|
|
1276
|
+
trigger: oneOf(r.trigger, LOAD_TRIGGERS),
|
|
1277
|
+
writer: oneOf(r.writer, WRITER_ATTRIBUTION),
|
|
1278
|
+
candidates: writers(r.candidates),
|
|
1279
|
+
by: id(r.by)
|
|
1280
|
+
} : null;
|
|
1281
|
+
},
|
|
1282
|
+
commit: (r) => {
|
|
1283
|
+
const k = obj(r.byKind);
|
|
1284
|
+
return {
|
|
1285
|
+
request: id(r.request),
|
|
1286
|
+
ms: ms(r.ms),
|
|
1287
|
+
frames: count(r.frames),
|
|
1288
|
+
byKind: prune({
|
|
1289
|
+
hifi: count(k.hifi),
|
|
1290
|
+
content: count(k.content),
|
|
1291
|
+
html: count(k.html),
|
|
1292
|
+
image: count(k.image)
|
|
1293
|
+
}),
|
|
1294
|
+
measuring: count(r.measuring),
|
|
1295
|
+
savedHeights: count(r.savedHeights),
|
|
1296
|
+
late: flag(r.late)
|
|
1297
|
+
};
|
|
1298
|
+
},
|
|
1299
|
+
visible: (r) => ({
|
|
1300
|
+
nodes: count(r.nodes),
|
|
1301
|
+
ms: ms(r.ms),
|
|
1302
|
+
assumed: flag(r.assumed)
|
|
1303
|
+
}),
|
|
1304
|
+
frame: (r) => {
|
|
1305
|
+
const node = str(r.node), outcome = oneOf(r.outcome, OUTCOMES);
|
|
1306
|
+
return node && outcome ? {
|
|
1307
|
+
node,
|
|
1308
|
+
frame: str(r.frame),
|
|
1309
|
+
gen: str(r.gen, 32),
|
|
1310
|
+
boot: str(r.boot, 32),
|
|
1311
|
+
admitted: ms(r.admitted),
|
|
1312
|
+
ready: ms(r.ready),
|
|
1313
|
+
firstMeasure: ms(r.firstMeasure),
|
|
1314
|
+
settled: ms(r.settled),
|
|
1315
|
+
outcome,
|
|
1316
|
+
kept: flag(r.kept)
|
|
1317
|
+
} : null;
|
|
1318
|
+
},
|
|
1319
|
+
move: (r) => {
|
|
1320
|
+
const node = str(r.node), c = cause(r.cause);
|
|
1321
|
+
return node && c ? {
|
|
1322
|
+
node,
|
|
1323
|
+
dx: delta(r.dx),
|
|
1324
|
+
dy: delta(r.dy),
|
|
1325
|
+
dw: delta(r.dw),
|
|
1326
|
+
dh: delta(r.dh),
|
|
1327
|
+
cause: c,
|
|
1328
|
+
late: flag(r.late)
|
|
1329
|
+
} : null;
|
|
1330
|
+
},
|
|
1331
|
+
quiet: (r) => ms(r.ms) === void 0 ? null : { ms: ms(r.ms) },
|
|
1332
|
+
settled: (r) => {
|
|
1333
|
+
if (ms(r.ms) === void 0) return null;
|
|
1334
|
+
const w = obj(r.weight);
|
|
1335
|
+
return {
|
|
1336
|
+
ms: ms(r.ms),
|
|
1337
|
+
moves: count(r.moves),
|
|
1338
|
+
distance: count(r.distance),
|
|
1339
|
+
lastMove: ms(r.lastMove),
|
|
1340
|
+
causes: Object.fromEntries(Object.entries(obj(r.causes)).slice(0, 32).flatMap(([k, n]) => cause(k) && count(n) !== void 0 ? [[k, n]] : [])),
|
|
1341
|
+
visibleReadyMs: ms(r.visibleReadyMs),
|
|
1342
|
+
allReadyMs: ms(r.allReadyMs),
|
|
1343
|
+
missing: count(r.missing),
|
|
1344
|
+
neverReady: count(r.neverReady),
|
|
1345
|
+
errors: count(r.errors),
|
|
1346
|
+
weight: prune({
|
|
1347
|
+
domNodes: count(w.domNodes),
|
|
1348
|
+
iframes: count(w.iframes),
|
|
1349
|
+
awake: count(w.awake),
|
|
1350
|
+
asleep: count(w.asleep),
|
|
1351
|
+
heapMB: tenth(w.heapMB)
|
|
1352
|
+
})
|
|
1353
|
+
};
|
|
1354
|
+
},
|
|
1355
|
+
cut: (r) => {
|
|
1356
|
+
const reason = oneOf(r.reason, CUT_REASONS);
|
|
1357
|
+
return reason ? {
|
|
1358
|
+
reason,
|
|
1359
|
+
request: id(r.request),
|
|
1360
|
+
ms: ms(r.ms),
|
|
1361
|
+
lastMove: ms(r.lastMove),
|
|
1362
|
+
frames: count(r.frames),
|
|
1363
|
+
ready: count(r.ready),
|
|
1364
|
+
visibleReadyMs: ms(r.visibleReadyMs),
|
|
1365
|
+
allReadyMs: ms(r.allReadyMs),
|
|
1366
|
+
visibleReady: count(r.visibleReady)
|
|
1367
|
+
} : null;
|
|
1368
|
+
},
|
|
1369
|
+
gesture: (r) => {
|
|
1370
|
+
const type = oneOf(r.type, GESTURES);
|
|
1371
|
+
return type ? {
|
|
1372
|
+
type,
|
|
1373
|
+
ms: ms(r.ms),
|
|
1374
|
+
frames: count(r.frames),
|
|
1375
|
+
p50: tenth(r.p50),
|
|
1376
|
+
p95: tenth(r.p95),
|
|
1377
|
+
max: tenth(r.max),
|
|
1378
|
+
over17ms: count(r.over17ms),
|
|
1379
|
+
over33ms: count(r.over33ms),
|
|
1380
|
+
longFrames: count(r.longFrames),
|
|
1381
|
+
hz: hz(r.hz),
|
|
1382
|
+
scale: scaleOf(r.scale),
|
|
1383
|
+
visible: count(r.visible),
|
|
1384
|
+
awake: count(r.awake),
|
|
1385
|
+
loaf: loafs(r.loaf),
|
|
1386
|
+
by: id(r.by)
|
|
1387
|
+
} : null;
|
|
1388
|
+
},
|
|
1389
|
+
error: (r, root) => {
|
|
1390
|
+
const source = oneOf(r.source, ERROR_SOURCES), message = str(r.message);
|
|
1391
|
+
const stack = Array.isArray(r.stack) ? r.stack.slice(0, 3).map((l) => str(l)).filter((l) => !!l) : [];
|
|
1392
|
+
if (!source || !message) return null;
|
|
1393
|
+
const modes = Array.isArray(r.modes) ? [...new Set(r.modes.flatMap((m) => oneOf(m, MODES) ?? []))] : [];
|
|
1394
|
+
return {
|
|
1395
|
+
source,
|
|
1396
|
+
frame: str(r.frame),
|
|
1397
|
+
component: str(r.component),
|
|
1398
|
+
message,
|
|
1399
|
+
stack: stack.length ? stack : void 0,
|
|
1400
|
+
late: flag(r.late),
|
|
1401
|
+
route: fixed(r.route, ROUTE_RE),
|
|
1402
|
+
method: fixed(r.method, METHOD_RE),
|
|
1403
|
+
status: r.status === "timeout" ? "timeout" : status(r.status),
|
|
1404
|
+
ms: ms(r.ms),
|
|
1405
|
+
tag: oneOf(r.tag, RESOURCE_TAGS),
|
|
1406
|
+
url: str(r.url)?.split(/[?#]/)[0],
|
|
1407
|
+
file: repoFile(str(r.file)?.split(/[?#]/)[0], root),
|
|
1408
|
+
plugin: str(r.plugin, 64),
|
|
1409
|
+
modes: modes.length ? modes : void 0
|
|
1410
|
+
};
|
|
1411
|
+
},
|
|
1412
|
+
dropped: (r) => count(r.n) === void 0 ? null : {
|
|
1413
|
+
n: count(r.n),
|
|
1414
|
+
reason: oneOf(r.reason, DROP_REASONS)
|
|
1415
|
+
},
|
|
1416
|
+
renders: (r) => {
|
|
1417
|
+
const rows = Array.isArray(r.rows) ? r.rows.slice(0, RENDERS.rows + 1).flatMap((x) => {
|
|
1418
|
+
const o = obj(x), who = str(o.who, 64);
|
|
1419
|
+
return who ? [prune({
|
|
1420
|
+
who,
|
|
1421
|
+
frame: str(o.frame),
|
|
1422
|
+
commits: count(o.commits),
|
|
1423
|
+
ms: tenth(o.ms),
|
|
1424
|
+
maxMs: tenth(o.maxMs),
|
|
1425
|
+
inGesture: count(o.inGesture),
|
|
1426
|
+
rows: count(o.rows)
|
|
1427
|
+
})] : [];
|
|
1428
|
+
}) : [];
|
|
1429
|
+
return rows.length && epoch(r.minute) !== void 0 ? {
|
|
1430
|
+
minute: epoch(r.minute),
|
|
1431
|
+
rows
|
|
1432
|
+
} : null;
|
|
1433
|
+
},
|
|
1434
|
+
warnings: (r) => {
|
|
1435
|
+
const top = Array.isArray(r.top) ? r.top.slice(0, WARNINGS.top).flatMap((x) => {
|
|
1436
|
+
const o = obj(x), node = str(o.node, 64), n = count(o.n);
|
|
1437
|
+
return node && n !== void 0 ? [{
|
|
1438
|
+
node,
|
|
1439
|
+
n
|
|
1440
|
+
}] : [];
|
|
1441
|
+
}) : [];
|
|
1442
|
+
return count(r.n) === void 0 ? null : {
|
|
1443
|
+
n: count(r.n),
|
|
1444
|
+
top
|
|
1445
|
+
};
|
|
1446
|
+
},
|
|
1447
|
+
theme: (r) => {
|
|
1448
|
+
const t = id(r.t), scope = oneOf(r.scope, THEME_SCOPES), to = fixed(r.to, THEME_RE), outcome = oneOf(r.outcome, TRANSITION_OUTCOMES);
|
|
1449
|
+
return t && scope && to && outcome ? {
|
|
1450
|
+
t,
|
|
1451
|
+
scope,
|
|
1452
|
+
to,
|
|
1453
|
+
by: id(r.by),
|
|
1454
|
+
frames: count(r.frames),
|
|
1455
|
+
applied: count(r.applied),
|
|
1456
|
+
p50Ms: ms(r.p50Ms),
|
|
1457
|
+
maxMs: ms(r.maxMs),
|
|
1458
|
+
late: count(r.late),
|
|
1459
|
+
outcome
|
|
1460
|
+
} : null;
|
|
1461
|
+
},
|
|
1462
|
+
mode: (r) => {
|
|
1463
|
+
const mode = oneOf(r.mode, MODES), phase = oneOf(r.phase, ["enter", "exit"]), t = id(r.t);
|
|
1464
|
+
return mode && phase && t ? {
|
|
1465
|
+
mode,
|
|
1466
|
+
phase,
|
|
1467
|
+
t,
|
|
1468
|
+
by: id(r.by),
|
|
1469
|
+
shownMs: ms(r.shownMs),
|
|
1470
|
+
firstFrameMs: ms(r.firstFrameMs),
|
|
1471
|
+
frames: count(r.frames),
|
|
1472
|
+
outcome: oneOf(r.outcome, TRANSITION_OUTCOMES),
|
|
1473
|
+
heldMs: ms(r.heldMs),
|
|
1474
|
+
errors: count(r.errors)
|
|
1475
|
+
} : null;
|
|
1476
|
+
},
|
|
1477
|
+
hmr: (r) => {
|
|
1478
|
+
const phase = oneOf(r.phase, ["lost", "back"]);
|
|
1479
|
+
return phase ? {
|
|
1480
|
+
phase,
|
|
1481
|
+
downMs: ms(r.downMs)
|
|
1482
|
+
} : null;
|
|
1483
|
+
},
|
|
1484
|
+
cmd: (r) => cleanCmd(r),
|
|
1485
|
+
self: (r) => {
|
|
1486
|
+
const cpuMs = tenth(r.cpuMs), hooks = obj(r.hookMs);
|
|
1487
|
+
if (cpuMs === void 0) return null;
|
|
1488
|
+
return {
|
|
1489
|
+
cpuMs,
|
|
1490
|
+
hookMs: prune(Object.fromEntries(SELF_HOOKS.map((k) => [k, tenth(hooks[k])]))),
|
|
1491
|
+
records: count(r.records),
|
|
1492
|
+
bytes: count(r.bytes),
|
|
1493
|
+
dropped: count(r.dropped)
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
/** A page record rebuilt from the envelope and its kind's allowlist, or null. Strings are cut to 200 characters. */
|
|
1498
|
+
function cleanPageRecord(raw, now = Date.now(), root) {
|
|
1499
|
+
const r = obj(raw);
|
|
1500
|
+
if (r.v !== 1) return null;
|
|
1501
|
+
const kind = oneOf(r.kind, PAGE_KINDS);
|
|
1502
|
+
const ws = typeof r.ws === "string" && WS.test(r.ws) ? r.ws : void 0;
|
|
1503
|
+
const page = id(r.page), load = r.load === "-" ? "-" : id(r.load), seq = count(r.seq);
|
|
1504
|
+
if (!kind || !ws || !page || !load || seq === void 0) return null;
|
|
1505
|
+
const body = BODY[kind](r, root);
|
|
1506
|
+
if (!body) return null;
|
|
1507
|
+
return {
|
|
1508
|
+
v: 1,
|
|
1509
|
+
ws,
|
|
1510
|
+
page,
|
|
1511
|
+
load,
|
|
1512
|
+
seq,
|
|
1513
|
+
ts: typeof r.ts === "number" && Number.isFinite(r.ts) && Math.abs(r.ts - now) < 864e5 ? Math.round(r.ts) : now,
|
|
1514
|
+
kind,
|
|
1515
|
+
...prune(body)
|
|
1516
|
+
};
|
|
1517
|
+
}
|
|
1518
|
+
/** A revision: the dev server's boot, and its send's number (plugin.ts). */
|
|
1519
|
+
const REVISION = /^\d{1,24}:\d{1,9}$/;
|
|
1520
|
+
const revision = (v) => typeof v === "string" && REVISION.test(v) ? v : void 0;
|
|
1521
|
+
const NAV_TYPES = [
|
|
1522
|
+
"navigate",
|
|
1523
|
+
"reload",
|
|
1524
|
+
"back_forward",
|
|
1525
|
+
"prerender"
|
|
1526
|
+
];
|
|
1527
|
+
const WATCHDOG_ACTIONS = ["retry", "gave-up"];
|
|
1528
|
+
/** Vite's timestamp, epoch ms */
|
|
1529
|
+
const hmrTs = (v) => typeof v === "number" && Number.isFinite(v) && v > 0 && v < 0x5af3107a4000 ? Math.round(v) : void 0;
|
|
1530
|
+
const strings = (v, n = RELOAD_BOUNDS.idsPerLine) => Array.isArray(v) ? v.slice(0, n).flatMap((x) => str(x) ?? []) : [];
|
|
1531
|
+
/** A shell line's fields by kind; null = dropped. `dropped` is R13's own: what the reload Outbox lost, counted (#5). */
|
|
1532
|
+
const SHELL_BODY = {
|
|
1533
|
+
"shell-boot": (r) => ({
|
|
1534
|
+
nav: oneOf(r.nav, NAV_TYPES),
|
|
1535
|
+
resumed: flag(r.resumed),
|
|
1536
|
+
prev: typeof r.prev === "string" && ID.test(r.prev) ? r.prev : void 0
|
|
1537
|
+
}),
|
|
1538
|
+
ready: (r) => {
|
|
1539
|
+
const node = str(r.node);
|
|
1540
|
+
if (!node) return null;
|
|
1541
|
+
const revisions = Array.isArray(r.revisions) ? r.revisions.slice(0, RELOAD_BOUNDS.idsPerLine).flatMap((x) => revision(x) ?? []) : void 0;
|
|
1542
|
+
return {
|
|
1543
|
+
cause: cause(r.cause),
|
|
1544
|
+
node,
|
|
1545
|
+
frame: str(r.frame),
|
|
1546
|
+
shown: str(r.shown),
|
|
1547
|
+
ms: ms(r.ms),
|
|
1548
|
+
revisions,
|
|
1549
|
+
queuedMs: ms(r.queuedMs),
|
|
1550
|
+
bootMs: ms(r.bootMs),
|
|
1551
|
+
heldBy: oneOf(r.heldBy, HELD_BY),
|
|
1552
|
+
asleep: flag(r.asleep)
|
|
1553
|
+
};
|
|
1554
|
+
},
|
|
1555
|
+
settled: (r) => {
|
|
1556
|
+
const node = str(r.node);
|
|
1557
|
+
return node ? {
|
|
1558
|
+
node,
|
|
1559
|
+
frame: str(r.frame),
|
|
1560
|
+
ms: ms(r.ms)
|
|
1561
|
+
} : null;
|
|
1562
|
+
},
|
|
1563
|
+
invalidated: (r) => {
|
|
1564
|
+
const rev = revision(r.revision), h = obj(r.held);
|
|
1565
|
+
return rev ? {
|
|
1566
|
+
revision: rev,
|
|
1567
|
+
asked: count(r.asked),
|
|
1568
|
+
offBoard: count(r.offBoard),
|
|
1569
|
+
onBoard: count(r.onBoard),
|
|
1570
|
+
awake: count(r.awake),
|
|
1571
|
+
asleep: count(r.asleep),
|
|
1572
|
+
held: prune(Object.fromEntries(HELD_BY.map((k) => [k, count(h[k])]))),
|
|
1573
|
+
parts: count(r.parts)
|
|
1574
|
+
} : null;
|
|
1575
|
+
},
|
|
1576
|
+
"invalidated-nodes": (r) => {
|
|
1577
|
+
const rev = revision(r.revision);
|
|
1578
|
+
return rev ? {
|
|
1579
|
+
revision: rev,
|
|
1580
|
+
part: count(r.part),
|
|
1581
|
+
parts: count(r.parts),
|
|
1582
|
+
nodes: strings(r.nodes)
|
|
1583
|
+
} : null;
|
|
1584
|
+
},
|
|
1585
|
+
inplace: (r) => {
|
|
1586
|
+
const node = str(r.node), ts = hmrTs(r.hmrTs);
|
|
1587
|
+
if (!node || ts === void 0 || r.fetched !== true || r.applied !== "unknown" && r.applied !== false) return null;
|
|
1588
|
+
return {
|
|
1589
|
+
node,
|
|
1590
|
+
frame: str(r.frame),
|
|
1591
|
+
hmrTs: ts,
|
|
1592
|
+
fetched: true,
|
|
1593
|
+
applyMs: ms(r.applyMs),
|
|
1594
|
+
nextObservedCommitMs: ms(r.nextObservedCommitMs),
|
|
1595
|
+
applied: r.applied,
|
|
1596
|
+
noAfterUpdate: flag(r.noAfterUpdate)
|
|
1597
|
+
};
|
|
1598
|
+
},
|
|
1599
|
+
watchdog: (r) => {
|
|
1600
|
+
const node = str(r.node), action = oneOf(r.action, WATCHDOG_ACTIONS);
|
|
1601
|
+
return node && action ? {
|
|
1602
|
+
node,
|
|
1603
|
+
frame: str(r.frame),
|
|
1604
|
+
nav: count(r.nav),
|
|
1605
|
+
action
|
|
1606
|
+
} : null;
|
|
1607
|
+
},
|
|
1608
|
+
dropped: (r) => count(r.n) === void 0 ? null : {
|
|
1609
|
+
n: count(r.n),
|
|
1610
|
+
reason: oneOf(r.reason, DROP_REASONS)
|
|
1611
|
+
}
|
|
1612
|
+
};
|
|
1613
|
+
const SHELL_KINDS = [...SHELL_RELOAD_KINDS, "dropped"];
|
|
1614
|
+
/** A shell line rebuilt from its envelope and its kind's allowlist, or null (#16, as `cleanPageRecord`). */
|
|
1615
|
+
function cleanReloadRecord(raw, now = Date.now()) {
|
|
1616
|
+
const r = obj(raw);
|
|
1617
|
+
const kind = oneOf(r.kind, SHELL_KINDS), page = id(r.page), seq = count(r.seq);
|
|
1618
|
+
if (!kind || !page || seq === void 0) return null;
|
|
1619
|
+
const body = SHELL_BODY[kind](r);
|
|
1620
|
+
if (!body) return null;
|
|
1621
|
+
return {
|
|
1622
|
+
ts: typeof r.ts === "number" && Number.isFinite(r.ts) && Math.abs(r.ts - now) < 864e5 ? Math.round(r.ts) : now,
|
|
1623
|
+
kind,
|
|
1624
|
+
tab: str(r.tab, 16),
|
|
1625
|
+
page,
|
|
1626
|
+
seq,
|
|
1627
|
+
...prune(body)
|
|
1628
|
+
};
|
|
1629
|
+
}
|
|
1630
|
+
/** #7: the `(page, seq)` a server wrote - best effort: 10 minutes, 4,096 per page, 64 pages. One window for both logs,
|
|
1631
|
+
* whose records share one seq per page. Every reader dedupes again: a retry past it, or after an eviction, is written
|
|
1632
|
+
* twice. Oldest first, by Map order. */
|
|
1633
|
+
const written = /* @__PURE__ */ new Map();
|
|
1634
|
+
const wroteLately = (root, page, seq, now) => {
|
|
1635
|
+
const at = written.get(`${root}\0${page}`)?.get(seq);
|
|
1636
|
+
return at !== void 0 && now - at < DEDUPE.windowMs;
|
|
1637
|
+
};
|
|
1638
|
+
function rememberWrites(root, recs, now) {
|
|
1639
|
+
for (const r of recs) {
|
|
1640
|
+
const key = `${root}\0${r.page}`;
|
|
1641
|
+
const seqs = written.get(key) ?? /* @__PURE__ */ new Map();
|
|
1642
|
+
written.delete(key);
|
|
1643
|
+
written.set(key, seqs);
|
|
1644
|
+
seqs.delete(r.seq);
|
|
1645
|
+
seqs.set(r.seq, now);
|
|
1646
|
+
if (seqs.size > DEDUPE.perPage) seqs.delete(seqs.keys().next().value);
|
|
1647
|
+
if (written.size > DEDUPE.pages) written.delete(written.keys().next().value);
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
/** failed appends, by project (#7): each 503 says the count so far */
|
|
1651
|
+
const failures = /* @__PURE__ */ new Map();
|
|
1652
|
+
const failedWrites = (root) => failures.get(root) ?? 0;
|
|
1653
|
+
/** A batch into its log: duplicates dropped, the answer only after the append returned. `kept` receives what was written.
|
|
1654
|
+
* An append that failed is `BatchFailed`, and remembers nothing: the retry writes it. */
|
|
1655
|
+
function writeBatch(root, log, recs, now, kept) {
|
|
1656
|
+
const inBatch = /* @__PURE__ */ new Set();
|
|
1657
|
+
const fresh = recs.filter((r) => {
|
|
1658
|
+
const k = `${r.page}:${r.seq}`;
|
|
1659
|
+
if (inBatch.has(k) || wroteLately(root, r.page, r.seq, now)) return false;
|
|
1660
|
+
inBatch.add(k);
|
|
1661
|
+
return true;
|
|
1662
|
+
});
|
|
1663
|
+
if (fresh.length) {
|
|
1664
|
+
try {
|
|
1665
|
+
perfLogs.append(root, log, fresh.map((r) => JSON.stringify(r)));
|
|
1666
|
+
} catch {
|
|
1667
|
+
const n = failedWrites(root) + 1;
|
|
1668
|
+
failures.set(root, n);
|
|
1669
|
+
return {
|
|
1670
|
+
error: "the perf log could not be written",
|
|
1671
|
+
failedWrites: n
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
rememberWrites(root, fresh, now);
|
|
1675
|
+
kept?.(fresh);
|
|
1676
|
+
}
|
|
1677
|
+
return {
|
|
1678
|
+
ok: true,
|
|
1679
|
+
accepted: fresh.length,
|
|
1680
|
+
duplicates: recs.length - fresh.length
|
|
1681
|
+
};
|
|
1682
|
+
}
|
|
1683
|
+
/** `POST perf/reload`'s batch (#16). */
|
|
1684
|
+
function takeReloadBatch(root, raws, now = Date.now()) {
|
|
1685
|
+
return writeBatch(root, "reloads", raws.slice(0, 5e3).flatMap((x) => {
|
|
1686
|
+
const c = cleanReloadRecord(x, now);
|
|
1687
|
+
return c ? [c] : [];
|
|
1688
|
+
}), now);
|
|
1689
|
+
}
|
|
1690
|
+
/** `POST perf/page`'s batch (#7): as `logPage`, a headless run's records stamped after the cleaner. */
|
|
1691
|
+
function takePageBatch(root, raws, o = {}, now = Date.now()) {
|
|
1692
|
+
return writeBatch(root, "page", raws.slice(0, 5e3).flatMap((x) => {
|
|
1693
|
+
const c = cleanPageRecord(x, now, root);
|
|
1694
|
+
return c ? [o.run ? {
|
|
1695
|
+
...c,
|
|
1696
|
+
run: o.run
|
|
1697
|
+
} : c] : [];
|
|
1698
|
+
}), now, o.kept);
|
|
1699
|
+
}
|
|
1700
|
+
/** One run line into runs.jsonl (spec 28 A3.8 #22) - best effort. A cancelled run writes its line too. */
|
|
1701
|
+
function appendRun(root, line) {
|
|
1702
|
+
try {
|
|
1703
|
+
perfLogs.append(root, "runs", [JSON.stringify(line)]);
|
|
1704
|
+
return true;
|
|
1705
|
+
} catch {
|
|
1706
|
+
return false;
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
/** The run lines from `since` (epoch ms): malformed lines skipped. */
|
|
1710
|
+
const readRuns = (root, since = 0) => perfLogs.read(root, "runs", since).filter((l) => typeof l.run === "string");
|
|
1711
|
+
/**
|
|
1712
|
+
* At start (spec 28 A1.7, spec 30 R9): design/.local/perf/ is the owner's alone - the directory 700 and every file in it
|
|
1713
|
+
* 600, those earlier versions created without modes included. Then each log's upkeep, as the writer does it hourly: a
|
|
1714
|
+
* live file opened 24 h ago or past its size closed - a 0.29 file is closed as a segment, `openedAt` its first line's
|
|
1715
|
+
* `ts` and `bound` its mtime + 24 h (#45) - segments past 14 days unlinked, the safety cap. Nothing is rewritten.
|
|
1716
|
+
* jam.jsonl's upkeep is queued in its own writer (perf-jam.ts). Synchronous, before the server listens. Best effort; a
|
|
1717
|
+
* link is never followed.
|
|
1718
|
+
*/
|
|
1719
|
+
function tidyPerfDir(root, now = clock()) {
|
|
1720
|
+
try {
|
|
1721
|
+
if (unsafe(root)) return;
|
|
1722
|
+
const dir = perfDir(root);
|
|
1723
|
+
let st;
|
|
1724
|
+
try {
|
|
1725
|
+
st = lstatSync(dir);
|
|
1726
|
+
} catch {
|
|
1727
|
+
return;
|
|
1728
|
+
}
|
|
1729
|
+
if (!st.isDirectory()) return;
|
|
1730
|
+
chmodSync(dir, 448);
|
|
1731
|
+
for (const name of readdirSync(dir)) {
|
|
1732
|
+
const f = join(dir, name);
|
|
1733
|
+
try {
|
|
1734
|
+
if (lstatSync(f).isFile()) chmodSync(f, 384);
|
|
1735
|
+
} catch {}
|
|
1736
|
+
}
|
|
1737
|
+
prunedAt.set(dir, now);
|
|
1738
|
+
for (const log of LOGS) upkeep(root, log, now, true);
|
|
1739
|
+
tidyJam(root, now);
|
|
1740
|
+
} catch {}
|
|
1741
|
+
}
|
|
1742
|
+
//#endregion
|
|
1743
|
+
export { WRITE_MATCH_MS as C, WRITER_HEADERS as S, ID12 as _, readPage as a, RELOAD_BOUNDS as b, takePageBatch as c, logJam as d, perf_jam_exports as f, parseAppLine as g, APP_LOG as h, perfDir as i, takeReloadBatch as l, appLogFiles as m, appendRun as n, readReloads as o, readJam as p, logReloadLines as r, readRuns as s, PAGE_BODY_MAX as t, tidyPerfDir as u, RECORDER_LEVELS as v, decodeCmds as w, UNKNOWN_CHANGE as x, RECORDER_PARAM as y };
|