// node --test relay/test/ember.test.mjs (no dependencies; CI runs it in the site job) // The fleet aggregation of Ember Tune records (relay/lib/ember.mjs) on a fixture of records in the shape // app/igneum-app/src/ember.rs record_json writes: known-good (five samples converge on one point), known-bad (an // outlier does not move the median), the de-duplication of re-sent logs, the manifest merge and the prior lookup. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { parseRecords, dedupe, aggregate, mergeTuning, priorFor, priorLine, median, spreadPct } from '../lib/ember.mjs'; const step = (clock_mhz, power_pct, watts, mhs, mark = 'ok') => ({ clock_mhz, power_pct, limit_w: 575 * power_pct / 100, watts, mhs, eff: Number((mhs / watts).toFixed(4)), gclk: clock_mhz || 2800, mclk: 10500, tmax: 68, faults: 0, mark }); const rec = (machine, ts, chosen, over = {}) => ({ ts, machine, app: '0.3.10', os: 'windows', card: 'NVIDIA_GeForce_RTX_5090', vendor: 'nvidia', driver: '581.57', driver_major: '581', class: 'l128w16', key: 'NVIDIA_GeForce_RTX_5090|581|l128w16', plan: 'full', steps: [step(0, 100, 290, 124.0), chosen], chosen, before: step(0, 100, 290, 124.0), eff: chosen.eff, mhs: chosen.mhs, watts: chosen.watts, ...over, }); // five machines, each landing near 2,470 MHz at 100%: 0.55 to 0.57 MH/W const good = [ rec('a1', 1000, step(2472, 100, 220, 123.5)), rec('b2', 1001, step(2472, 100, 222, 123.1)), rec('c3', 1002, step(2781, 100, 236, 123.8)), rec('d4', 1003, step(2472, 100, 218, 123.6)), rec('e5', 1004, step(2163, 100, 212, 122.9)), ]; const outlier = rec('f6', 1005, step(1854, 50, 130, 118.0)); // 0.908 MH/W: a card with a broken draw reading const baseline = rec('g7', 1006, step(0, 80, 290, 122.3), { plan: 'baseline', steps: [step(0, 80, 290, 122.3)], before: null }); const amd = (machine, ts, chosen) => rec(machine, ts, chosen, { card: 'AMD_Radeon_RX_9070_XT', vendor: 'amd', driver: '32.0.15801.1', driver_major: '32', key: 'AMD_Radeon_RX_9070_XT|32|l128w16', plan: 'confirm', before: null }); test('five samples converge on the median point and an outlier does not move it', () => { const { priors, table } = aggregate(good, { minSamples: 5 }); const p = priors['NVIDIA_GeForce_RTX_5090|581|l128w16']; assert.ok(p, 'a prior at the sample floor'); assert.equal(p.clock_mhz, 2470, 'the median clock cap, rounded to 10 MHz'); assert.equal(p.power_pct, 100); assert.equal(p.samples, 5); assert.equal(p.machines, 5); assert.ok(p.eff > 0.55 && p.eff < 0.57, `eff ${p.eff}`); assert.ok(p.spread_pct >= 0 && p.spread_pct < 3, `spread ${p.spread_pct}`); assert.equal(p.before_eff, Number((124 / 290).toFixed(4))); assert.ok(p.gain_pct > 25, `gain ${p.gain_pct}% over the untuned 100% point`); assert.equal(p.updated, '1970-01-01T00:16:44Z'); // the outlier: 0.908 MH/W at 1,854 MHz joins; the median moves by one rank at most const with6 = aggregate(good.concat(outlier), { minSamples: 5 }).priors['NVIDIA_GeForce_RTX_5090|581|l128w16']; assert.equal(with6.samples, 6); assert.equal(with6.clock_mhz, 2470); assert.equal(with6.power_pct, 100); assert.ok(with6.eff < 0.58, `the outlier's 0.908 MH/W did not drag the median: ${with6.eff}`); assert.ok(with6.spread_pct < 5, `spread ${with6.spread_pct}`); assert.equal(table[0].key, 'NVIDIA_GeForce_RTX_5090|581|l128w16'); }); test('under the floor there is no prior, and baseline records never make one', () => { const { priors, table } = aggregate(good.slice(0, 4), { minSamples: 5 }); assert.deepEqual(priors, {}); assert.equal(table[0].samples, 4, 'the table still shows the count'); const b = aggregate([baseline, baseline], { minSamples: 1 }); assert.deepEqual(b.priors, {}, 'measure-only records say what a card does, never what to set'); assert.equal(b.table[0].baseline_samples, 1, 'the duplicate upload counted once'); assert.equal(b.table[0].baseline_eff, Number((122.3 / 290).toFixed(4))); // a marked chosen step (a faulted or hot winner cannot exist, but a record with one is ignored) const bad = rec('h8', 1007, step(2000, 100, 200, 120, 'faulted')); assert.deepEqual(aggregate([bad], { minSamples: 1 }).priors, {}); }); test('records are parsed out of log text and de-duplicated on machine, card and time', () => { const line = `1791230000 TUNE ${JSON.stringify(good[0])}`; const text = ['1791229999 status: x', line, line, `1791230001 TUNE ${JSON.stringify(good[1])}`, '1791230002 TUNE {not json'].join('\n'); const rs = parseRecords(text); assert.equal(rs.length, 3); assert.equal(dedupe(rs).length, 2); assert.equal(parseRecords('').length, 0); }); test('the manifest merge keeps the kernel-variant cards and carries the settings', () => { const existing = { updated: '2026-10-04T21:00:00Z', window_days: 7, cards: { NVIDIA_GeForce_RTX_5090: { variant: 'u2-ldg', race: true, candidates: ['u2-ldg', 'ldg', 'base'] } }, priors: { 'old|1|v2': { samples: 9 } } }; const { priors } = aggregate(good, { minSamples: 5 }); const t = mergeTuning(existing, priors, { rate_tolerance_pct: 1 }); assert.equal(t.cards.NVIDIA_GeForce_RTX_5090.variant, 'u2-ldg', 'lever 2 untouched'); assert.equal(t.window_days, 7); assert.deepEqual(t.ember, { enabled: true, min_samples: 5, rate_tolerance_pct: 1 }); assert.ok(!t.priors['old|1|v2'], 'a key without samples in the window drops out'); assert.ok(t.priors['NVIDIA_GeForce_RTX_5090|581|l128w16']); // the kill switch rides the same section assert.equal(mergeTuning(existing, {}, { enabled: false }).ember.enabled, false); assert.deepEqual(mergeTuning(null, {}).cards, {}); // the round trip: canonical JSON (what publish-manifest.sh signs) parses back to the same prior const back = JSON.parse(JSON.stringify(t)); assert.deepEqual(priorFor(back, 'NVIDIA_GeForce_RTX_5090|581|l128w16'), { clock_mhz: 2470, power_pct: 100, mem_mhz: 0, eff: priors['NVIDIA_GeForce_RTX_5090|581|l128w16'].eff, samples: 5 }); assert.equal(priorFor(back, 'NVIDIA_GeForce_RTX_5090|581|l128w16', 6), null, 'six wanted, five there'); assert.equal(priorFor(back, 'nothing|0|v2'), null); assert.equal(priorFor(null, 'x'), null); }); test('AMD confirm records aggregate by their own key, and the line reads', () => { const rs = [amd('p1', 2000, step(2600, 90, 177, 17.7)), amd('p2', 2001, step(2600, 90, 180, 17.6)), amd('p3', 2002, step(2500, 90, 170, 17.4))]; const { priors, table } = aggregate(rs, { minSamples: 3 }); const p = priors['AMD_Radeon_RX_9070_XT|32|l128w16']; assert.equal(p.clock_mhz, 2600); assert.equal(p.power_pct, 90); assert.equal(p.vendor, 'amd'); assert.equal(p.gain_pct, undefined, 'confirm records carry no before step and no baseline was uploaded'); assert.match(priorLine(p), /^AMD Radeon RX 9070 XT \| driver 32 \| l128w16: 2600 MHz at 90%, 0\.\d+ MH\/W, 17\.6 MH\/s at 177 W, spread \d+(\.\d+)?%, 3 sample\(s\) from 3 machine\(s\)$/); assert.equal(table.length, 1); }); test('Ember 2: climb records carry the memory clock into the prior', () => { const climb = (m, ts, mem) => ({ ...rec(m, ts, { ...step(2472, 100, 220, 123.5), mem_mhz: mem }), plan: 'climb' }); const { priors } = aggregate([climb('a1', 1, 14001), climb('b2', 2, 14001), climb('c3', 3, 13901), climb('d4', 4, 14001), climb('e5', 5, 14001)], { minSamples: 5 }); const p = priors['NVIDIA_GeForce_RTX_5090|581|l128w16']; assert.equal(p.mem_mhz, 14000, 'the median memory clock, rounded to 10 MHz'); assert.equal(p.clock_mhz, 2470); assert.equal(priorFor({ priors, ember: { min_samples: 5 } }, 'NVIDIA_GeForce_RTX_5090|581|l128w16').mem_mhz, 14000); }); test('median and spread', () => { assert.equal(median([3, 1, 2]), 2); assert.equal(median([4, 1, 2, 3]), 2.5); assert.equal(median([]), 0); assert.equal(spreadPct([1, 1, 1]), 0); assert.equal(spreadPct([10]), 0); assert.equal(spreadPct([9, 10, 11]), 10); });