@openmixer/plugin-qualify 0.17.5 → 0.18.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.
Files changed (109) hide show
  1. package/README.md +127 -13
  2. package/bin/plugin-qualify-sync.mjs +3 -1
  3. package/dist/clap-qualify.d.ts +3 -1
  4. package/dist/clap-qualify.js +7 -1
  5. package/dist/clap-qualify.js.map +1 -1
  6. package/dist/direct-limit.d.ts +27 -0
  7. package/dist/direct-limit.js +52 -0
  8. package/dist/direct-limit.js.map +1 -0
  9. package/dist/distributed-records.d.ts +4 -2
  10. package/dist/distributed-records.js +1 -1
  11. package/dist/distributed-records.js.map +1 -1
  12. package/dist/host-admission.d.ts +12 -2
  13. package/dist/host-admission.js +24 -4
  14. package/dist/host-admission.js.map +1 -1
  15. package/dist/host-profiles.d.ts +8 -0
  16. package/dist/host-profiles.js +16 -1
  17. package/dist/host-profiles.js.map +1 -1
  18. package/dist/hosting-codes.generated.d.ts +10 -0
  19. package/dist/hosting-codes.generated.js +10 -0
  20. package/dist/hosting-codes.generated.js.map +1 -1
  21. package/dist/hosting-suitability.d.ts +34 -8
  22. package/dist/hosting-suitability.js +92 -7
  23. package/dist/hosting-suitability.js.map +1 -1
  24. package/dist/index.d.ts +3 -0
  25. package/dist/index.js +3 -0
  26. package/dist/index.js.map +1 -1
  27. package/dist/measurement/format.d.ts +54 -8
  28. package/dist/measurement/format.js +54 -3
  29. package/dist/measurement/format.js.map +1 -1
  30. package/dist/measurement/plan.d.ts +3 -3
  31. package/dist/measurement/plan.js +1 -1
  32. package/dist/measurement-grid.d.ts +8 -0
  33. package/dist/measurement-grid.js +4 -0
  34. package/dist/measurement-grid.js.map +1 -1
  35. package/dist/memory-bound.d.ts +6 -0
  36. package/dist/memory-bound.js +8 -0
  37. package/dist/memory-bound.js.map +1 -1
  38. package/dist/memory-budget.d.ts +39 -0
  39. package/dist/memory-budget.js +32 -0
  40. package/dist/memory-budget.js.map +1 -0
  41. package/dist/probe-judge.d.ts +11 -0
  42. package/dist/probe-judge.js +26 -10
  43. package/dist/probe-judge.js.map +1 -1
  44. package/dist/probe-merge.js +6 -5
  45. package/dist/probe-merge.js.map +1 -1
  46. package/dist/probe-output-fixtures.d.ts +6 -0
  47. package/dist/probe-output-fixtures.js +8 -0
  48. package/dist/probe-output-fixtures.js.map +1 -1
  49. package/dist/probe-output.d.ts +4 -2
  50. package/dist/probe-output.js +2 -1
  51. package/dist/probe-output.js.map +1 -1
  52. package/dist/probe-runner.js +7 -3
  53. package/dist/probe-runner.js.map +1 -1
  54. package/dist/qualification-record.d.ts +116 -10
  55. package/dist/qualification-record.js +34 -30
  56. package/dist/qualification-record.js.map +1 -1
  57. package/dist/qualify-adapter-pass.d.ts +67 -0
  58. package/dist/qualify-adapter-pass.js +255 -0
  59. package/dist/qualify-adapter-pass.js.map +1 -0
  60. package/dist/qualify-declarations.d.ts +53 -9
  61. package/dist/qualify-declarations.js +54 -2
  62. package/dist/qualify-declarations.js.map +1 -1
  63. package/dist/qualify-rejudge.d.ts +81 -0
  64. package/dist/qualify-rejudge.js +421 -0
  65. package/dist/qualify-rejudge.js.map +1 -0
  66. package/dist/qualify-sync.d.ts +72 -1
  67. package/dist/qualify-sync.js +194 -21
  68. package/dist/qualify-sync.js.map +1 -1
  69. package/fixtures/records-0.16.1/http___example.org_raffo@unversioned-2060c09ff626.default.probe.json +46 -0
  70. package/fixtures/records-0.16.1/http___example.org_raffo@unversioned-2060c09ff626.default.qualification.json +185 -0
  71. package/fixtures/records-0.16.1/http___lv2plug.in_plugins_eg-amp@unversioned-f046a60efd45.default.probe.json +51 -0
  72. package/fixtures/records-0.16.1/http___lv2plug.in_plugins_eg-amp@unversioned-f046a60efd45.default.qualification.json +232 -0
  73. package/fixtures/records-0.16.1/https___github.com_lucianodato_noise-repellent@unversioned-c3b5e21dd9a7.default.probe.json +51 -0
  74. package/fixtures/records-0.16.1/https___github.com_lucianodato_noise-repellent@unversioned-c3b5e21dd9a7.default.qualification.json +245 -0
  75. package/fixtures/records-0.16.1/in.lsp-plug.impulse_responses_mono@unversioned-d5235d2f5f53.custom.probe.json +52 -0
  76. package/fixtures/records-0.16.1/in.lsp-plug.impulse_responses_mono@unversioned-d5235d2f5f53.custom.qualification.json +145 -0
  77. package/fixtures/records-0.16.1/in.lsp-plug.trigger_midi_mono@unversioned-d5235d2f5f53.custom.probe.json +52 -0
  78. package/fixtures/records-0.16.1/in.lsp-plug.trigger_midi_mono@unversioned-d5235d2f5f53.custom.qualification.json +155 -0
  79. package/package.json +1 -1
  80. package/src/clap-qualify.ts +8 -1
  81. package/src/direct-limit.ts +68 -0
  82. package/src/distributed-records.ts +1 -1
  83. package/src/host-admission.ts +27 -4
  84. package/src/host-profiles.ts +17 -1
  85. package/src/hosting-codes.generated.ts +10 -0
  86. package/src/hosting-suitability.ts +105 -11
  87. package/src/index.ts +3 -0
  88. package/src/measurement/format.ts +97 -3
  89. package/src/measurement/plan.ts +3 -3
  90. package/src/measurement-grid.ts +5 -0
  91. package/src/memory-bound.ts +10 -0
  92. package/src/memory-budget.ts +55 -0
  93. package/src/probe-judge.ts +29 -9
  94. package/src/probe-merge.ts +6 -5
  95. package/src/probe-output-fixtures.ts +10 -1
  96. package/src/probe-output.ts +5 -2
  97. package/src/probe-runner.ts +5 -3
  98. package/src/qualification-record.ts +121 -18
  99. package/src/qualify-adapter-pass.ts +278 -0
  100. package/src/qualify-declarations.ts +67 -3
  101. package/src/qualify-rejudge.ts +452 -0
  102. package/src/qualify-sync.ts +210 -20
  103. package/tools/_declarations.py +1 -0
  104. package/tools/benchmark.py +25 -2
  105. package/tools/gen-python-declarations.mjs +1 -0
  106. package/tools/lv2-measure.mjs +7 -4
  107. package/tools/lv2_presets.py +1 -1
  108. package/tools/lv2_resolve.py +49 -0
  109. package/tools/mod-host-admit.mjs +23 -6
@@ -147,9 +147,33 @@ export const GRID_CONTINUITY_QUANTUM = CLAP_SWEEP_FRAMES;
147
147
  * same two (src/memory-bound.test.ts holds them together).
148
148
  */
149
149
  export const MEMORY_LIMIT_RAM_FRACTION = 0.5;
150
+ /**
151
+ * The address space (`RLIMIT_AS`) of a process plugin-qualify spawns directly, with no runner to give it a cgroup: all the
152
+ * machine's RAM. Address space is what a plugin reserves, not what it touches, so this is the far bound of a direct
153
+ * run, not its budget. Off for a sanitizer build, which reserves far more address space than it uses.
154
+ */
155
+ export const ADDRESS_SPACE_LIMIT_RAM_FRACTION = 1;
150
156
  export const MEMORY_BOUND_EXIT_ENV = "OMX_RT_OOM_EXIT";
151
157
  export const MEMORY_BOUND_EXIT_STATUS = 121;
152
158
 
159
+ /**
160
+ * The memory-only pass (`plugin-qualify-sync --memory-only`). With `PROBE_MEMORY_ONLY_ENV` set the probers take the
161
+ * footprint and nothing else; the probe runner passes its environment on, so its limits hold unchanged.
162
+ * `PROBE_NOTES_ENV` is the count of notes an instrument that declares no voice capacity holds, basis `probe`:
163
+ * `MEMORY_PASS_PROBE_NOTES` unless `memory_notes` says otherwise.
164
+ */
165
+ export const PROBE_MEMORY_ONLY_ENV = "OMX_PROBE_MEMORY_ONLY";
166
+ export const PROBE_NOTES_ENV = "OMX_PROBE_NOTES";
167
+ export const MEMORY_PASS_PROBE_NOTES = 16;
168
+
169
+ /**
170
+ * What `tools/mod-host-admit.mjs` is told in an adapter-only pass: how long the host has for one command, its address
171
+ * space (`ulimit -v`, in KB) and the directory for the state files of the plugin it loads.
172
+ */
173
+ export const ADMIT_STEP_MS_ENV = "OMX_ADMIT_STEP_MS";
174
+ export const ADMIT_ADDRESS_KB_ENV = "OMX_ADMIT_ADDRESS_KB";
175
+ export const ADMIT_STATE_DIR_ENV = "OMX_ADMIT_STATE_DIR";
176
+
153
177
  /** Seconds one `.clap` may take to qualify at every rate before the prober is killed. */
154
178
  export const CLAP_QUALIFY_TIMEOUT_S = 120;
155
179
 
@@ -208,10 +232,16 @@ export const DESCRIPTOR_CODE_DIR_UNWRITABLE = "qualify.descriptor.dir-unwritable
208
232
 
209
233
  /** Every threshold, so none is baked into the code. */
210
234
  export interface HostingPolicy {
211
- /** Minimum instantiate/destroy lifecycles. The operator's §14.4 ruling: 1000. */
235
+ /** Minimum instantiate/destroy lifecycles. §14.4 sets it: 1000. */
212
236
  readonly cycleFloor: number;
213
237
  /** Minimum continuous soak, seconds. Default 3 h — the operator's longest show. */
214
238
  readonly soakSeconds: number;
239
+ /**
240
+ * The memory growth a soak may show past warm-up, in KB per minute of audio. 10 MB/min ships provisional and is
241
+ * tightened from measurements; a verdict that rests on it says so.
242
+ */
243
+ readonly memoryGrowthKbPerMinCeiling: number;
244
+ readonly memoryGrowthCeilingProvisional: boolean;
215
245
  /** Above this ratio a plugin is spiky: in-process a spike is the whole desk's xrun. */
216
246
  readonly costTailRatioCeiling: number;
217
247
  /** A soak rate whose settled output sits this far under the plugin's own best rate is
@@ -222,7 +252,7 @@ export interface HostingPolicy {
222
252
  }
223
253
 
224
254
  /**
225
- * Defaults. The cycle floor is the operator's ruling of 2026-09-05 (§14.4, option c) and the
255
+ * Defaults. The cycle floor is the decision of 2026-09-05 (§14.4, option c) and the
226
256
  * soak is §3's default show length. The per-instance cost ceiling is the HOST's budget and
227
257
  * lives in {@link HOST_BUDGETS} (plugin-qualify §3a), not here.
228
258
  */
@@ -231,6 +261,8 @@ export const DEFAULT_HOSTING_POLICY: HostingPolicy = {
231
261
  soakSeconds: 3 * 60 * 60,
232
262
  costTailRatioCeiling: 4,
233
263
  soakDeadDropDb: 40,
264
+ memoryGrowthKbPerMinCeiling: 10240,
265
+ memoryGrowthCeilingProvisional: true,
234
266
  };
235
267
 
236
268
  /** What a host spends on one plugin instance — the
@@ -247,10 +279,42 @@ export type { HostBudget };
247
279
  * `omx-clap-host` runs its plugins one JACK client each, called in turn on the server's one cycle,
248
280
  * so it gets mod-host's share and pair.
249
281
  */
282
+ const GIB_KB = 1024 * 1024;
283
+
284
+ /**
285
+ * The desk's memory budget: half of its 30 GiB for the plugins of a console, the rest the engine's and the system's;
286
+ * one instance's share is what the probe bounds a run by, 4 GiB.
287
+ */
288
+ const DESK_MEMORY = { perInstanceKb: 4 * GIB_KB, perConsoleKb: 16 * GIB_KB };
289
+
290
+ /**
291
+ * What a Raspberry Pi gives its plugins' memory, by the board's RAM: all but 1 GiB for the system and Zynthian's own
292
+ * UI, and an instance's share an eighth of that. Provisional until measured on a Pi: the stress runs show a 4 GiB Pi 5 stopping
293
+ * at 400 MB free, and no plugin's footprint has been read there yet.
294
+ */
295
+ const piMemory = (ramGib: number): NonNullable<HostBudget["memory"]> => ({
296
+ perInstanceKb: ((ramGib - 1) * GIB_KB) / 8,
297
+ perConsoleKb: (ramGib - 1) * GIB_KB,
298
+ provisional: true,
299
+ cells: PI_CELLS,
300
+ });
301
+
302
+ /**
303
+ * The cells a Raspberry Pi runs: zynthian-sys's JACK settings, 48000/128 on a Pi 5 with the V5 kit and 48000/256 on a Pi 4 and
304
+ * the other kits, the one a Pi 5 with no kit card also ran its stress and first boot at. The Zynthian benchmark's matrix sweeps
305
+ * more cells; these two are what a Pi is set to.
306
+ */
307
+ export const PI_CELLS = [
308
+ { rate: MEASURE_BASE_RATE, quantum: 128 },
309
+ { rate: MEASURE_BASE_RATE, quantum: 256 },
310
+ ];
311
+
250
312
  export const HOST_BUDGETS = {
251
- "openmixer-console": { coreFractionCeiling: 0.05, rate: COST_REFERENCE_RATE, quantum: QUANTUM_FRAMES },
313
+ "openmixer-console": { coreFractionCeiling: 0.05, rate: COST_REFERENCE_RATE, quantum: QUANTUM_FRAMES, memory: DESK_MEMORY },
252
314
  jalv: { coreFractionCeiling: 0.25, rate: MEASURE_BASE_RATE, quantum: 256 },
253
315
  "mod-host": { coreFractionCeiling: 0.05, rate: MEASURE_BASE_RATE, quantum: 128 },
254
316
  "omx-clap-host": { coreFractionCeiling: 0.05, rate: MEASURE_BASE_RATE, quantum: 128 },
317
+ "openmixer-pi-4gb": { coreFractionCeiling: 0.05, rate: MEASURE_BASE_RATE, quantum: 128, memory: piMemory(4) },
318
+ "openmixer-pi-8gb": { coreFractionCeiling: 0.05, rate: MEASURE_BASE_RATE, quantum: 128, memory: piMemory(8) },
255
319
  } as const satisfies Readonly<Record<string, HostBudget>>;
256
320
 
@@ -0,0 +1,452 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ // Copyright (C) 2026 Pau Aliagas <linuxnow@gmail.com>
3
+ /**
4
+ * Keeping a device's qualification records through an upgrade, instead of measuring every plugin again. An upgrade is a
5
+ * different host (`HOSTING_NOTE`): the figures are carried across and judged again, never trusted.
6
+ *
7
+ * `plugin-qualify-sync --rejudge` measures nothing. Each record's probe output goes through the one judge
8
+ * (`judgeProbeOutput`) under today's rules, with the host runs the record already holds, and the verdicts, the tiers and
9
+ * the plugin's pin word are replaced; the figures and their provenance are not touched, and the record says who took the
10
+ * figures and when. A verdict that needs a run the record lacks (an LV2 plugin's omx-clap-host verdict is decided by its
11
+ * adapter run) reads `unknown`, and the record owes that run: nothing is guessed, and a run that never happened gives a
12
+ * plugin no pin word. A record whose plugin's bytes changed, or whose probe output is not the one it names, is left as it
13
+ * is and owes a qualification. A second run changes nothing.
14
+ *
15
+ * `plugin-qualify-sync --memory-only` takes one figure: the footprint of each recorded plugin at the cells a Pi runs,
16
+ * through the probe runner, under the limits of an ordinary run, in a world of its bundle only. Only the `memory` of those
17
+ * cells changes in the record. It judges nothing: a `--rejudge` afterwards reads the footprints. Growth over time needs a
18
+ * soak, which this does not run: the record says so in `notMeasured`.
19
+ */
20
+ import { mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync } from "node:fs";
21
+ import { tmpdir } from "node:os";
22
+ import { basename, dirname, join } from "node:path";
23
+
24
+ import { outcomeOfVerdict } from "./distributed-records.js";
25
+ import { ADAPTER_RUN_HOSTS, ADMITTED_HOSTS, type HostAdmission } from "./host-admission.js";
26
+ import { OMX_CLAP_HOST_CONFIGURED_EXTENSIONS, shippedHostProfile, type HostProfile } from "./host-profiles.js";
27
+ import { HOSTING_CODE } from "./hosting-codes.js";
28
+ import { readMemoryFootprint } from "./measurement/format.js";
29
+ import type { PluginTarget } from "./loader.js";
30
+ import { cellKey, type GridCell } from "./measurement-grid.js";
31
+ import { pinVerdictOf } from "./pins-file.js";
32
+ import { failedOutcome, judgeProbeOutput, type RecordReason } from "./probe-judge.js";
33
+ import { PROBE_OK, readProbeOutput, sha256Hex, type ProbeGridPoint, type ProbeOutput } from "./probe-output.js";
34
+ import { probeTarget, ProbeRunnerError, type ProbeRun } from "./probe-runner.js";
35
+ import { proberOffer } from "./clap-qualify.js";
36
+ import {
37
+ IN_PROCESS_TIER_HOST,
38
+ ISOLATED_TIER_HOST,
39
+ QUALIFICATION_RECORD_SCHEMA,
40
+ memoryOfCell,
41
+ tiersOf,
42
+ unmeasuredWordOf,
43
+ verdictRows,
44
+ type NotMeasured,
45
+ type OwedMeasurement,
46
+ type QualificationRecord,
47
+ type RecordGridPoint,
48
+ type RecordVerdict,
49
+ } from "./qualification-record.js";
50
+ import { PI_CELLS, PROBE_MEMORY_ONLY_ENV, PROBE_NOTES_ENV } from "./qualify-declarations.js";
51
+ import { SYNC_STATE_SCHEMA, digestFiles, recordStem, type SyncContext, type SyncEntry, type SyncPlugin, type SyncState } from "./qualify-sync.js";
52
+ import type { SuitabilityRating } from "./rating.js";
53
+ import { writeFileWhole } from "./whole-file.js";
54
+
55
+ /** The codes of a measurement a record owes. */
56
+ export const OWED_CODE = {
57
+ /** An LV2 verdict of a host that runs it through the adapter, and no measured adapter run. */
58
+ adapterRun: "qualify.sync.adapter-run-owed",
59
+ /** A verdict decided by a run under its host (mod-host, jalv), and no run in the record. */
60
+ hostRun: "qualify.sync.host-run-owed",
61
+ /** The plugin's files are not the bytes the record measured. */
62
+ bytesChanged: "qualify.sync.bytes-changed",
63
+ /** The probe output is missing, is not the copy the record names, or does not read. */
64
+ figuresUnreadable: "qualify.sync.figures-unreadable",
65
+ /** The plugin has a record that holds no figures at all. */
66
+ noFigures: "qualify.sync.no-figures",
67
+ } as const;
68
+
69
+ /** The cells the memory-only pass measures: the ones a Pi runs, 48 kHz at 128 and 256 frames. */
70
+ export const MEMORY_PASS_CELLS: readonly GridCell[] = PI_CELLS;
71
+
72
+ /** What a memory-only pass cannot say, in every record it writes to: growth over time needs a soak. */
73
+ export const MEMORY_PASS_NOT_MEASURED: NotMeasured = { dimension: "memoryGrowth", code: HOSTING_CODE.stabilityUnsoaked };
74
+
75
+ const RECORD_SUFFIX = ".qualification.json";
76
+
77
+ /** The tool as the record says it: this package's own version, read where it is installed. */
78
+ export function rejudgeToolName(): string {
79
+ try {
80
+ const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")) as { version?: string };
81
+ return `plugin-qualify ${pkg.version ?? "unknown"}`;
82
+ } catch {
83
+ return "plugin-qualify unknown";
84
+ }
85
+ }
86
+
87
+ /** A world holding one bundle, as plugin-hostd runs a pinned LV2 plugin, for as long as `body` runs. */
88
+ export function lv2World<T>(bundle: string, body: (world: string) => T): T {
89
+ const world = mkdtempSync(join(tmpdir(), "plugin-qualify-sync-world-"));
90
+ try {
91
+ symlinkSync(bundle, join(world, basename(bundle)));
92
+ return body(world);
93
+ } finally {
94
+ rmSync(world, { recursive: true, force: true });
95
+ }
96
+ }
97
+
98
+ // --- the judgment of one record ---------------------------------------------------------------------------------------
99
+
100
+ /** The probe output with the footprints a memory-only pass merged into the record: the probe copy itself stays as it was taken. */
101
+ function withMemoryRun(output: ProbeOutput, record: QualificationRecord): ProbeOutput {
102
+ const merged = record.memoryRun;
103
+ if (merged === undefined) return output;
104
+ const grid: ProbeGridPoint[] = [...output.grid];
105
+ for (const c of merged.cells) {
106
+ const point = record.measurements.grid.find((p) => p.rate === c.rate && p.quantum === c.quantum);
107
+ const memory = readMemoryFootprint(point?.memory);
108
+ if (memory === undefined) continue;
109
+ const at = grid.findIndex((p) => p.rate === c.rate && p.quantum === c.quantum);
110
+ if (at >= 0) grid[at] = { ...grid[at]!, memory };
111
+ else grid.push({ rate: c.rate, quantum: c.quantum, outcome: PROBE_OK, memory });
112
+ }
113
+ return { ...output, grid };
114
+ }
115
+
116
+ /** The offer a profile is judged under for a CLAP plugin; `undefined` for a host the prober cannot stand in for. */
117
+ const offerIdOf = (profile: HostProfile): string | undefined => {
118
+ const offer = proberOffer(profile);
119
+ return offer === undefined ? undefined : offer.length > 0 ? "custom" : "default";
120
+ };
121
+
122
+ /**
123
+ * The profiles a record is judged under: the hosts it has verdicts for, the console's two tiers, the host of this run, and
124
+ * the Pi boards, whose budgets read the footprints. A host only joins a CLAP plugin's record of its own offer.
125
+ */
126
+ function rejudgeProfiles(record: QualificationRecord, output: ProbeOutput, host: string): { profiles: HostProfile[]; kept: RecordVerdict[] } {
127
+ const format = output.plugin.format;
128
+ const added = [IN_PROCESS_TIER_HOST, ISOLATED_TIER_HOST[format], host, ...ADAPTER_RUN_HOSTS].filter((id): id is string => id !== undefined);
129
+ const profiles = new Map<string, HostProfile>();
130
+ const kept: RecordVerdict[] = [];
131
+ for (const v of record.verdicts) {
132
+ const p = shippedHostProfile(v.host);
133
+ if (p === undefined) kept.push(v);
134
+ else profiles.set(p.id, p);
135
+ }
136
+ for (const id of added) {
137
+ const p = shippedHostProfile(id);
138
+ if (p === undefined || profiles.has(id)) continue;
139
+ if (format === "lv2" || offerIdOf(p) === output.offer.id) profiles.set(id, p);
140
+ }
141
+ return { profiles: [...profiles.values()], kept };
142
+ }
143
+
144
+ /** The runs the record holds, as the judge takes them: its adapter run, and each `run` verdict of a host that is decided by one. */
145
+ function admissionsOf(record: QualificationRecord): Record<string, HostAdmission> {
146
+ const out: Record<string, HostAdmission> = {};
147
+ for (const v of record.verdicts) {
148
+ const host = ADMITTED_HOSTS.find((h) => h === v.host);
149
+ if (host === undefined || v.arm !== "run") continue;
150
+ const r = v.reasons.find((x) => x.dimension === "hostAdmission");
151
+ out[host] = r === undefined ? { host, measured: true, admitted: true, params: {} } : { host, measured: r.rating !== "unknown", admitted: false, code: r.code, params: r.params };
152
+ }
153
+ const a = record.adapter;
154
+ if (a?.measured === true) {
155
+ const run: HostAdmission = { host: "omx-clap-host", measured: true, admitted: a.admitted, ...(a.code === undefined ? {} : { code: a.code }), params: a.params };
156
+ for (const id of ADAPTER_RUN_HOSTS) out[id] = run;
157
+ }
158
+ return out;
159
+ }
160
+
161
+ const owedReason = (code: string, params: RecordReason["params"]): RecordReason => ({ dimension: "hostAdmission", rating: "unknown", code, params });
162
+
163
+ /** The runs the verdicts of these profiles need and the record does not hold. A run that did not end well decides nothing, so it owes none. */
164
+ function runsOwed(record: QualificationRecord, output: ProbeOutput, profiles: readonly HostProfile[]): { reasons: Record<string, RecordReason>; owes: OwedMeasurement[] } {
165
+ const reasons: Record<string, RecordReason> = {};
166
+ const owes: OwedMeasurement[] = [];
167
+ if (failedOutcome(output) !== undefined) return { reasons, owes };
168
+ if (output.plugin.format === "lv2" && record.adapter?.measured !== true) {
169
+ const why = record.adapter?.code;
170
+ const params: RecordReason["params"] = why === undefined ? {} : { lastRun: why };
171
+ for (const p of profiles) if (ADAPTER_RUN_HOSTS.includes(p.id)) reasons[p.id] = owedReason(OWED_CODE.adapterRun, params);
172
+ owes.push({ measurement: "adapter-run", host: "omx-clap-host", code: OWED_CODE.adapterRun, ...(why === undefined ? {} : { detail: why }) });
173
+ }
174
+ for (const v of record.verdicts) {
175
+ if (!ADMITTED_HOSTS.some((h) => h === v.host) || v.arm === "run") continue;
176
+ reasons[v.host] = owedReason(OWED_CODE.hostRun, { host: v.host });
177
+ owes.push({ measurement: "host-run", host: v.host, code: OWED_CODE.hostRun });
178
+ }
179
+ return { reasons, owes };
180
+ }
181
+
182
+ export class RejudgeRefusal extends Error {}
183
+
184
+ /**
185
+ * One record judged again from its probe output, with what it owes. The figures, who took them and when are as they were;
186
+ * only the verdicts, the tiers and the cost reasons beside them are new. `at` is the prior re-judge's when nothing else
187
+ * differs, so that a second run leaves the file alone.
188
+ */
189
+ export function rejudgeRecord(record: QualificationRecord, probeBytes: Uint8Array, options: { readonly host: string; readonly by: string; readonly at: string }): { record: QualificationRecord; owes: readonly OwedMeasurement[] } {
190
+ if (sha256Hex(probeBytes) !== record.probe.sha256) throw new RejudgeRefusal(`${record.probe.file} is not the probe output the record names (its SHA-256 differs)`);
191
+ const read = readProbeOutput(Buffer.from(probeBytes).toString("utf8"));
192
+ if (!read.ok) throw new RejudgeRefusal(`${record.probe.file}: ${read.code}: ${read.problem}`);
193
+ const output = withMemoryRun(read.output, record);
194
+ const { profiles, kept } = rejudgeProfiles(record, output, options.host);
195
+ const { reasons, owes } = runsOwed(record, output, profiles);
196
+ const judgment = judgeProbeOutput(output, profiles, { admissions: admissionsOf(record), owed: reasons });
197
+ const memory = record.memoryRun === undefined ? {} : { memory: { tool: record.memoryRun.tool, measuredAt: record.memoryRun.measuredAt } };
198
+ const a = record.adapter;
199
+ const adapterTaken = a?.versions === undefined || a.measuredAt === undefined ? {} : { adapter: { versions: a.versions, measuredAt: a.measuredAt } };
200
+ const { rejudged: prior, ...rest } = record;
201
+ const next = (at: string): QualificationRecord => ({
202
+ ...rest,
203
+ verdicts: [...verdictRows(judgment), ...kept],
204
+ tiers: tiersOf(judgment),
205
+ cost: judgment.hosts.map((h) => ({ host: h.verdict.host, ...h.cost })),
206
+ rejudged: {
207
+ at,
208
+ by: options.by,
209
+ figures: { tool: record.tool, measuredAt: record.measuredAt, runId: read.output.run.id, ...memory, ...adapterTaken },
210
+ ...(owes.length === 0 ? {} : { owes }),
211
+ },
212
+ });
213
+ const same = prior !== undefined && JSON.stringify(next(prior.at)) === JSON.stringify(record);
214
+ return { record: same ? record : next(options.at), owes };
215
+ }
216
+
217
+ // --- a device's records -----------------------------------------------------------------------------------------------
218
+
219
+ /** The file that lists what a re-judge left owing, beside the state file. */
220
+ export const owedFile = (stateFile: string): string => join(dirname(stateFile), "qualify-owed.json");
221
+
222
+ export const OWED_SCHEMA = "plugin-qualify.owed/1";
223
+
224
+ export interface OwingRecord {
225
+ readonly plugin: string;
226
+ /** The record's file name, when the plugin has one. */
227
+ readonly record?: string;
228
+ readonly owes: readonly OwedMeasurement[];
229
+ }
230
+
231
+ export interface RejudgeResult {
232
+ readonly state: SyncState;
233
+ readonly changed: number;
234
+ readonly unchanged: number;
235
+ readonly owing: readonly OwingRecord[];
236
+ }
237
+
238
+ /** The entries whose files are not the bytes they were qualified from, and their records' stems. */
239
+ function changedStems(state: SyncState): { stems: Set<string>; entries: Set<string> } {
240
+ const stems = new Set<string>();
241
+ const entries = new Set<string>();
242
+ for (const [key, e] of Object.entries(state.entries)) {
243
+ const now = digestFiles(e.root, e.files.map((f) => f.path), e.files);
244
+ if (now !== undefined && now.every((f) => e.files.some((x) => x.path === f.path && x.sha256 === f.sha256))) continue;
245
+ entries.add(key);
246
+ for (const p of e.plugins) {
247
+ const stem = p.record === undefined ? undefined : recordStem(p.record);
248
+ if (stem !== undefined) stems.add(stem);
249
+ }
250
+ }
251
+ return { stems, entries };
252
+ }
253
+
254
+ const readRecord = (path: string): QualificationRecord | undefined => {
255
+ try {
256
+ const r = JSON.parse(readFileSync(path, "utf8")) as QualificationRecord;
257
+ return r.schema === QUALIFICATION_RECORD_SCHEMA ? r : undefined;
258
+ } catch {
259
+ return undefined;
260
+ }
261
+ };
262
+
263
+ const nameOf = (record: QualificationRecord): string => record.plugin.id;
264
+
265
+ /** A state plugin with the verdict its record now holds for the host: the pin's files stay as they are, only its word follows. */
266
+ function pluginWithVerdict(plugin: SyncPlugin, v: RecordVerdict): SyncPlugin {
267
+ const { pin, code: _code, ...rest } = plugin;
268
+ const word = v.measured ? pinVerdictOf(v.rating as SuitabilityRating) : undefined;
269
+ return {
270
+ ...rest,
271
+ outcome: outcomeOfVerdict(v),
272
+ ...(v.deciding === undefined ? {} : { code: v.deciding }),
273
+ ...(pin === undefined ? {} : { pin: { uri: pin.uri, integrity: pin.integrity, ...(word === undefined ? {} : { verdict: word }) } }),
274
+ };
275
+ }
276
+
277
+ /**
278
+ * Every record of the device's qualification directory judged again, the state's outcomes and pin words brought into line,
279
+ * and the list of what is owed written beside the state file. Each file is written whole, and only when it differs.
280
+ */
281
+ export function rejudgeState(ctx: SyncContext, state: SyncState, now: () => string = () => new Date().toISOString()): RejudgeResult {
282
+ const { qualificationDir: dir, hostProfile, stateFile } = ctx.settings;
283
+ const by = rejudgeToolName();
284
+ const at = now();
285
+ const changed = changedStems(state);
286
+ const judged = new Map<string, QualificationRecord>();
287
+ const owing: OwingRecord[] = [];
288
+ let changedCount = 0;
289
+ let unchanged = 0;
290
+ let names: string[] = [];
291
+ try {
292
+ names = readdirSync(dir).filter((f) => f.endsWith(RECORD_SUFFIX)).sort();
293
+ } catch {
294
+ // no directory, no record
295
+ }
296
+ for (const name of names) {
297
+ const path = join(dir, name);
298
+ const record = readRecord(path);
299
+ if (record === undefined) {
300
+ ctx.log(`${name}: does not read as a record; left as it is`);
301
+ owing.push({ plugin: name, record: name, owes: [{ measurement: "qualification", code: OWED_CODE.figuresUnreadable }] });
302
+ continue;
303
+ }
304
+ if (changed.stems.has(recordStem(name) ?? "")) {
305
+ owing.push({ plugin: nameOf(record), record: name, owes: [{ measurement: "qualification", code: OWED_CODE.bytesChanged }] });
306
+ continue;
307
+ }
308
+ let result: ReturnType<typeof rejudgeRecord>;
309
+ try {
310
+ result = rejudgeRecord(record, readFileSync(join(dir, record.probe.file)), { host: hostProfile.id, by, at });
311
+ } catch (e) {
312
+ const detail = e instanceof RejudgeRefusal ? e.message : `${record.probe.file}: ${(e as NodeJS.ErrnoException).code ?? String(e)}`;
313
+ ctx.log(`${name}: ${detail}; left as it is`);
314
+ owing.push({ plugin: nameOf(record), record: name, owes: [{ measurement: "qualification", code: OWED_CODE.figuresUnreadable, detail }] });
315
+ continue;
316
+ }
317
+ if (result.record === record) unchanged++;
318
+ else {
319
+ writeFileWhole(path, `${JSON.stringify(result.record, null, 2)}\n`);
320
+ changedCount++;
321
+ }
322
+ judged.set(name, result.record);
323
+ if (result.owes.length > 0) owing.push({ plugin: nameOf(record), record: name, owes: result.owes });
324
+ }
325
+ const entries: Record<string, SyncEntry> = {};
326
+ for (const [key, entry] of Object.entries(state.entries)) {
327
+ if (changed.entries.has(key)) {
328
+ entries[key] = entry;
329
+ continue;
330
+ }
331
+ const plugins = entry.plugins.map((plugin) => {
332
+ const record = plugin.record === undefined ? undefined : judged.get(basename(plugin.record));
333
+ const v = record?.verdicts.find((x) => x.host === hostProfile.id);
334
+ if (v !== undefined) return pluginWithVerdict(plugin, v);
335
+ if (record === undefined && plugin.outcome === "unmeasured" && plugin.record?.endsWith(RECORD_SUFFIX) !== true) {
336
+ owing.push({ plugin: plugin.uri, ...(plugin.record === undefined ? {} : { record: basename(plugin.record) }), owes: [{ measurement: "qualification", code: OWED_CODE.noFigures }] });
337
+ }
338
+ return plugin;
339
+ });
340
+ entries[key] = { ...entry, plugins };
341
+ }
342
+ owing.sort((a, b) => (a.record ?? a.plugin).localeCompare(b.record ?? b.plugin));
343
+ for (const o of owing) ctx.log(`owes: ${o.plugin}: ${o.owes.map((x) => `${x.measurement}${x.host === undefined ? "" : ` under ${x.host}`}`).join(", ")}`);
344
+ writeIfDifferent(owedFile(stateFile), `${JSON.stringify({ schema: OWED_SCHEMA, owing }, null, 1)}\n`);
345
+ return { state: { schema: SYNC_STATE_SCHEMA, entries }, changed: changedCount, unchanged, owing };
346
+ }
347
+
348
+ function writeIfDifferent(path: string, text: string): void {
349
+ try {
350
+ if (readFileSync(path, "utf8") === text) return;
351
+ } catch {
352
+ // not there yet
353
+ }
354
+ writeFileWhole(path, text);
355
+ }
356
+
357
+ // --- the memory-only pass ---------------------------------------------------------------------------------------------
358
+
359
+ /**
360
+ * A memory-only run's footprints in a record: the `memory` of each cell and nothing else of it, a cell the record's grid
361
+ * does not hold joined with no other figure. A cell the run could not read says why instead of a figure, so that it is
362
+ * never read as fitting. Pure.
363
+ */
364
+ export function mergeMemoryRun(record: QualificationRecord, output: ProbeOutput, cells: readonly GridCell[]): QualificationRecord {
365
+ const grid: RecordGridPoint[] = [...record.measurements.grid];
366
+ const said: { rate: number; quantum: number; outcome: string; memoryLimit?: { bytes: number; mechanism: string } }[] = [];
367
+ for (const c of cells) {
368
+ const p = output.grid.find((g) => cellKey(g) === cellKey(c));
369
+ const outcome = p?.outcome ?? output.run.outcome;
370
+ const memory = p?.memory ?? { unmeasuredReason: unmeasuredWordOf(outcome), unmeasuredDetail: `the memory-only run ended ${outcome}` };
371
+ const figures = memoryOfCell({ rate: c.rate, quantum: c.quantum, outcome, memory });
372
+ const at = grid.findIndex((g) => cellKey(g) === cellKey(c));
373
+ if (at >= 0) grid[at] = { ...grid[at]!, ...figures };
374
+ else grid.push({ rate: c.rate, quantum: c.quantum, outcome: PROBE_OK, latency: { unmeasuredReason: "not-attempted", code: HOSTING_CODE.latencyUnmeasured }, ...figures });
375
+ said.push({ rate: c.rate, quantum: c.quantum, outcome, ...(p?.memoryLimit === undefined ? {} : { memoryLimit: p.memoryLimit }) });
376
+ }
377
+ const notMeasured = record.notMeasured ?? [];
378
+ return {
379
+ ...record,
380
+ measurements: { ...record.measurements, grid },
381
+ notMeasured: notMeasured.some((n) => n.dimension === MEMORY_PASS_NOT_MEASURED.dimension) ? notMeasured : [...notMeasured, MEMORY_PASS_NOT_MEASURED],
382
+ memoryRun: { tool: output.run.tools, measuredAt: output.run.startedAt, runId: output.run.id, cells: said },
383
+ };
384
+ }
385
+
386
+ /** Whether a record already holds this pass's footprints. */
387
+ const memoryTaken = (record: QualificationRecord, cells: readonly GridCell[]): boolean =>
388
+ record.memoryRun !== undefined && cells.every((c) => record.memoryRun!.cells.some((m) => cellKey(m) === cellKey(c)));
389
+
390
+ export interface MemoryPassResult {
391
+ readonly measured: number;
392
+ readonly already: number;
393
+ readonly failed: number;
394
+ }
395
+
396
+ /** The extensions a record was measured with: its file says which offer it is. */
397
+ const offerOf = (recordName: string): readonly string[] => (recordName.endsWith(`.custom${RECORD_SUFFIX}`) ? OMX_CLAP_HOST_CONFIGURED_EXTENSIONS : []);
398
+
399
+ /**
400
+ * The footprint of each plugin of the state, at `MEMORY_PASS_CELLS`, merged into the record its verdict for this run's host
401
+ * came from. One plugin's failure is that plugin's; each record is written whole as its plugin ends, so a pass cut short
402
+ * keeps what it finished, and a pass run again skips those unless `force`.
403
+ */
404
+ export function memoryPass(ctx: SyncContext, state: SyncState, options: { readonly force?: boolean; readonly run?: ProbeRun } = {}): MemoryPassResult {
405
+ const { memory, budgetS, memoryNotes } = ctx.settings;
406
+ const count = { measured: 0, already: 0, failed: 0 };
407
+ const changed = changedStems(state);
408
+ for (const [key, entry] of Object.entries(state.entries)) {
409
+ if (changed.entries.has(key)) {
410
+ ctx.log(`${key}: its files changed since it was measured; the sync measures it whole`);
411
+ continue;
412
+ }
413
+ for (const plugin of entry.plugins) {
414
+ if (plugin.record === undefined || !plugin.record.endsWith(RECORD_SUFFIX)) continue;
415
+ const name = basename(plugin.record);
416
+ const path = join(ctx.settings.qualificationDir, name);
417
+ const record = readRecord(path);
418
+ if (record === undefined) {
419
+ ctx.log(`${plugin.uri}: its record ${name} does not read; no footprint taken`);
420
+ count.failed++;
421
+ continue;
422
+ }
423
+ if (options.force !== true && memoryTaken(record, MEMORY_PASS_CELLS)) {
424
+ count.already++;
425
+ continue;
426
+ }
427
+ const started = Date.now();
428
+ const env = { ...ctx.env, [PROBE_MEMORY_ONLY_ENV]: "1", [PROBE_NOTES_ENV]: String(memoryNotes) };
429
+ const limits = { rates: [...new Set(MEMORY_PASS_CELLS.map((c) => c.rate))], cells: MEMORY_PASS_CELLS, wallS: budgetS, ...(memory === undefined ? {} : { memory }), ...(options.run === undefined ? {} : { run: options.run }) };
430
+ try {
431
+ const probed =
432
+ entry.format === "clap"
433
+ ? probeTarget(clapTarget(key, entry, record), { ...limits, offer: offerOf(name), env })
434
+ : lv2World(entry.root, (world) => probeTarget({ format: "lv2", uri: record.plugin.id, bundle: entry.root }, { ...limits, env: { ...env, LV2_PATH: world } }));
435
+ if (probed.output.binary.sha256 !== record.binary.sha256) throw new ProbeRunnerError(`measured binary ${probed.output.binary.sha256.slice(0, 12)}, the record's is ${record.binary.sha256.slice(0, 12)}`);
436
+ writeFileWhole(path, `${JSON.stringify(mergeMemoryRun(record, probed.output, MEMORY_PASS_CELLS), null, 2)}\n`);
437
+ count.measured++;
438
+ ctx.log(`${plugin.uri}: footprint taken at ${MEMORY_PASS_CELLS.map(cellKey).join(", ")} in ${((Date.now() - started) / 1000).toFixed(1)} s`);
439
+ } catch (e) {
440
+ if (!(e instanceof ProbeRunnerError)) throw e;
441
+ count.failed++;
442
+ ctx.log(`${plugin.uri}: no footprint: ${e.message}`);
443
+ }
444
+ }
445
+ }
446
+ return count;
447
+ }
448
+
449
+ function clapTarget(key: string, entry: SyncEntry, record: QualificationRecord): PluginTarget {
450
+ const file = key.startsWith("clap:") ? key.slice("clap:".length) : join(entry.root, record.binary.files[0]?.path ?? "");
451
+ return { format: "clap", uri: `urn:clap:${record.plugin.id}`, bundle: entry.root, binary: basename(file), binaryPath: file };
452
+ }