@marver-design/marver 0.27.0 → 0.27.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +94 -0
  2. package/README.md +5 -2
  3. package/dist/{anchor-check-ChdDqL5z.mjs → anchor-check-gkxkPAIv.mjs} +1 -1
  4. package/dist/{board-tree-DRioF94d.mjs → board-tree-BXR8dw4b.mjs} +3 -1
  5. package/dist/{boards-CGg6piKF.mjs → boards-BHKP5yhI.mjs} +1 -1
  6. package/dist/{boards-DlTcpF47.mjs → boards-BqUqKNW8.mjs} +4 -4
  7. package/dist/{build-DZnFysGU.mjs → build-VgNg4rr8.mjs} +7 -22
  8. package/dist/cli.mjs +32 -18
  9. package/dist/{collab-Dxg2trOK.mjs → collab-BEAcXrBb.mjs} +1 -1
  10. package/dist/{comments-DxOkCSNE.mjs → comments-YEU1Zhv9.mjs} +4 -4
  11. package/dist/{context-HQ4Giw58.mjs → context-Ccjrhr1J.mjs} +3 -3
  12. package/dist/{daemon-BSA0lV8d.mjs → daemon-Dc9AH0M3.mjs} +4 -4
  13. package/dist/{dev-eQJhWsqg.mjs → dev-ri6nAlRp.mjs} +11 -11
  14. package/dist/{init-COD0x48i.mjs → init-W0nxR1OY.mjs} +3 -3
  15. package/dist/{link-DDXZVGtv.mjs → link-CdTgn94Y.mjs} +4 -4
  16. package/dist/{ls-D7O1G505.mjs → ls-BZ5TAtd5.mjs} +1 -1
  17. package/dist/{manifest-Nl3KSVOk.mjs → manifest-aFyDmL8C.mjs} +2 -2
  18. package/dist/{marks-CWM3WJjc.mjs → marks-BF9pNEgH.mjs} +2 -2
  19. package/dist/{marks-CKgzucHZ.mjs → marks-CnCCfAbD.mjs} +3 -3
  20. package/dist/{marver-id-gate-BjF6v4bT.mjs → marver-id-gate-D-R0hSt_.mjs} +1 -1
  21. package/dist/perf-DkAUFT6W.mjs +1319 -0
  22. package/dist/perf-jam-BaH3Lg0m.mjs +635 -0
  23. package/dist/{plugin-C-E7omsh.mjs → plugin-B8UghYuF.mjs} +2592 -112
  24. package/dist/{poster-Bx83ZYVK.mjs → poster-BNgafWMK.mjs} +1 -1
  25. package/dist/{profile-Bh5Y5zpW.mjs → profile-jIS6BcAs.mjs} +1 -1
  26. package/dist/{publish-bakes-aljgm45S.mjs → publish-bakes-38Nja78p.mjs} +3 -3
  27. package/dist/{recover-t9Pno3u9.mjs → recover-B8AiI29B.mjs} +2 -2
  28. package/dist/{serve-C8rVheYk.mjs → serve-Dp3nHstu.mjs} +3 -3
  29. package/dist/{share-W5H3BNvP.mjs → share-Ce4tP7ji.mjs} +1 -1
  30. package/dist/{shot-DoHLl6ig.mjs → shot-CMQgMVZm.mjs} +1 -1
  31. package/dist/{shot-LFhC-WYD.mjs → shot-Ch24tiMO.mjs} +30 -5
  32. package/dist/{slots-DCDo3N0x.mjs → slots-CFNnegCc.mjs} +8 -1
  33. package/dist/{sync-BQVlHZpx.mjs → sync-CQdGARJ9.mjs} +1 -1
  34. package/dist/{work-BroFwnup.mjs → work-Bxz-ObF3.mjs} +3 -3
  35. package/dist/{work-Bp1p39kq.mjs → work-CNhFF9pU.mjs} +1 -1
  36. package/docs/jam-session.md +2 -1
  37. package/docs/performance.md +197 -35
  38. package/package.json +1 -1
  39. package/src/client/content/video.tsx +10 -2
  40. package/src/client/shell/App.tsx +10 -3
  41. package/src/client/shell/Comments.tsx +5 -3
  42. package/src/client/shell/ContextMenu.tsx +5 -3
  43. package/src/client/shell/LockedApp.tsx +3 -0
  44. package/src/client/shell/Play.tsx +4 -2
  45. package/src/client/shell/Tip.tsx +3 -1
  46. package/src/client/shell/api.ts +47 -20
  47. package/src/client/shell/app-bridge.ts +254 -0
  48. package/src/client/shell/app-perf.ts +92 -0
  49. package/src/client/shell/canvas/Canvas.tsx +115 -22
  50. package/src/client/shell/canvas/FrameNode.tsx +10 -6
  51. package/src/client/shell/canvas/admission.ts +23 -8
  52. package/src/client/shell/canvas/ctl.ts +6 -0
  53. package/src/client/shell/canvas/sleep.ts +57 -17
  54. package/src/client/shell/canvas/updates.ts +2 -1
  55. package/src/client/shell/inset.ts +44 -0
  56. package/src/client/shell/jam-capture.ts +19 -14
  57. package/src/client/shell/jam-keys.ts +8 -3
  58. package/src/client/shell/jam-panel.tsx +6 -3
  59. package/src/client/shell/jam-voice.ts +48 -10
  60. package/src/client/shell/main.tsx +14 -1
  61. package/src/client/shell/marks-ui.tsx +5 -3
  62. package/src/client/shell/marver-app.ts +21 -0
  63. package/src/client/shell/perf-run-probe.ts +34 -0
  64. package/src/client/shell/perf-run.ts +70 -0
  65. package/src/client/shell/perf.ts +11 -0
  66. package/src/client/shell/recorder.ts +45 -3
  67. package/src/client/shell/store.ts +52 -24
  68. package/src/client/shell/styles.css +47 -15
  69. package/src/client/shell/unsaved.ts +81 -0
  70. package/src/client/shell/whiteout-probes.ts +141 -0
  71. package/src/shared/app-api.ts +205 -0
  72. package/src/shared/app-perf.ts +308 -0
  73. package/src/shared/logo-sniff.ts +86 -0
  74. package/src/shared/machine.ts +132 -0
  75. package/src/shared/perf-run.ts +177 -0
  76. package/templates/AGENTS-embedded.md +3 -0
  77. package/templates/AGENTS-studio.md +3 -0
  78. package/templates/instructions/boards.md +3 -1
  79. package/dist/bake-DM_uWgAA.mjs +0 -786
  80. package/dist/perf-BWzQwZqj.mjs +0 -369
  81. package/dist/perf-BoDSkbEb.mjs +0 -429
  82. package/dist/perf-jam-D55U3FZK.mjs +0 -242
@@ -0,0 +1,635 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-8H4AJuhK.mjs";
2
+ import { a as checkRealDirs } from "./boards-BHKP5yhI.mjs";
3
+ import { appendFileSync, chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from "node:fs";
4
+ import { basename, join } from "node:path";
5
+ import { appendFile, readFile, stat, writeFile } from "node:fs/promises";
6
+ //#region src/server/perf.ts
7
+ /**
8
+ * The perf logs, one JSON line per event in design/.local/perf/ - local, never committed, the owner's alone (the
9
+ * directory 700, every file in it 600). `npx marver perf` reads them back.
10
+ * - reloads.jsonl, the reload log (spec 26 Part 1): the server's view (an edit and the frames it reached, a full
11
+ * reload sent or dropped) and the page's (each document that actually booted, and how long it took);
12
+ * - page.jsonl, the recorder's page half (spec 28 A1.7): what each board load did, from the page - its request and
13
+ * commit, each frame's outcome, every move and why, quiet, the last move and settled - plus gestures and
14
+ * errors. Each record is rebuilt from its kind's allowlist; `npx marver perf --loads` reads them back.
15
+ */
16
+ var perf_exports = /* @__PURE__ */ __exportAll({
17
+ CUT_REASONS: () => CUT_REASONS,
18
+ DROP_REASONS: () => DROP_REASONS,
19
+ GESTURES: () => GESTURES,
20
+ LOAD_CAUSES: () => LOAD_CAUSES,
21
+ OUTCOMES: () => OUTCOMES,
22
+ PAGE_BODY_MAX: () => PAGE_BODY_MAX,
23
+ PAGE_KINDS: () => PAGE_KINDS,
24
+ SOURCES: () => SOURCES,
25
+ appendRun: () => appendRun,
26
+ cleanPageRecord: () => cleanPageRecord,
27
+ logPage: () => logPage,
28
+ logReload: () => logReload,
29
+ pageLog: () => pageLog,
30
+ perfDir: () => perfDir,
31
+ readPage: () => readPage,
32
+ readReloads: () => readReloads,
33
+ readRuns: () => readRuns,
34
+ reloadLog: () => reloadLog,
35
+ runsLog: () => runsLog,
36
+ tidyPerfDir: () => tidyPerfDir
37
+ });
38
+ const MAX = 2e6;
39
+ /** page.jsonl's: one load of a 120-frame board writes ~140 KB (two thirds of it moves), so 2 MB held about fourteen
40
+ * such loads - hours, not the 14 days the lines may stay. 8 MB holds four times as many. */
41
+ const PAGE_MAX = 8e6;
42
+ const maxOf = (file) => basename(file) === "page.jsonl" ? PAGE_MAX : MAX;
43
+ const KEEP_MS = 12096e5;
44
+ /** The logs whose lines age out and whose size is held at start (spec 28 A1.7, A1.13): every one with a `ts` per line. */
45
+ const AGED = [
46
+ "reloads.jsonl",
47
+ "page.jsonl",
48
+ "runs.jsonl",
49
+ "jam.jsonl"
50
+ ];
51
+ const perfDir = (root) => join(root, "design", ".local", "perf");
52
+ const reloadLog = (root) => join(perfDir(root), "reloads.jsonl");
53
+ const pageLog = (root) => join(perfDir(root), "page.jsonl");
54
+ /** runs.jsonl, the headless runs' lines (spec 28 A3.4, A3.8 #22): 2 MB, 14 days, in tidyPerfDir like the others. */
55
+ const runsLog = (root) => join(perfDir(root), "runs.jsonl");
56
+ /** design/, design/.local and its perf/ must be real directories inside the project: a log is never written through a link. */
57
+ const unsafe = (root) => checkRealDirs(root, [
58
+ [join(root, "design"), "design"],
59
+ [join(root, "design", ".local"), "design/.local"],
60
+ [perfDir(root), "design/.local/perf"]
61
+ ]);
62
+ /** One append to a perf log: the directory 700 and the file 600 from their creation, never through a link, the
63
+ * oldest half gone past its cap (maxOf). Throws - callers swallow. */
64
+ function appendLog(root, file, text) {
65
+ const why = unsafe(root);
66
+ if (why) throw new Error(why);
67
+ mkdirSync(join(root, "design", ".local"), { recursive: true });
68
+ mkdirSync(perfDir(root), {
69
+ recursive: true,
70
+ mode: 448
71
+ });
72
+ if (existsSync(file)) {
73
+ if (!lstatSync(file).isFile()) throw new Error("not a regular file");
74
+ if (statSync(file).size > maxOf(file)) {
75
+ const lines = readFileSync(file, "utf8").split("\n");
76
+ writeFileSync(file, lines.slice(Math.floor(lines.length / 2)).join("\n"));
77
+ }
78
+ }
79
+ appendFileSync(file, text, { mode: 384 });
80
+ }
81
+ function logReload(root, entry) {
82
+ try {
83
+ appendLog(root, reloadLog(root), JSON.stringify({
84
+ ts: Date.now(),
85
+ ...entry
86
+ }) + "\n");
87
+ } catch {}
88
+ }
89
+ const readLines = (file, since) => {
90
+ try {
91
+ return readFileSync(file, "utf8").split("\n").flatMap((l) => {
92
+ try {
93
+ const e = JSON.parse(l);
94
+ return typeof e?.ts === "number" && e.ts >= since ? [e] : [];
95
+ } catch {
96
+ return [];
97
+ }
98
+ });
99
+ } catch {
100
+ return [];
101
+ }
102
+ };
103
+ const readReloads = (root, since = 0) => readLines(reloadLog(root), since);
104
+ /** The page's records from `since` (epoch ms): malformed lines skipped. */
105
+ const readPage = (root, since = 0) => readLines(pageLog(root), since);
106
+ /** What one request may carry. */
107
+ const PAGE_BODY_MAX = 524288;
108
+ const PAGE_KINDS = [
109
+ "load",
110
+ "commit",
111
+ "visible",
112
+ "frame",
113
+ "move",
114
+ "quiet",
115
+ "settled",
116
+ "cut",
117
+ "gesture",
118
+ "error",
119
+ "dropped"
120
+ ];
121
+ const LOAD_CAUSES = [
122
+ "boot",
123
+ "switch",
124
+ "disk-reload"
125
+ ];
126
+ const CUT_REASONS = [
127
+ "switch",
128
+ "reload",
129
+ "hidden",
130
+ "timeout",
131
+ "superseded",
132
+ "error",
133
+ "edited",
134
+ "unsaved"
135
+ ];
136
+ const OUTCOMES = [
137
+ "settled",
138
+ "ready",
139
+ "missing",
140
+ "error",
141
+ "never-ready",
142
+ "cut"
143
+ ];
144
+ const GESTURES = [
145
+ "pan",
146
+ "zoom",
147
+ "layout"
148
+ ];
149
+ const SOURCES = [
150
+ "shell",
151
+ "frame",
152
+ "stage"
153
+ ];
154
+ /** why records were dropped, when it is not the queue's bound or a failed send: errors past a load's 50 */
155
+ const DROP_REASONS = ["errors"];
156
+ const ID = /^[0-9a-f]{12}$/;
157
+ const WS = /^[0-9a-f]{16}$/;
158
+ const CAUSE = /^[a-z][a-z0-9:+-]{0,63}$/;
159
+ const BOARD = /^(?:~unplaced|[a-z0-9][a-z0-9-]{0,63})$/;
160
+ const str = (v, n = 200) => typeof v === "string" && v ? v.slice(0, n) : void 0;
161
+ const id = (v) => typeof v === "string" && ID.test(v) ? v : void 0;
162
+ const count$1 = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e7 ? v : void 0;
163
+ const ms$1 = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v < 1e9 ? Math.round(v) : void 0;
164
+ const tenth = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v < 1e7 ? Math.round(v * 10) / 10 : void 0;
165
+ /** the display's refresh rate the Mac app told the shell (spec 28 A3.8 #9): 1-1000 Hz */
166
+ const hz = (v) => typeof v === "number" && Number.isFinite(v) && v >= 1 && v <= 1e3 ? Math.round(v) : void 0;
167
+ const delta = (v) => typeof v === "number" && Number.isFinite(v) && Math.abs(v) < 1e7 ? Math.round(v) : void 0;
168
+ const flag = (v) => v === true ? true : void 0;
169
+ const oneOf$1 = (v, set) => typeof v === "string" && set.includes(v) ? v : void 0;
170
+ const obj = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : {};
171
+ const prune$1 = (o) => Object.fromEntries(Object.entries(o).filter(([, x]) => x !== void 0));
172
+ const cause = (v) => typeof v === "string" && CAUSE.test(v) ? v : void 0;
173
+ /** Each kind's fields, rebuilt from what the page sent - anything else is left out; null = the record is dropped. */
174
+ const BODY = {
175
+ load: (r) => {
176
+ const board = typeof r.board === "string" && BOARD.test(r.board) ? r.board : void 0, c = oneOf$1(r.cause, LOAD_CAUSES), request = id(r.request);
177
+ return board && c && request ? {
178
+ board,
179
+ cause: c,
180
+ request,
181
+ sinceNav: ms$1(r.sinceNav)
182
+ } : null;
183
+ },
184
+ commit: (r) => {
185
+ const k = obj(r.byKind);
186
+ return {
187
+ request: id(r.request),
188
+ ms: ms$1(r.ms),
189
+ frames: count$1(r.frames),
190
+ byKind: prune$1({
191
+ hifi: count$1(k.hifi),
192
+ content: count$1(k.content),
193
+ html: count$1(k.html),
194
+ image: count$1(k.image)
195
+ }),
196
+ measuring: count$1(r.measuring),
197
+ savedHeights: count$1(r.savedHeights),
198
+ late: flag(r.late)
199
+ };
200
+ },
201
+ visible: (r) => ({
202
+ nodes: count$1(r.nodes),
203
+ ms: ms$1(r.ms),
204
+ assumed: flag(r.assumed)
205
+ }),
206
+ frame: (r) => {
207
+ const node = str(r.node), outcome = oneOf$1(r.outcome, OUTCOMES);
208
+ return node && outcome ? {
209
+ node,
210
+ frame: str(r.frame),
211
+ gen: str(r.gen, 32),
212
+ boot: str(r.boot, 32),
213
+ admitted: ms$1(r.admitted),
214
+ ready: ms$1(r.ready),
215
+ firstMeasure: ms$1(r.firstMeasure),
216
+ settled: ms$1(r.settled),
217
+ outcome,
218
+ kept: flag(r.kept)
219
+ } : null;
220
+ },
221
+ move: (r) => {
222
+ const node = str(r.node), c = cause(r.cause);
223
+ return node && c ? {
224
+ node,
225
+ dx: delta(r.dx),
226
+ dy: delta(r.dy),
227
+ dw: delta(r.dw),
228
+ dh: delta(r.dh),
229
+ cause: c,
230
+ late: flag(r.late)
231
+ } : null;
232
+ },
233
+ quiet: (r) => ms$1(r.ms) === void 0 ? null : { ms: ms$1(r.ms) },
234
+ settled: (r) => {
235
+ if (ms$1(r.ms) === void 0) return null;
236
+ const w = obj(r.weight);
237
+ return {
238
+ ms: ms$1(r.ms),
239
+ moves: count$1(r.moves),
240
+ distance: count$1(r.distance),
241
+ lastMove: ms$1(r.lastMove),
242
+ causes: Object.fromEntries(Object.entries(obj(r.causes)).slice(0, 32).flatMap(([k, n]) => cause(k) && count$1(n) !== void 0 ? [[k, n]] : [])),
243
+ visibleReadyMs: ms$1(r.visibleReadyMs),
244
+ allReadyMs: ms$1(r.allReadyMs),
245
+ missing: count$1(r.missing),
246
+ neverReady: count$1(r.neverReady),
247
+ errors: count$1(r.errors),
248
+ weight: prune$1({
249
+ domNodes: count$1(w.domNodes),
250
+ iframes: count$1(w.iframes),
251
+ awake: count$1(w.awake),
252
+ asleep: count$1(w.asleep),
253
+ heapMB: tenth(w.heapMB)
254
+ })
255
+ };
256
+ },
257
+ cut: (r) => {
258
+ const reason = oneOf$1(r.reason, CUT_REASONS);
259
+ return reason ? {
260
+ reason,
261
+ request: id(r.request),
262
+ ms: ms$1(r.ms),
263
+ lastMove: ms$1(r.lastMove),
264
+ frames: count$1(r.frames),
265
+ ready: count$1(r.ready)
266
+ } : null;
267
+ },
268
+ gesture: (r) => {
269
+ const type = oneOf$1(r.type, GESTURES);
270
+ return type ? {
271
+ type,
272
+ ms: ms$1(r.ms),
273
+ frames: count$1(r.frames),
274
+ p50: tenth(r.p50),
275
+ p95: tenth(r.p95),
276
+ max: tenth(r.max),
277
+ over17ms: count$1(r.over17ms),
278
+ over33ms: count$1(r.over33ms),
279
+ longFrames: count$1(r.longFrames),
280
+ hz: hz(r.hz)
281
+ } : null;
282
+ },
283
+ error: (r) => {
284
+ const source = oneOf$1(r.source, SOURCES), message = str(r.message);
285
+ const stack = Array.isArray(r.stack) ? r.stack.slice(0, 3).map((l) => str(l)).filter((l) => !!l) : [];
286
+ return source && message ? {
287
+ source,
288
+ frame: str(r.frame),
289
+ component: str(r.component),
290
+ message,
291
+ stack: stack.length ? stack : void 0,
292
+ late: flag(r.late)
293
+ } : null;
294
+ },
295
+ dropped: (r) => count$1(r.n) === void 0 ? null : {
296
+ n: count$1(r.n),
297
+ reason: oneOf$1(r.reason, DROP_REASONS)
298
+ }
299
+ };
300
+ /** A page record rebuilt from the envelope and its kind's allowlist, or null. Strings are cut to 200 characters. */
301
+ function cleanPageRecord(raw, now = Date.now()) {
302
+ const r = obj(raw);
303
+ if (r.v !== 1) return null;
304
+ const kind = oneOf$1(r.kind, PAGE_KINDS);
305
+ const ws = typeof r.ws === "string" && WS.test(r.ws) ? r.ws : void 0;
306
+ const page = id(r.page), load = r.load === "-" ? "-" : id(r.load), seq = count$1(r.seq);
307
+ if (!kind || !ws || !page || !load || seq === void 0) return null;
308
+ const body = BODY[kind](r);
309
+ if (!body) return null;
310
+ return {
311
+ v: 1,
312
+ ws,
313
+ page,
314
+ load,
315
+ seq,
316
+ ts: typeof r.ts === "number" && Number.isFinite(r.ts) && Math.abs(r.ts - now) < 864e5 ? Math.round(r.ts) : now,
317
+ kind,
318
+ ...prune$1(body)
319
+ };
320
+ }
321
+ /**
322
+ * The page's records into design/.local/perf/page.jsonl - best effort; how many were kept. A headless run's records
323
+ * are stamped with its `run` here, by the server and after the cleaner (spec 28 A3.8 #17): a page never names its
324
+ * run - the cleaner rebuilds every record from its kind's allowlist, so a `run` a page sent is gone. `kept` (when
325
+ * given) receives the cleaned records, for the run's collector.
326
+ */
327
+ function logPage(root, raws, o = {}) {
328
+ const now = Date.now();
329
+ const recs = raws.slice(0, 5e3).flatMap((x) => {
330
+ const c = cleanPageRecord(x, now);
331
+ return c ? [o.run ? {
332
+ ...c,
333
+ run: o.run
334
+ } : c] : [];
335
+ });
336
+ if (!recs.length) return 0;
337
+ try {
338
+ appendLog(root, pageLog(root), recs.map((c) => JSON.stringify(c)).join("\n") + "\n");
339
+ } catch {
340
+ return 0;
341
+ }
342
+ o.kept?.(recs);
343
+ return recs.length;
344
+ }
345
+ /** One run line into runs.jsonl (spec 28 A3.8 #22) - best effort. A cancelled run writes its line too. */
346
+ function appendRun(root, line) {
347
+ try {
348
+ appendLog(root, runsLog(root), JSON.stringify(line) + "\n");
349
+ return true;
350
+ } catch {
351
+ return false;
352
+ }
353
+ }
354
+ /** The run lines from `since` (epoch ms): malformed lines skipped. */
355
+ const readRuns = (root, since = 0) => readLines(runsLog(root), since);
356
+ /** At start (spec 28 A1.7): design/.local/perf/ is the owner's alone - the directory 700 and every file in it 600,
357
+ * those earlier versions created without modes included - and the logs whose lines age out lose the ones older than
358
+ * 14 days and, still past their cap (2 MB; page.jsonl 8 MB), their oldest half. Synchronous, before the server
359
+ * listens: no append of this process interleaves. Best effort; a link is never followed. */
360
+ function tidyPerfDir(root, now = Date.now()) {
361
+ try {
362
+ if (unsafe(root)) return;
363
+ const dir = perfDir(root);
364
+ let st;
365
+ try {
366
+ st = lstatSync(dir);
367
+ } catch {
368
+ return;
369
+ }
370
+ if (!st.isDirectory()) return;
371
+ chmodSync(dir, 448);
372
+ for (const name of readdirSync(dir)) {
373
+ const f = join(dir, name);
374
+ try {
375
+ if (!lstatSync(f).isFile()) continue;
376
+ chmodSync(f, 384);
377
+ if (!AGED.includes(name)) continue;
378
+ const text = readFileSync(f, "utf8");
379
+ let lines = text.split("\n").filter((l) => {
380
+ try {
381
+ const e = JSON.parse(l);
382
+ return typeof e?.ts === "number" && e.ts >= now - KEEP_MS;
383
+ } catch {
384
+ return false;
385
+ }
386
+ });
387
+ if (Buffer.byteLength(lines.join("\n")) > maxOf(name)) lines = lines.slice(Math.floor(lines.length / 2));
388
+ const next = lines.length ? lines.join("\n") + "\n" : "";
389
+ if (next === text) continue;
390
+ const tmp = `${f}.${process.pid}.tmp`;
391
+ writeFileSync(tmp, next, { mode: 384 });
392
+ renameSync(tmp, f);
393
+ } catch {}
394
+ }
395
+ } catch {}
396
+ }
397
+ //#endregion
398
+ //#region src/server/perf-jam.ts
399
+ /**
400
+ * Jam timings (0.26.1, spec 27a Part D): one JSON line per event in design/.local/perf/jam.jsonl - local, never
401
+ * committed, never published, never sent. `npx marver perf --jam` reads it back.
402
+ *
403
+ * Numbers and fixed codes only. Every event is rebuilt from an allowlist: opaque ids, frame ids, enums, bounded
404
+ * integers - no words, notes, prompts, replies, titles, names, timelines or free-text errors (a failure is a code).
405
+ * Writes are buffered and appended by one serialized writer off the hot path; past 1,000 events or 1 MB waiting,
406
+ * new events are dropped and counted (the count written when the writer catches up). A write that fails changes
407
+ * nothing for the operation it measured.
408
+ */
409
+ var perf_jam_exports = /* @__PURE__ */ __exportAll({
410
+ FAILURES: () => FAILURES,
411
+ KINDS: () => KINDS,
412
+ RUN_OUTCOMES: () => RUN_OUTCOMES,
413
+ SHOT_OUTCOMES: () => SHOT_OUTCOMES,
414
+ cleanJamEvent: () => cleanJamEvent,
415
+ jamLog: () => jamLog,
416
+ jamSeams: () => jamSeams,
417
+ logJam: () => logJam,
418
+ readJam: () => readJam
419
+ });
420
+ const jamLog = (root) => join(root, "design", ".local", "perf", "jam.jsonl");
421
+ const MAX_FILE = 2e6;
422
+ /** A test seam: how a batch of lines is appended (to hold an append open). Never set outside tests. */
423
+ const jamSeams = {};
424
+ const MAX_EVENTS = 1e3;
425
+ const MAX_BYTES = 1e6;
426
+ const CAPTURE = /^cap-\d{10,16}-[a-z0-9]{4,12}$/;
427
+ const BATCH = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
428
+ const FRAME = /^[a-z0-9][\w-]{0,79}(\/[a-z0-9][\w-]{0,79}){1,3}$/i;
429
+ const capId = (v) => typeof v === "string" && CAPTURE.test(v) ? v : void 0;
430
+ const batchId = (v) => typeof v === "string" && BATCH.test(v) ? v : void 0;
431
+ const frameId = (v) => typeof v === "string" && FRAME.test(v) ? v : void 0;
432
+ const ms = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v < 1e9 ? Math.round(v) : void 0;
433
+ const count = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e6 ? v : void 0;
434
+ const oneOf = (v, set) => typeof v === "string" && set.includes(v) ? v : void 0;
435
+ const msList = (v, max = 50) => Array.isArray(v) ? v.slice(0, max).map(ms).filter((x) => x !== void 0) : void 0;
436
+ const prune = (o) => Object.fromEntries(Object.entries(o).filter(([, x]) => x !== void 0 && !(Array.isArray(x) && !x.length)));
437
+ const RUN_OUTCOMES = [
438
+ "review",
439
+ "done",
440
+ "asks",
441
+ "failed"
442
+ ];
443
+ const FAILURES = [
444
+ "agent",
445
+ "changed",
446
+ "delivery",
447
+ "stopped",
448
+ "other"
449
+ ];
450
+ const SHOT_OUTCOMES = [
451
+ "ok",
452
+ "unchecked",
453
+ "stale",
454
+ "timeout",
455
+ "error"
456
+ ];
457
+ const KINDS = [
458
+ "capture",
459
+ "comment",
460
+ "wrapup"
461
+ ];
462
+ /** An event rebuilt from the allowlist, or null. */
463
+ function cleanJamEvent(raw) {
464
+ switch (raw.kind) {
465
+ case "capture": {
466
+ const p = raw.pictures && typeof raw.pictures === "object" ? raw.pictures : {};
467
+ const capture = capId(raw.capture);
468
+ if (!capture) return null;
469
+ return prune({
470
+ kind: "capture",
471
+ capture,
472
+ sealMs: ms(raw.sealMs),
473
+ wordsWaitMs: ms(raw.wordsWaitMs),
474
+ pictures: prune({
475
+ count: count(p.count),
476
+ ms: msList(p.ms),
477
+ late: count(p.late),
478
+ failed: count(p.failed)
479
+ }),
480
+ interrupted: raw.interrupted === true ? true : void 0
481
+ });
482
+ }
483
+ case "upload": {
484
+ const capture = capId(raw.capture);
485
+ return capture ? prune({
486
+ kind: "upload",
487
+ capture,
488
+ ms: ms(raw.ms),
489
+ tries: count(raw.tries)
490
+ }) : null;
491
+ }
492
+ case "name": {
493
+ const capture = capId(raw.capture);
494
+ return capture ? prune({
495
+ kind: "name",
496
+ capture,
497
+ ms: ms(raw.ms),
498
+ ok: raw.ok === true
499
+ }) : null;
500
+ }
501
+ case "words": return prune({
502
+ kind: "words",
503
+ audioMs: ms(raw.audioMs),
504
+ waitMs: ms(raw.waitMs),
505
+ decodeMs: ms(raw.decodeMs),
506
+ backlogMs: ms(raw.backlogMs),
507
+ modelLoadMs: ms(raw.modelLoadMs)
508
+ });
509
+ case "run": {
510
+ const batch = batchId(raw.batch);
511
+ if (!batch) return null;
512
+ const members = Array.isArray(raw.members) ? raw.members.slice(0, 50).map((m) => prune({
513
+ kind: oneOf(m?.kind, KINDS),
514
+ waitMs: ms(m?.waitMs),
515
+ outcome: oneOf(m?.outcome, RUN_OUTCOMES)
516
+ })) : void 0;
517
+ return prune({
518
+ kind: "run",
519
+ batch,
520
+ members,
521
+ beforeMs: ms(raw.beforeMs),
522
+ agentMs: msList(raw.agentMs, 10),
523
+ afterMs: ms(raw.afterMs),
524
+ deliverMs: ms(raw.deliverMs),
525
+ attempts: count(raw.attempts),
526
+ failure: oneOf(raw.failure, FAILURES),
527
+ redelivered: raw.redelivered === true ? true : void 0
528
+ });
529
+ }
530
+ case "shot": return prune({
531
+ kind: "shot",
532
+ frame: frameId(raw.frame),
533
+ scale: count(raw.scale),
534
+ ms: ms(raw.ms),
535
+ recaptures: count(raw.recaptures),
536
+ outcome: oneOf(raw.outcome, SHOT_OUTCOMES)
537
+ });
538
+ default: return null;
539
+ }
540
+ }
541
+ const writers = /* @__PURE__ */ new Map();
542
+ /** Captures already logged, per root: a resent capture event is written once. */
543
+ const seenCaptures = /* @__PURE__ */ new Map();
544
+ /** Log one Jam event (best effort, never in the way): rebuilt from the allowlist, buffered, appended in order. */
545
+ function logJam(root, raw) {
546
+ try {
547
+ const ev = cleanJamEvent(raw);
548
+ if (!ev) return;
549
+ if (ev.kind === "capture") {
550
+ const seen = seenCaptures.get(root) ?? /* @__PURE__ */ new Set();
551
+ seenCaptures.set(root, seen);
552
+ if (seen.has(ev.capture)) return;
553
+ seen.add(ev.capture);
554
+ if (seen.size > 5e3) seen.delete(seen.values().next().value);
555
+ }
556
+ const line = JSON.stringify({
557
+ v: 1,
558
+ ts: Date.now(),
559
+ ...ev
560
+ }) + "\n";
561
+ const w = writers.get(root) ?? {
562
+ queue: [],
563
+ bytes: 0,
564
+ inFlight: 0,
565
+ dropped: 0,
566
+ busy: false,
567
+ writes: 0
568
+ };
569
+ writers.set(root, w);
570
+ if (w.queue.length + w.inFlight >= MAX_EVENTS || w.bytes + line.length > MAX_BYTES) {
571
+ w.dropped++;
572
+ return;
573
+ }
574
+ w.queue.push(line);
575
+ w.bytes += line.length;
576
+ if (!w.busy) {
577
+ w.busy = true;
578
+ w.draining = new Promise((done) => setImmediate(() => void drain(root, w).finally(done)));
579
+ }
580
+ } catch {}
581
+ }
582
+ async function drain(root, w) {
583
+ const file = jamLog(root);
584
+ try {
585
+ mkdirSync(join(file, ".."), {
586
+ recursive: true,
587
+ mode: 448
588
+ });
589
+ } catch {}
590
+ while (w.queue.length || w.dropped) {
591
+ const lines = w.queue.splice(0);
592
+ const held = lines.reduce((n, l) => n + l.length, 0);
593
+ w.inFlight = lines.length;
594
+ if (w.dropped) {
595
+ lines.push(JSON.stringify({
596
+ v: 1,
597
+ ts: Date.now(),
598
+ kind: "dropped",
599
+ count: w.dropped
600
+ }) + "\n");
601
+ w.dropped = 0;
602
+ }
603
+ try {
604
+ await (jamSeams.append ?? ((f, d) => appendFile(f, d, { mode: 384 })))(file, lines.join(""));
605
+ if (++w.writes % 50 === 0 && (await stat(file)).size > MAX_FILE) {
606
+ const all = (await readFile(file, "utf8")).split("\n");
607
+ await writeFile(file, all.slice(Math.floor(all.length / 2)).join("\n"));
608
+ }
609
+ } catch {}
610
+ w.inFlight = 0;
611
+ w.bytes = Math.max(0, w.bytes - held);
612
+ }
613
+ w.busy = false;
614
+ }
615
+ /** The log read back from `since` (epoch ms): malformed lines skipped. */
616
+ function readJam(root, since = 0) {
617
+ try {
618
+ const all = readFileSync(jamLog(root), "utf8").split("\n").flatMap((l) => {
619
+ try {
620
+ const e = JSON.parse(l);
621
+ return e && typeof e.ts === "number" ? [e] : [];
622
+ } catch {
623
+ return [];
624
+ }
625
+ });
626
+ return {
627
+ events: all.filter((e) => e.ts >= since),
628
+ ...all.length ? { first: all[0].ts } : {}
629
+ };
630
+ } catch {
631
+ return { events: [] };
632
+ }
633
+ }
634
+ //#endregion
635
+ export { appendRun as a, perf_exports as c, readRuns as d, tidyPerfDir as f, PAGE_BODY_MAX as i, readPage as l, perf_jam_exports as n, logPage as o, readJam as r, logReload as s, logJam as t, readReloads as u };