@svrnsec/pulse 0.3.1 → 0.5.0

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.
@@ -0,0 +1,403 @@
1
+ /**
2
+ * @svrnsec/pulse — Population Entropy Analysis
3
+ *
4
+ * Server-side Sybil detector: given N engagement tokens from the same cohort
5
+ * (e.g., all ad clicks in a 10-minute window), determine whether they
6
+ * represent independently-operated consumer devices or a coordinated farm.
7
+ *
8
+ * Why per-token verification is necessary but not sufficient
9
+ * ──────────────────────────────────────────────────────────
10
+ * A single token from a farm device may pass every individual check:
11
+ * - Real hardware ✓ (it is a real phone)
12
+ * - Valid idle proof ✓ (45+ seconds of forced downtime)
13
+ * - Plausible entropy ✓ (within normal ranges)
14
+ *
15
+ * But 1,000 farm devices in one warehouse share inescapable physical context:
16
+ * - Same ambient temperature → correlated DRAM timing variance
17
+ * - Same power circuit → near-identical ENF phase deviation
18
+ * - Coordinator dispatch → rhythmic submission timestamp patterns
19
+ * - Same operational pattern → homogeneous thermal transitions
20
+ * - Economic throughput limit → idle durations cluster at the minimum
21
+ *
22
+ * Population analysis exposes these systematic correlations that are
23
+ * individually invisible but statistically unmistakable at scale.
24
+ *
25
+ * Statistical tests (5)
26
+ * ─────────────────────
27
+ * 1. Timestamp rhythm — autocorrelation of inter-arrival times
28
+ * 2. Entropy dispersion — coefficient of variation across physics scores
29
+ * 3. Thermal diversity — Shannon entropy of thermal transition labels
30
+ * 4. Idle duration cluster — fraction of durations near the minimum viable
31
+ * 5. ENF phase coherence — variance of measured grid frequency deviations
32
+ *
33
+ * Scoring
34
+ * ───────
35
+ * Each test returns a sybilScore in [0, 100] where 100 = maximally suspicious.
36
+ * The population sybilScore is a weighted average of the five tests.
37
+ * An authentic cohort scores < 40; a clear farm scores > 70.
38
+ */
39
+
40
+ // ── analysePopulation ─────────────────────────────────────────────────────────
41
+
42
+ /**
43
+ * Analyse a cohort of parsed engagement tokens for population-level Sybil signals.
44
+ *
45
+ * @param {ParsedEngagementToken[]} tokens decoded (not yet signature-verified) tokens
46
+ * @param {object} [opts]
47
+ * @param {number} [opts.minSample=5] minimum cohort size for a meaningful verdict
48
+ * @returns {PopulationVerdict}
49
+ */
50
+ export function analysePopulation(tokens, opts = {}) {
51
+ const { minSample = 5 } = opts;
52
+
53
+ if (!Array.isArray(tokens) || tokens.length < minSample) {
54
+ return _insufficientSample(tokens?.length ?? 0, minSample);
55
+ }
56
+
57
+ const rhythmResult = testTimestampRhythm(tokens);
58
+ const dispersion = testEntropyDispersion(tokens);
59
+ const thermalDiv = testThermalDiversity(tokens);
60
+ const idlePlaus = testIdlePlausibility(tokens);
61
+ const enfCoherence = testEnfCoherence(tokens);
62
+
63
+ // Weighted aggregate (weights reflect signal reliability and discriminative power)
64
+ const sybilScore = Math.round(
65
+ rhythmResult.score * 0.25 +
66
+ dispersion.score * 0.25 +
67
+ thermalDiv.score * 0.20 +
68
+ idlePlaus.score * 0.20 +
69
+ enfCoherence.score * 0.10
70
+ );
71
+
72
+ // Confidence scales with cohort size — small cohorts can't make strong claims
73
+ const confidence = +Math.min(1.0, tokens.length / 50).toFixed(3);
74
+
75
+ const flags = [
76
+ rhythmResult.score > 60 && 'RHYTHMIC_DISPATCH',
77
+ dispersion.score > 60 && 'HOMOGENEOUS_HARDWARE',
78
+ thermalDiv.score > 60 && 'UNIFORM_THERMAL_PATTERN',
79
+ idlePlaus.score > 60 && 'SYNTHETIC_IDLE_CLUSTERING',
80
+ enfCoherence.score > 60 && 'CO_LOCATED_DEVICES',
81
+ ].filter(Boolean);
82
+
83
+ return {
84
+ authentic: sybilScore < 40,
85
+ sybilScore,
86
+ confidence,
87
+ tests: {
88
+ timestampRhythm: rhythmResult,
89
+ entropyDispersion: dispersion,
90
+ thermalDiversity: thermalDiv,
91
+ idlePlausibility: idlePlaus,
92
+ enfCoherence,
93
+ },
94
+ flags,
95
+ summary: _formatSummary(sybilScore, flags, tokens.length),
96
+ };
97
+ }
98
+
99
+ // ── Test 1: Timestamp Rhythm ──────────────────────────────────────────────────
100
+
101
+ /**
102
+ * Farm coordinators dispatch tokens in batches at scheduler-driven intervals.
103
+ * A coordinator sending 1,000 tokens at 120-second intervals produces strong
104
+ * positive autocorrelation in the inter-arrival time series.
105
+ *
106
+ * Real users click independently; their inter-arrival times are random.
107
+ *
108
+ * @param {ParsedEngagementToken[]} tokens
109
+ * @returns {{ score: number, ac1: number, ac2: number, detail: string }}
110
+ */
111
+ export function testTimestampRhythm(tokens) {
112
+ const timestamps = tokens.map(t => t.iat).filter(Number.isFinite).sort((a, b) => a - b);
113
+ if (timestamps.length < 4) {
114
+ return { score: 0, ac1: 0, ac2: 0, detail: 'insufficient_samples' };
115
+ }
116
+
117
+ // Inter-arrival times in milliseconds
118
+ const deltas = [];
119
+ for (let i = 1; i < timestamps.length; i++) {
120
+ deltas.push(timestamps[i] - timestamps[i - 1]);
121
+ }
122
+
123
+ const n = deltas.length;
124
+ const mean = _mean(deltas);
125
+ const v0 = _variance(deltas, mean);
126
+
127
+ // Zero-variance: perfectly uniform dispatch — maximally suspicious
128
+ if (v0 < 1e-6) {
129
+ return { score: 100, ac1: 1, ac2: 1, detail: 'perfectly_uniform_dispatch' };
130
+ }
131
+
132
+ const ac1 = _autocorrAtLag(deltas, mean, v0, 1);
133
+ const ac2 = _autocorrAtLag(deltas, mean, v0, 2);
134
+
135
+ // Use the more suspicious of the two lags
136
+ const signal = Math.max(Math.abs(ac1), Math.abs(ac2));
137
+
138
+ // Threshold calibration:
139
+ // |ac| < 0.15 → independent (real users)
140
+ // |ac| > 0.40 → rhythmic (farm coordinator)
141
+ const score = Math.min(100, Math.max(0,
142
+ ((signal - 0.15) / (0.40 - 0.15)) * 100
143
+ ));
144
+
145
+ return {
146
+ score: Math.round(score),
147
+ ac1: +ac1.toFixed(3),
148
+ ac2: +ac2.toFixed(3),
149
+ detail: signal > 0.40 ? 'rhythmic_dispatch_detected' : 'normal_variance',
150
+ };
151
+ }
152
+
153
+ // ── Test 2: Entropy Dispersion ────────────────────────────────────────────────
154
+
155
+ /**
156
+ * Real users bring diverse device histories: different ages, temperatures,
157
+ * load profiles. Their physics scores (entropy, jitter) span a wide range.
158
+ *
159
+ * Farm devices in one warehouse are typically the same model, same ambient
160
+ * temperature, running the same workload. Their scores cluster tightly.
161
+ *
162
+ * @param {ParsedEngagementToken[]} tokens
163
+ * @returns {{ score: number, cv: number, mean: number, detail: string }}
164
+ */
165
+ export function testEntropyDispersion(tokens) {
166
+ const scores = tokens.map(t => t.hw?.ent).filter(v => v != null && Number.isFinite(v) && v > 0);
167
+ if (scores.length < 3) {
168
+ return { score: 0, cv: 0, mean: 0, detail: 'no_entropy_data' };
169
+ }
170
+
171
+ const mean = _mean(scores);
172
+ if (mean < 1e-9) return { score: 50, cv: 0, mean: 0, detail: 'zero_entropy_scores' };
173
+
174
+ const stddev = Math.sqrt(_variance(scores, mean));
175
+ const cv = stddev / mean; // Coefficient of Variation
176
+
177
+ // Calibration:
178
+ // CV > 0.12 → diverse hardware (real users)
179
+ // CV < 0.04 → homogeneous (farm devices)
180
+ const score = Math.min(100, Math.max(0,
181
+ ((0.10 - cv) / (0.10 - 0.03)) * 100
182
+ ));
183
+
184
+ return {
185
+ score: Math.round(score),
186
+ cv: +cv.toFixed(4),
187
+ mean: +mean.toFixed(3),
188
+ detail: cv < 0.04 ? 'homogeneous_hardware_detected' : 'normal_diversity',
189
+ };
190
+ }
191
+
192
+ // ── Test 3: Thermal Diversity ─────────────────────────────────────────────────
193
+
194
+ /**
195
+ * Real users encounter devices in diverse thermal states throughout the day:
196
+ * cold morning wake-ups, warm post-commute picks, hot post-gaming sessions.
197
+ * The population distribution across transition labels should be broad.
198
+ *
199
+ * Farm devices maintain constant operational throughput. Their transitions
200
+ * cluster at 'step_function' (scripted pause) or 'sustained_hot' (constant load).
201
+ *
202
+ * @param {ParsedEngagementToken[]} tokens
203
+ * @returns {{ score: number, suspiciousRatio: number, distribution: object, detail: string }}
204
+ */
205
+ export function testThermalDiversity(tokens) {
206
+ const transitions = tokens.map(t => t.idle?.therm).filter(Boolean);
207
+ if (transitions.length < 3) {
208
+ return { score: 0, suspiciousRatio: 0, distribution: {}, detail: 'no_thermal_data' };
209
+ }
210
+
211
+ const counts = {};
212
+ for (const t of transitions) counts[t] = (counts[t] ?? 0) + 1;
213
+ const total = transitions.length;
214
+
215
+ // Farm-indicator transitions
216
+ const suspicious = (counts.step_function ?? 0) + (counts.sustained_hot ?? 0);
217
+ const suspiciousRatio = suspicious / total;
218
+
219
+ // Shannon entropy of the transition label distribution
220
+ const probs = Object.values(counts).map(c => c / total);
221
+ const popEntropy = _shannonEntropy(probs);
222
+ const maxEntropy = Math.log2(6); // 6 possible labels
223
+
224
+ // Combined: raw suspicious ratio + low population entropy both indicate farm
225
+ const ratioScore = Math.min(100, suspiciousRatio * 100);
226
+ const entropyScore = Math.min(80, (1 - popEntropy / maxEntropy) * 80);
227
+ const score = Math.round(0.6 * ratioScore + 0.4 * entropyScore);
228
+
229
+ const detail = suspiciousRatio > 0.50
230
+ ? 'majority_farm_thermal_transitions'
231
+ : popEntropy < maxEntropy * 0.40
232
+ ? 'low_thermal_label_diversity'
233
+ : 'normal_distribution';
234
+
235
+ return {
236
+ score,
237
+ suspiciousRatio: +suspiciousRatio.toFixed(3),
238
+ distribution: Object.fromEntries(
239
+ Object.entries(counts).map(([k, v]) => [k, +(v / total).toFixed(3)])
240
+ ),
241
+ detail,
242
+ };
243
+ }
244
+
245
+ // ── Test 4: Idle Duration Plausibility ───────────────────────────────────────
246
+
247
+ /**
248
+ * Real humans idle unpredictably: 5 minutes for a notification, an hour for
249
+ * lunch, 8 hours overnight. The distribution is broad and roughly log-normal.
250
+ *
251
+ * Farm scripts, constrained by throughput targets, idle for exactly the
252
+ * minimum duration required to pass attestation (~45–90s). The population
253
+ * reveals a tight cluster just above the minimum viable threshold.
254
+ *
255
+ * @param {ParsedEngagementToken[]} tokens
256
+ * @returns {{ score: number, clusterRatio: number, medianMs: number, detail: string }}
257
+ */
258
+ export function testIdlePlausibility(tokens) {
259
+ const durations = tokens.map(t => t.idle?.dMs).filter(d => d != null && d > 0);
260
+ if (durations.length < 3) {
261
+ return { score: 0, clusterRatio: 0, medianMs: 0, detail: 'no_idle_data' };
262
+ }
263
+
264
+ // The "barely made it" window: idle just long enough to pass, then immediately resume
265
+ const CLUSTER_LO = 45_000; // MIN_IDLE_MS
266
+ const CLUSTER_HI = 100_000; // 2.2× minimum — beyond this, farms lose too much throughput
267
+
268
+ const inCluster = durations.filter(d => d >= CLUSTER_LO && d <= CLUSTER_HI).length;
269
+ const clusterRatio = inCluster / durations.length;
270
+
271
+ // Low coefficient of variation = durations are suspiciously uniform
272
+ const mean = _mean(durations);
273
+ const stddev = Math.sqrt(_variance(durations, mean));
274
+ const cv = stddev / (mean + 1);
275
+
276
+ // Scoring: high cluster ratio + low duration CV = farm fingerprint
277
+ const clusterScore = Math.min(70, Math.max(0, (clusterRatio - 0.40) / (0.80 - 0.40)) * 70);
278
+ const cvScore = Math.min(30, Math.max(0, (0.50 - cv) / (0.50 - 0.10)) * 30);
279
+ const score = Math.round(clusterScore + cvScore);
280
+
281
+ return {
282
+ score,
283
+ clusterRatio: +clusterRatio.toFixed(3),
284
+ medianMs: _median(durations),
285
+ detail: clusterRatio > 0.70 ? 'idle_duration_clustered_at_minimum' : 'normal_distribution',
286
+ };
287
+ }
288
+
289
+ // ── Test 5: ENF Phase Coherence ───────────────────────────────────────────────
290
+
291
+ /**
292
+ * All devices on the same electrical circuit share the same ENF phase.
293
+ * A 1,000-device farm in a single warehouse shares one power feed — their
294
+ * ENF frequency deviations are nearly identical.
295
+ *
296
+ * Real users across a city are on separate circuits with independent phase
297
+ * evolution; their deviations spread naturally over a measurable range.
298
+ *
299
+ * @param {ParsedEngagementToken[]} tokens
300
+ * @returns {{ score: number, phaseVariance: number|null, detail: string }}
301
+ */
302
+ export function testEnfCoherence(tokens) {
303
+ const deviations = tokens
304
+ .map(t => t.hw?.enfDev)
305
+ .filter(d => d != null && Number.isFinite(d));
306
+
307
+ if (deviations.length < 4) {
308
+ return { score: 0, phaseVariance: null, detail: 'enf_unavailable_or_insufficient' };
309
+ }
310
+
311
+ const mean = _mean(deviations);
312
+ const variance = _variance(deviations, mean);
313
+
314
+ // Calibration (Hz² variance):
315
+ // variance > 0.0002 → diverse circuits (real users across a city)
316
+ // variance < 0.00002 → same rack, same feed (co-located farm)
317
+ const score = Math.round(Math.min(100, Math.max(0,
318
+ (1 - variance / 0.0002) * 100
319
+ )));
320
+
321
+ return {
322
+ score,
323
+ phaseVariance: +variance.toFixed(8),
324
+ detail: variance < 0.00002 ? 'co_located_devices_same_circuit' : 'normal_phase_spread',
325
+ };
326
+ }
327
+
328
+ // ── Internal helpers ──────────────────────────────────────────────────────────
329
+
330
+ function _mean(arr) {
331
+ if (!arr.length) return 0;
332
+ return arr.reduce((s, v) => s + v, 0) / arr.length;
333
+ }
334
+
335
+ function _variance(arr, mean) {
336
+ if (arr.length < 2) return 0;
337
+ return arr.reduce((s, v) => s + (v - mean) ** 2, 0) / arr.length;
338
+ }
339
+
340
+ function _median(arr) {
341
+ const s = [...arr].sort((a, b) => a - b);
342
+ const mid = Math.floor(s.length / 2);
343
+ return s.length % 2 ? s[mid] : Math.round((s[mid - 1] + s[mid]) / 2);
344
+ }
345
+
346
+ function _autocorrAtLag(data, mean, variance, lag) {
347
+ const n = data.length;
348
+ if (lag >= n || variance < 1e-12) return 0;
349
+ let cov = 0;
350
+ for (let i = 0; i < n - lag; i++) {
351
+ cov += (data[i] - mean) * (data[i + lag] - mean);
352
+ }
353
+ return cov / ((n - lag) * variance);
354
+ }
355
+
356
+ function _shannonEntropy(probs) {
357
+ return -probs
358
+ .filter(p => p > 0)
359
+ .reduce((s, p) => s + p * Math.log2(p), 0);
360
+ }
361
+
362
+ function _formatSummary(score, flags, n) {
363
+ const risk = score >= 80 ? 'HIGH_RISK'
364
+ : score >= 50 ? 'SUSPICIOUS'
365
+ : score >= 30 ? 'MARGINAL'
366
+ : 'CLEAN';
367
+ const flagStr = flags.length ? ` flags=[${flags.join(',')}]` : '';
368
+ return `${risk}: sybilScore=${score}/100 n=${n}${flagStr}`;
369
+ }
370
+
371
+ function _insufficientSample(n, required) {
372
+ return {
373
+ authentic: true, // default to not blocking on insufficient data
374
+ sybilScore: 0,
375
+ confidence: 0,
376
+ tests: {},
377
+ flags: [],
378
+ summary: `INSUFFICIENT_SAMPLE: ${n}/${required} tokens required for analysis`,
379
+ };
380
+ }
381
+
382
+ // ── JSDoc types ───────────────────────────────────────────────────────────────
383
+
384
+ /**
385
+ * @typedef {object} ParsedEngagementToken
386
+ * @property {number} iat issued-at Unix ms
387
+ * @property {object} [idle] idle proof summary
388
+ * @property {number|null} [idle.dMs] idle duration ms
389
+ * @property {string} [idle.therm] thermal transition label
390
+ * @property {object} [hw] hardware signal summary
391
+ * @property {number} [hw.ent] normalized entropy score (0–1)
392
+ * @property {number} [hw.enfDev] ENF frequency deviation (Hz)
393
+ */
394
+
395
+ /**
396
+ * @typedef {object} PopulationVerdict
397
+ * @property {boolean} authentic true if population appears legitimate (sybilScore < 40)
398
+ * @property {number} sybilScore 0–100 suspicion score (higher = more suspicious)
399
+ * @property {number} confidence 0–1 confidence in verdict (scales with cohort size)
400
+ * @property {object} tests per-test result objects
401
+ * @property {string[]} flags fired detection flags
402
+ * @property {string} summary one-line human-readable summary
403
+ */
@@ -0,0 +1,248 @@
1
+ /**
2
+ * @sovereign/pulse — Hypervisor & Cloud Provider Fingerprinter
3
+ *
4
+ * Each hypervisor has a distinct "steal-time rhythm" — a characteristic
5
+ * pattern in how it schedules guest vCPUs on host physical cores.
6
+ * This creates detectable signatures in the timing autocorrelation profile.
7
+ *
8
+ * Think of it like a heartbeat EKG:
9
+ * KVM → regular 50-iteration bursts (~250ms quantum at 5ms/iter)
10
+ * Xen → longer 150-iteration bursts (~750ms credit scheduler quantum)
11
+ * VMware → irregular bursts, memory balloon noise
12
+ * Hyper-V → 78-iteration bursts (~390ms at 5ms/iter, 15.6ms quantum)
13
+ * Nitro → almost none — SR-IOV passthrough is nearly invisible
14
+ * Physical → no rhythm at all
15
+ *
16
+ * Canvas renderer strings give a second, independent signal that we cross-
17
+ * reference to increase confidence in the provider classification.
18
+ */
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // Provider profile database
22
+ // ---------------------------------------------------------------------------
23
+ // Each profile is calibrated from real benchmark data.
24
+ // Fields: lag1_range, lag50_range, qe_range, cv_range, renderer_hints
25
+
26
+ const PROVIDER_PROFILES = [
27
+ {
28
+ id: 'physical',
29
+ label: 'Physical Hardware',
30
+ profile: 'analog-fog',
31
+ confidence: 0, // set dynamically
32
+ match: ({ lag1, lag50, qe, cv, entropyJitterRatio, isSoftwareRenderer }) =>
33
+ !isSoftwareRenderer &&
34
+ Math.abs(lag1) < 0.20 &&
35
+ Math.abs(lag50) < 0.15 &&
36
+ qe > 3.0 &&
37
+ cv > 0.06 &&
38
+ (entropyJitterRatio === null || entropyJitterRatio >= 1.02),
39
+ },
40
+ {
41
+ id: 'kvm-generic',
42
+ label: 'KVM Hypervisor (generic)',
43
+ profile: 'picket-fence',
44
+ match: ({ lag1, lag50, qe, cv }) =>
45
+ lag1 > 0.40 && qe < 2.5 && cv < 0.15 && Math.abs(lag50) > 0.25,
46
+ providerHints: ['digitalocean', 'linode', 'vultr', 'hetzner', 'ovh'],
47
+ },
48
+ {
49
+ id: 'kvm-digitalocean',
50
+ label: 'DigitalOcean Droplet (KVM)',
51
+ profile: 'picket-fence',
52
+ match: ({ lag1, lag50, qe, cv, rendererHints }) =>
53
+ lag1 > 0.55 && qe < 2.0 && cv < 0.12 &&
54
+ (rendererHints.some(r => ['llvmpipe', 'virtio', 'qxl'].includes(r)) ||
55
+ lag50 > 0.30),
56
+ },
57
+ {
58
+ id: 'kvm-aws-ec2-xen',
59
+ label: 'AWS EC2 (Xen/older generation)',
60
+ profile: 'picket-fence',
61
+ // Xen credit scheduler has longer period (~150 iters)
62
+ match: ({ lag1, lag25, lag50, qe, cv }) =>
63
+ qe < 2.2 && cv < 0.13 &&
64
+ lag25 > 0.20 && lag50 > 0.20 &&
65
+ lag1 < 0.50, // lag-1 less pronounced than KVM
66
+ },
67
+ {
68
+ id: 'nitro-aws',
69
+ label: 'AWS EC2 Nitro (near-baremetal)',
70
+ profile: 'near-physical',
71
+ // Nitro uses SR-IOV and dedicated hardware — steal-time is very low.
72
+ // Looks almost physical but canvas renderer gives it away.
73
+ match: ({ lag1, lag50, qe, cv, isSoftwareRenderer, rendererHints }) =>
74
+ qe > 2.5 && cv > 0.05 &&
75
+ lag1 < 0.25 && lag50 < 0.20 &&
76
+ (isSoftwareRenderer ||
77
+ rendererHints.some(r => r.includes('nvidia t4') || r.includes('nvidia a10'))),
78
+ },
79
+ {
80
+ id: 'vmware-esxi',
81
+ label: 'VMware ESXi',
82
+ profile: 'burst-scheduler',
83
+ // VMware balloon driver creates irregular memory pressure bursts
84
+ match: ({ lag1, lag50, qe, cv, rendererHints }) =>
85
+ qe < 2.5 &&
86
+ (rendererHints.some(r => r.includes('vmware')) ||
87
+ (lag1 > 0.30 && lag50 < lag1 * 0.7 && cv < 0.14)),
88
+ },
89
+ {
90
+ id: 'hyperv',
91
+ label: 'Microsoft Hyper-V',
92
+ profile: 'picket-fence',
93
+ // 15.6ms scheduler quantum → burst every ~78 iters
94
+ match: ({ lag1, lag25, qe, cv, rendererHints }) =>
95
+ qe < 2.3 &&
96
+ (rendererHints.some(r => r.includes('microsoft basic render') || r.includes('warp')) ||
97
+ (lag25 > 0.25 && lag1 > 0.35 && cv < 0.12)),
98
+ },
99
+ {
100
+ id: 'gcp-kvm',
101
+ label: 'Google Cloud (KVM)',
102
+ profile: 'picket-fence',
103
+ match: ({ lag1, lag50, qe, cv, rendererHints }) =>
104
+ qe < 2.3 && lag1 > 0.45 &&
105
+ (rendererHints.some(r => r.includes('swiftshader') || r.includes('google')) ||
106
+ (lag50 > 0.28 && cv < 0.11)),
107
+ },
108
+ {
109
+ id: 'gh200-datacenter',
110
+ label: 'NVIDIA GH200 / HPC Datacenter',
111
+ profile: 'hypervisor-flat',
112
+ // Even with massive compute, still trapped by hypervisor clock.
113
+ // GH200 shows near-zero Hurst (extreme quantization) + very high lag-1.
114
+ match: ({ lag1, qe, hurst, cv, rendererHints }) =>
115
+ (rendererHints.some(r => r.includes('gh200') || r.includes('grace hopper') ||
116
+ r.includes('nvidia a100') || r.includes('nvidia h100')) ||
117
+ (hurst < 0.10 && lag1 > 0.60 && qe < 1.8 && cv < 0.10)),
118
+ },
119
+ {
120
+ id: 'generic-vm',
121
+ label: 'Virtual Machine (unclassified)',
122
+ profile: 'picket-fence',
123
+ match: ({ lag1, qe, cv, isSoftwareRenderer }) =>
124
+ isSoftwareRenderer ||
125
+ (qe < 2.0 && lag1 > 0.35) ||
126
+ (cv < 0.02),
127
+ },
128
+ ];
129
+
130
+ // ---------------------------------------------------------------------------
131
+ // detectProvider
132
+ // ---------------------------------------------------------------------------
133
+
134
+ /**
135
+ * Classifies the host environment based on timing + canvas signals.
136
+ *
137
+ * @param {object} p
138
+ * @param {import('./jitter.js').JitterAnalysis} p.jitter
139
+ * @param {object} p.autocorrelations - extended lags including lag25, lag50
140
+ * @param {import('../collector/canvas.js').CanvasFingerprint} p.canvas
141
+ * @param {object|null} p.phases
142
+ * @returns {ProviderResult}
143
+ */
144
+ export function detectProvider({ jitter, autocorrelations, canvas, phases }) {
145
+ const rendererHints = _rendererHints(canvas?.webglRenderer, canvas?.webglVendor);
146
+
147
+ const signals = {
148
+ lag1: Math.abs(autocorrelations?.lag1 ?? 0),
149
+ lag25: Math.abs(autocorrelations?.lag25 ?? 0),
150
+ lag50: Math.abs(autocorrelations?.lag50 ?? 0),
151
+ qe: jitter.quantizationEntropy,
152
+ cv: jitter.stats?.cv ?? 0,
153
+ hurst: jitter.hurstExponent ?? 0.5,
154
+ isSoftwareRenderer: canvas?.isSoftwareRenderer ?? false,
155
+ rendererHints,
156
+ entropyJitterRatio: phases?.entropyJitterRatio ?? null,
157
+ };
158
+
159
+ // Score each profile and pick the best match
160
+ const scored = PROVIDER_PROFILES
161
+ .filter(p => {
162
+ try { return p.match(signals); }
163
+ catch { return false; }
164
+ })
165
+ .map(p => ({
166
+ ...p,
167
+ // Physical hardware is the last resort; give it lower priority when
168
+ // other profiles match so we don't misclassify VMs.
169
+ priority: p.id === 'physical' ? 0 : 1,
170
+ }))
171
+ .sort((a, b) => b.priority - a.priority);
172
+
173
+ const best = scored[0] ?? { id: 'unknown', label: 'Unknown', profile: 'unknown' };
174
+
175
+ // Confidence: how many "VM indicator" thresholds the signals cross
176
+ const vmIndicatorCount = [
177
+ signals.qe < 2.5,
178
+ signals.lag1 > 0.35,
179
+ signals.lag50 > 0.20,
180
+ signals.cv < 0.04,
181
+ signals.isSoftwareRenderer,
182
+ signals.hurst < 0.15,
183
+ phases?.entropyJitterRatio != null && phases.entropyJitterRatio < 1.02,
184
+ ].filter(Boolean).length;
185
+
186
+ const isPhysical = best.id === 'physical';
187
+ const confidence = isPhysical
188
+ ? Math.max(20, 95 - vmIndicatorCount * 15)
189
+ : Math.min(95, 40 + vmIndicatorCount * 12);
190
+
191
+ return {
192
+ providerId: best.id,
193
+ providerLabel: best.label,
194
+ profile: best.profile,
195
+ confidence,
196
+ isVirtualized: best.id !== 'physical',
197
+ signals,
198
+ alternatives: scored.slice(1, 3).map(p => ({ id: p.id, label: p.label })),
199
+ rendererHints,
200
+ schedulerQuantumMs: _estimateQuantum(signals),
201
+ };
202
+ }
203
+
204
+ /**
205
+ * @typedef {object} ProviderResult
206
+ * @property {string} providerId
207
+ * @property {string} providerLabel
208
+ * @property {string} profile 'analog-fog' | 'picket-fence' | 'burst-scheduler' | 'near-physical' | 'hypervisor-flat' | 'unknown'
209
+ * @property {number} confidence 0–100
210
+ * @property {boolean} isVirtualized
211
+ * @property {object} signals
212
+ * @property {object[]} alternatives
213
+ * @property {string[]} rendererHints
214
+ * @property {number|null} schedulerQuantumMs
215
+ */
216
+
217
+ // ---------------------------------------------------------------------------
218
+ // Internal helpers
219
+ // ---------------------------------------------------------------------------
220
+
221
+ /**
222
+ * Extract lowercase hint tokens from WebGL renderer string for pattern matching.
223
+ */
224
+ function _rendererHints(renderer = '', vendor = '') {
225
+ return `${renderer} ${vendor}`.toLowerCase()
226
+ .split(/[\s\/(),]+/)
227
+ .filter(t => t.length > 2);
228
+ }
229
+
230
+ /**
231
+ * Estimate the hypervisor's scheduler quantum from the dominant autocorrelation lag.
232
+ * Returns null if the device appears to be physical.
233
+ */
234
+ function _estimateQuantum({ lag1, lag25, lag50, qe }) {
235
+ if (qe > 3.2) return null; // likely physical
236
+
237
+ // Find the dominant lag (highest absolute autocorrelation beyond lag-5)
238
+ const lags = [
239
+ { lag: 50, ac: lag50 },
240
+ { lag: 25, ac: lag25 },
241
+ ];
242
+ const peak = lags.reduce((b, c) => c.ac > b.ac ? c : b, { lag: 0, ac: 0 });
243
+
244
+ if (peak.ac < 0.20) return null;
245
+
246
+ // Quantum (ms) ≈ dominant_lag × estimated_iteration_time (≈5ms)
247
+ return peak.lag * 5;
248
+ }