mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
The results viewer formats -0 and non-finite values like Python, survives engines without toFixed(100), and shows an error when readAsText throws. CSV export writes an empty metric name as `metric:` and orders columns by name across all rows. StudyConfig rejects `metrics: []` at parse. The evaluator logs the position_ids fallback and matches more rejection wordings, runs in eval mode (restoring module modes afterwards), and handles left padding in classification. Preset getters return deep copies. Fixes #666
830 lines
37 KiB
Python
830 lines
37 KiB
Python
"""Contract tests for the static results viewer in ``docs/index.html``.
|
|
|
|
The viewer renders a user-chosen ``results.json``. These tests run its results
|
|
section under Node with a minimal recording DOM (no browser needed) and check
|
|
that file content is rendered as text, that the charted metric matches
|
|
``AblationReport.save_plots``, and that every render replaces earlier output.
|
|
They skip when ``node`` is not installed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.reporting.report import AblationReport, AblationResult
|
|
|
|
VIEWER = Path(__file__).resolve().parents[1] / "docs" / "index.html"
|
|
NODE = shutil.which("node")
|
|
pytestmark = pytest.mark.skipif(NODE is None, reason="node is not installed")
|
|
|
|
PAYLOAD = '<img src=x onerror="globalThis.pwned=1">'
|
|
|
|
# Loads the viewer's RESULTS section into a recording DOM. Elements are only
|
|
# created through createElement; any innerHTML write is recorded instead of
|
|
# parsed, so the test sees exactly what a browser would have parsed as markup.
|
|
HARNESS = r"""
|
|
const fs = require('fs');
|
|
const [htmlPath, scenariosPath] = process.argv.slice(2);
|
|
const html = fs.readFileSync(htmlPath, 'utf8');
|
|
const start = html.indexOf('// ====== RESULTS ======');
|
|
const end = html.indexOf('// ====== MODELS TABLE ======');
|
|
if (start < 0 || end < start) throw new Error('results section markers not found');
|
|
// The upload zone's drop handler lives in the DRAG & DROP section.
|
|
const dropStart = html.indexOf('// ====== DRAG & DROP ======');
|
|
const dropEnd = html.indexOf('</script>', dropStart);
|
|
if (dropStart < 0 || dropEnd < dropStart) throw new Error('drag and drop section not found');
|
|
// The demo data set the "LOAD DEMO DATA" button renders.
|
|
const demoStart = html.indexOf('const DEMO_RESULTS = {');
|
|
const demoEnd = html.indexOf('\n};', demoStart);
|
|
if (demoStart < 0 || demoEnd < demoStart) throw new Error('DEMO_RESULTS not found');
|
|
const source = html.slice(demoStart, demoEnd + 3) + '\n' + html.slice(start, end) + '\n' +
|
|
html.slice(dropStart, dropEnd);
|
|
const DEMO_RESULTS = new Function(html.slice(demoStart, demoEnd + 3) + '\nreturn DEMO_RESULTS;')();
|
|
|
|
function makeDocument() {
|
|
const created = [];
|
|
const htmlWrites = [];
|
|
class Text {
|
|
constructor(text) { this.text = String(text); }
|
|
get textContent() { return this.text; }
|
|
snapshot() { return this.text; }
|
|
}
|
|
class Element {
|
|
constructor(tag) {
|
|
this.tagName = String(tag).toLowerCase();
|
|
this.children = [];
|
|
this.style = {};
|
|
this.className = '';
|
|
this.title = '';
|
|
this.attributes = {};
|
|
this.listeners = {};
|
|
}
|
|
addEventListener(type, handler) { this.listeners[type] = handler; }
|
|
appendChild(child) { this.children.push(child); return child; }
|
|
append(...children) {
|
|
for (const c of children) this.appendChild(typeof c === 'object' ? c : new Text(c));
|
|
}
|
|
replaceChildren(...children) { this.children = []; this.append(...children); }
|
|
setAttribute(name, value) { this.attributes[name] = String(value); }
|
|
get textContent() { return this.children.map(c => c.textContent).join(''); }
|
|
set textContent(value) { this.children = value === '' ? [] : [new Text(value)]; }
|
|
set innerHTML(value) { htmlWrites.push(String(value)); this.children = [new Text(value)]; }
|
|
get innerHTML() { throw new Error('innerHTML read is not supported by the test DOM'); }
|
|
findAll(tag, out = []) {
|
|
for (const c of this.children) {
|
|
if (c instanceof Element) {
|
|
if (c.tagName === tag) out.push(c);
|
|
c.findAll(tag, out);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
}
|
|
const ids = {};
|
|
for (const id of ['results-display', 'stats-grid', 'impact-chart', 'results-table', 'upload-zone']) {
|
|
ids[id] = new Element('div');
|
|
}
|
|
const document = {
|
|
getElementById: id => ids[id] || null,
|
|
createElement: tag => { created.push(String(tag).toLowerCase()); return new Element(tag); },
|
|
createTextNode: text => new Text(text),
|
|
};
|
|
return {document, ids, created, htmlWrites, alerts: []};
|
|
}
|
|
|
|
// FileReader stand-in: a file is {text} (read succeeds) or {fail: true} (read errors).
|
|
class FakeFileReader {
|
|
readAsText(file) {
|
|
if (file.fail) { if (this.onerror) this.onerror({target: this}); return; }
|
|
this.result = file.text;
|
|
this.onload({target: this});
|
|
}
|
|
}
|
|
|
|
// Async stand-in: reads complete only when a scenario step says so, in any order.
|
|
// abort() is recorded but a completion is still delivered, as when a browser has
|
|
// already queued the load event, so the viewer must ignore stale completions itself.
|
|
// A file marked {throws: true} makes readAsText throw synchronously, as a browser
|
|
// does when the reader cannot start (for example InvalidStateError).
|
|
function makeAsyncReader(pending, aborts) {
|
|
return class AsyncFileReader {
|
|
readAsText(file) {
|
|
if (file.throws) throw new Error('readAsText threw');
|
|
this.file = file;
|
|
pending[file.id] = this;
|
|
}
|
|
// As in a browser, aborting a reader that never started a read does nothing.
|
|
abort() { if (this.file) aborts.push(this.file.id); }
|
|
};
|
|
}
|
|
|
|
function finishRead(reader) {
|
|
if (reader.file.fail) { if (reader.onerror) reader.onerror({target: reader}); return; }
|
|
reader.result = reader.file.text;
|
|
reader.onload({target: reader});
|
|
}
|
|
|
|
function load(dom, Reader = FakeFileReader) {
|
|
const optional = name => `${name}: typeof ${name} === "function" ? ${name} : null`;
|
|
return new Function('document', 'FileReader', 'alert', source + '\nreturn {renderResults, ' +
|
|
['selectImpactMetric', 'measuredValue', 'loadResults', 'loadDemoResults', 'toFixedHalfEven']
|
|
.map(optional).join(', ') +
|
|
'};')(dom.document, Reader, message => dom.alerts.push(String(message)));
|
|
}
|
|
|
|
// Files arrive as raw JSON text, exactly as the viewer's file loader reads them.
|
|
const {renders: scenarios, values, timings, sequences, formats} =
|
|
JSON.parse(fs.readFileSync(scenariosPath, 'utf8'));
|
|
const output = {renders: {}, values: null, timings: null, sequences: {}, formats: null,
|
|
legacy_formats: null, native_formats: null, legacy_render: null};
|
|
// Non-finite numbers cannot travel in JSON; the test sends them as their names.
|
|
const formatArgs = formats.map(([v, d]) => [typeof v === 'string' ? Number(v) : v, d]);
|
|
for (const [name, steps] of Object.entries(sequences)) {
|
|
const dom = makeDocument();
|
|
const pending = {};
|
|
const aborts = [];
|
|
const api = load(dom, makeAsyncReader(pending, aborts));
|
|
const thrown = [];
|
|
for (const step of steps) {
|
|
try {
|
|
if (step.finish) finishRead(pending[step.finish]);
|
|
else if (step.start === 'demo') api.loadDemoResults();
|
|
else {
|
|
const file = {id: step.id, text: step.text, fail: !!step.fail, throws: !!step.throws};
|
|
if (step.start === 'upload') api.loadResults({target: {files: [file]}});
|
|
else dom.ids['upload-zone'].listeners.drop({preventDefault() {}, dataTransfer: {files: [file]}});
|
|
}
|
|
} catch (err) { thrown.push(String(err && err.message || err)); }
|
|
}
|
|
const table = dom.ids['results-table'];
|
|
output.sequences[name] = {
|
|
stats: dom.ids['stats-grid'].textContent,
|
|
components: table.findAll('tr').slice(2).map(tr => tr.findAll('td')[1].textContent),
|
|
aborts,
|
|
thrown,
|
|
};
|
|
}
|
|
const probe = load(makeDocument());
|
|
if (probe.toFixedHalfEven) {
|
|
output.formats = formatArgs.map(([v, d]) => probe.toFixedHalfEven(v, d));
|
|
// A pre-ES2020 engine throws RangeError for toFixed digits above 20.
|
|
const nativeToFixed = Number.prototype.toFixed;
|
|
output.native_formats = formatArgs.map(([v, d]) => nativeToFixed.call(v, d));
|
|
Number.prototype.toFixed = function (digits) {
|
|
if (digits > 20) throw new RangeError('toFixed() digits argument must be between 0 and 20');
|
|
return nativeToFixed.call(this, digits);
|
|
};
|
|
try {
|
|
output.legacy_formats = formatArgs.map(([v, d]) => {
|
|
try { return probe.toFixedHalfEven(v, d); } catch (err) { return 'threw: ' + err.message; }
|
|
});
|
|
const dom = makeDocument();
|
|
let error = null;
|
|
try { load(dom).renderResults(DEMO_RESULTS); } catch (err) { error = String(err && err.message || err); }
|
|
output.legacy_render = {error, rows: dom.ids['results-table'].findAll('tr').length};
|
|
} finally {
|
|
Number.prototype.toFixed = nativeToFixed;
|
|
}
|
|
}
|
|
if (probe.measuredValue) {
|
|
output.values = values.map(v => probe.measuredValue(v));
|
|
output.timings = timings.map(text => {
|
|
const begin = process.hrtime.bigint();
|
|
const value = probe.measuredValue(text);
|
|
return {ms: Number(process.hrtime.bigint() - begin) / 1e6, value};
|
|
});
|
|
}
|
|
for (const [name, texts] of Object.entries(scenarios)) {
|
|
const dom = makeDocument();
|
|
const api = load(dom);
|
|
const renders = [];
|
|
for (const {via, text, fail} of texts) {
|
|
dom.created.length = 0;
|
|
dom.htmlWrites.length = 0;
|
|
dom.alerts.length = 0;
|
|
let error = null;
|
|
let data = null;
|
|
const file = fail ? {fail: true} : {text};
|
|
try {
|
|
if (via === 'upload') api.loadResults({target: {files: [file]}});
|
|
else if (via === 'drop') {
|
|
dom.ids['upload-zone'].listeners.drop({preventDefault() {}, dataTransfer: {files: [file]}});
|
|
} else {
|
|
data = JSON.parse(text);
|
|
api.renderResults(data);
|
|
}
|
|
} catch (err) { error = String(err && err.message || err); }
|
|
const table = dom.ids['results-table'];
|
|
const chart = dom.ids['impact-chart'];
|
|
renders.push({
|
|
error,
|
|
selected: api.selectImpactMetric && data !== null ? api.selectImpactMetric(data) : null,
|
|
alerts: [...dom.alerts],
|
|
shown: dom.ids['results-display'].style.display === 'block',
|
|
created: [...dom.created],
|
|
html_writes: [...dom.htmlWrites],
|
|
stats: dom.ids['stats-grid'].textContent,
|
|
chart: chart.textContent,
|
|
bars: chart.findAll('div').filter(d => d.className === 'bar-label')
|
|
.map(d => ({text: d.textContent, title: d.title})),
|
|
headers: table.findAll('th').map(th => th.textContent),
|
|
// Ablation rows only: skip the header row and the baseline row.
|
|
rows: table.findAll('tr').slice(2).map(tr => tr.findAll('td').map(td => td.textContent)),
|
|
table: table.textContent,
|
|
});
|
|
}
|
|
output.renders[name] = renders;
|
|
}
|
|
process.stdout.write(JSON.stringify(output));
|
|
"""
|
|
|
|
|
|
def upload(text: str | None) -> dict:
|
|
"""Load ``text`` through the file input (``None``: the file read fails)."""
|
|
return {"via": "upload", "text": text, "fail": text is None}
|
|
|
|
|
|
def drop(text: str | None) -> dict:
|
|
"""Load ``text`` by dropping it on the upload zone (``None``: the file read fails)."""
|
|
return {"via": "drop", "text": text, "fail": text is None}
|
|
|
|
|
|
def _entry(item: object) -> dict:
|
|
if isinstance(item, dict) and item.get("via") in {"upload", "drop"}:
|
|
return item
|
|
# A str is raw file text; anything else is serialized as the viewer would read it.
|
|
text = item if isinstance(item, str) else json.dumps(item)
|
|
return {"via": "render", "text": text, "fail": False}
|
|
|
|
|
|
def _run_harness(
|
|
tmp_path: Path, scenarios: dict[str, list[object]], values=(), timings=(), sequences=None,
|
|
formats=(),
|
|
) -> dict:
|
|
harness = tmp_path / "harness.js"
|
|
harness.write_text(HARNESS, encoding="utf-8")
|
|
cases = tmp_path / "scenarios.json"
|
|
texts = {name: [_entry(item) for item in files] for name, files in scenarios.items()}
|
|
payload = {
|
|
"renders": texts, "values": list(values), "timings": list(timings),
|
|
"sequences": sequences or {}, "formats": [list(f) for f in formats],
|
|
}
|
|
cases.write_text(json.dumps(payload), encoding="utf-8")
|
|
proc = subprocess.run(
|
|
[NODE, str(harness), str(VIEWER), str(cases)],
|
|
capture_output=True, text=True, timeout=60, check=False,
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
return json.loads(proc.stdout)
|
|
|
|
|
|
def _run_viewer(tmp_path: Path, scenarios: dict[str, list[object]]) -> dict[str, list[dict]]:
|
|
return _run_harness(tmp_path, scenarios)["renders"]
|
|
|
|
|
|
def _render_one(tmp_path: Path, data: object) -> dict:
|
|
return _run_viewer(tmp_path, {"case": [data]})["case"][0]
|
|
|
|
|
|
def _python_impact_metric(data: dict, tmp_path: Path) -> str | None:
|
|
"""The metric ``obliteratus report`` would plot for ``data`` (see cli._cmd_report)."""
|
|
data = json.loads(json.dumps(data)) # the same file text the viewer reads
|
|
primary = data.get("primary_metric")
|
|
report = AblationReport(
|
|
model_name=data["model_name"],
|
|
primary_metric=primary if isinstance(primary, str) else None,
|
|
)
|
|
report.add_baseline(data["baseline_metrics"])
|
|
for r in data["results"]:
|
|
report.add_result(AblationResult(r["strategy"], r["component"], r["description"], r["metrics"]))
|
|
plotted: list[str] = []
|
|
report.plot_impact = lambda metric=None, output_path=None, **_: plotted.append(metric)
|
|
report.plot_heatmap = lambda output_path=None: False
|
|
report.save_plots(tmp_path / "plots")
|
|
return plotted[0] if plotted else None
|
|
|
|
|
|
def _results(*metrics: dict, strategy: str = "layer_removal") -> list[dict]:
|
|
return [
|
|
{"strategy": strategy, "component": f"layer_{i}", "description": f"Remove layer {i}",
|
|
"metrics": m}
|
|
for i, m in enumerate(metrics)
|
|
]
|
|
|
|
|
|
GOOD = {
|
|
"model_name": "m",
|
|
"primary_metric": "perplexity",
|
|
"baseline_metrics": {"perplexity": 10.0},
|
|
"results": _results({"perplexity": 12.0}, {"perplexity": 9.0}),
|
|
}
|
|
EMPTY = {
|
|
"model_name": "m",
|
|
"baseline_metrics": {"perplexity": None},
|
|
"results": _results({"perplexity": 3.0}),
|
|
}
|
|
|
|
SELECTION_CASES = {
|
|
# Review example: the primary metric has a baseline but no measured ablation.
|
|
"primary_without_delta": {
|
|
"model_name": "m", "primary_metric": "a",
|
|
"baseline_metrics": {"a": 1.0, "b": 2.0},
|
|
"results": _results({"a": None, "b": 3.0}),
|
|
},
|
|
# A non-string primary_metric is ignored, never coerced to a key like "null".
|
|
"null_primary": {
|
|
"model_name": "m", "primary_metric": None,
|
|
"baseline_metrics": {"a": 2.0, "null": 1.0},
|
|
"results": _results({"a": 5.0, "null": 3.0}),
|
|
},
|
|
"bool_primary": {
|
|
"model_name": "m", "primary_metric": True,
|
|
"baseline_metrics": {"a": 2.0, "true": 1.0},
|
|
"results": _results({"a": 5.0, "true": 3.0}),
|
|
},
|
|
"object_primary": {
|
|
"model_name": "m", "primary_metric": {"name": "y"},
|
|
"baseline_metrics": {"A": 2.0, "y": 1.0, "[object Object]": 4.0},
|
|
"results": _results({"A": 5.0, "y": 3.0, "[object Object]": 1.0}),
|
|
},
|
|
"primary_measured": {
|
|
"model_name": "m", "primary_metric": "b",
|
|
"baseline_metrics": {"a": 1.0, "b": 2.0},
|
|
"results": _results({"a": 1.5, "b": 3.0}),
|
|
},
|
|
"primary_not_in_baseline": {
|
|
"model_name": "m", "primary_metric": "z",
|
|
"baseline_metrics": {"a": None, "b": 2.0},
|
|
"results": _results({"a": 1.0, "b": 3.0, "z": 4.0}),
|
|
},
|
|
# Python's float() accepts numeric strings and rejects booleans.
|
|
"numeric_string_and_bool": {
|
|
"model_name": "m", "primary_metric": "a",
|
|
"baseline_metrics": {"a": True, "b": "2.5"},
|
|
"results": _results({"a": 1.0, "b": " 30.5e0 "}),
|
|
},
|
|
# Fallback order is sorted by code point (as save_json writes keys), so "10" < "2".
|
|
"integer_like_keys": {
|
|
"model_name": "m",
|
|
"baseline_metrics": {"10": 1.0, "2": 2.0},
|
|
"results": _results({"10": 2.0, "2": 3.0}),
|
|
},
|
|
"mixed_integer_like_keys": {
|
|
"model_name": "m", "primary_metric": "b",
|
|
"baseline_metrics": {"b": 1.0, "10": None, "x": 4.0, "2": 3.0},
|
|
"results": _results({"b": None, "10": 2.0, "x": 5.0, "2": 1.0}),
|
|
},
|
|
"save_json_sorted_integer_like_keys": {
|
|
"model_name": "m", "primary_metric": None,
|
|
"baseline_metrics": {"1": None, "10": 1.0, "2": 2.0, "a": 3.0},
|
|
"results": _results({"1": 1.0, "10": 2.0, "2": 3.0, "a": 4.0}),
|
|
},
|
|
# Strings outside the shared grammar are unavailable on both sides.
|
|
"unicode_digit_baseline": {
|
|
"model_name": "m", "primary_metric": "a",
|
|
"baseline_metrics": {"a": "\u0661\u0662.\u0663", "b": 2.0},
|
|
"results": _results({"a": 3.0, "b": 3.0}),
|
|
},
|
|
# Raw metric names that look like generated "<name>_delta" columns (#618 r3).
|
|
"delta_suffix_collision": {
|
|
"model_name": "m", "primary_metric": "a",
|
|
"baseline_metrics": {"a": 1.0, "a_delta": 1.0},
|
|
"results": _results({"a": None, "a_delta": 3.0}),
|
|
},
|
|
"metric_named_like_delta_column": {
|
|
"model_name": "m",
|
|
"baseline_metrics": {"x": 1.0, "x_delta": 5.0},
|
|
"results": _results({"x": 2.0, "x_delta": None}),
|
|
},
|
|
"pct_change_suffix_collision": {
|
|
"model_name": "m", "primary_metric": "a",
|
|
"baseline_metrics": {"a": 1.0, "a_pct_change": 10.0},
|
|
"results": _results({"a": None, "a_pct_change": 20.0}),
|
|
},
|
|
"unsorted_file_order": {
|
|
"model_name": "m", "primary_metric": "z",
|
|
"baseline_metrics": {"b": 1.0, "2": 1.0, "a": 1.0, "10": 1.0},
|
|
"results": _results({"b": 2.0, "2": 2.0, "a": 2.0, "10": 2.0}),
|
|
},
|
|
"astral_vs_bmp_names": {
|
|
"model_name": "m",
|
|
"baseline_metrics": {"\U0001f600": 1.0, "\uff21": 1.0},
|
|
"results": _results({"\U0001f600": 2.0, "\uff21": 2.0}),
|
|
},
|
|
"bom_and_nel_whitespace": {
|
|
"model_name": "m",
|
|
"baseline_metrics": {"a": "\ufeff1", "b": "\u00852", "c": " 1e3 "},
|
|
"results": _results({"a": 3.0, "b": 3.0, "c": "\t5\n"}),
|
|
},
|
|
"no_measured_change": EMPTY,
|
|
}
|
|
|
|
|
|
def test_untrusted_file_content_renders_as_text_only(tmp_path):
|
|
"""Names from a loaded results.json must never be parsed as markup."""
|
|
data = {
|
|
"model_name": PAYLOAD,
|
|
"primary_metric": PAYLOAD,
|
|
"baseline_metrics": {PAYLOAD: 1.0},
|
|
"results": [
|
|
{"strategy": PAYLOAD, "component": PAYLOAD, "description": PAYLOAD,
|
|
"metrics": {PAYLOAD: 2.0}},
|
|
{"strategy": "s", "component": f'"><script>globalThis.pwned=2</script>{PAYLOAD}',
|
|
"description": "d", "metrics": {PAYLOAD: 0.5}},
|
|
],
|
|
}
|
|
render = _render_one(tmp_path, data)
|
|
assert render["error"] is None
|
|
assert render["html_writes"] == [], "results must be built without innerHTML"
|
|
assert "img" not in render["created"] and "script" not in render["created"]
|
|
assert PAYLOAD in render["stats"]
|
|
assert render["headers"][2] == PAYLOAD
|
|
assert {row[0] for row in render["rows"]} == {PAYLOAD, "s"}
|
|
assert any(bar["text"] == PAYLOAD and bar["title"] == PAYLOAD for bar in render["bars"])
|
|
|
|
|
|
def test_empty_state_message_renders_untrusted_name_as_text(tmp_path):
|
|
data = {
|
|
"model_name": "m", "primary_metric": PAYLOAD,
|
|
"baseline_metrics": {PAYLOAD: 1.0},
|
|
"results": _results({PAYLOAD: None}),
|
|
}
|
|
render = _render_one(tmp_path, data)
|
|
assert render["error"] is None
|
|
assert render["html_writes"] == []
|
|
assert "img" not in render["created"]
|
|
|
|
|
|
@pytest.mark.parametrize("name", sorted(SELECTION_CASES))
|
|
def test_viewer_charts_the_metric_obliteratus_report_plots(tmp_path, name):
|
|
data = SELECTION_CASES[name]
|
|
expected = _python_impact_metric(data, tmp_path)
|
|
render = _render_one(tmp_path, data)
|
|
assert render["error"] is None
|
|
assert render["selected"] is not None, "viewer must expose selectImpactMetric"
|
|
assert render["selected"]["metric"] == expected
|
|
if expected is None:
|
|
assert render["headers"] == [] and render["rows"] == [] and render["chart"] == ""
|
|
assert "No metric" in render["stats"]
|
|
else:
|
|
assert render["headers"][2] == expected
|
|
|
|
|
|
def test_review_example_and_non_string_primary_pick_python_metric(tmp_path):
|
|
"""Pinned values for the #618 review examples (independent of the parity helper)."""
|
|
out = _run_viewer(tmp_path, {
|
|
"review": [SELECTION_CASES["primary_without_delta"]],
|
|
"null": [SELECTION_CASES["null_primary"]],
|
|
})
|
|
review, null = out["review"][0], out["null"][0]
|
|
assert review["headers"][2] == "b"
|
|
assert review["rows"] == [["layer_removal", "layer_0", "3.0000", "+1.0000", "+50.0%"]]
|
|
assert "Plotted b" in review["chart"] and "a has no measured change" in review["chart"]
|
|
assert null["headers"][2] == "a"
|
|
assert "has no measured change" not in null["chart"]
|
|
|
|
|
|
def test_consecutive_loads_replace_every_result_region(tmp_path):
|
|
"""An empty-state file must not leave the previous file's table or chart behind."""
|
|
out = _run_viewer(tmp_path, {
|
|
"good_then_empty": [GOOD, EMPTY],
|
|
"good_then_unmeasured_primary": [
|
|
GOOD,
|
|
{"model_name": "m", "primary_metric": "a", "baseline_metrics": {"a": 1.0},
|
|
"results": _results({"a": None})},
|
|
],
|
|
"good_then_good": [GOOD, {**GOOD, "results": _results({"perplexity": 11.0})}],
|
|
})
|
|
for name in ("good_then_empty", "good_then_unmeasured_primary"):
|
|
first, second = out[name]
|
|
assert first["table"] and first["chart"]
|
|
assert second["error"] is None
|
|
assert second["table"] == "", "previous file's results table must be cleared"
|
|
assert second["chart"] == ""
|
|
assert second["rows"] == [] and second["headers"] == []
|
|
assert "No metric" in second["stats"]
|
|
assert len(first["rows"]) == 2
|
|
first, second = out["good_then_good"]
|
|
assert [row[1] for row in second["rows"]] == ["layer_0"]
|
|
assert len(second["bars"]) == 1
|
|
|
|
|
|
# Shared numeric-string grammar (report._metric_value and the viewer's measuredValue):
|
|
# ASCII digits, optional sign, decimal point and exponent, ASCII whitespace only.
|
|
VALUE_CASES = [
|
|
1.5, 0, -3, True, False, None, [], {}, "1.5", " 1e3 ", "\t-2.5E-1\n", "+.5", "1.",
|
|
"\u00a01", "\u0661\u0662.\u0663", "\u0085" + "2", "\ufeff1", "1\u2003", "1_000",
|
|
"nan", "inf", "-Infinity", "1e400", "0x10", "1e", ".", "", " ", "1 2", "\u0661",
|
|
]
|
|
|
|
|
|
def test_numeric_strings_follow_one_shared_grammar(tmp_path):
|
|
from obliteratus.reporting.report import _normalize_metrics
|
|
|
|
viewer = _run_harness(tmp_path, {}, VALUE_CASES)["values"]
|
|
assert viewer is not None, "viewer must expose measuredValue"
|
|
python = [_normalize_metrics({"m": value})[0]["m"] for value in VALUE_CASES]
|
|
assert viewer == python
|
|
assert python[VALUE_CASES.index(" 1e3 ")] == 1000.0
|
|
for rejected in ("\u0661\u0662.\u0663", "\u00852", "\ufeff1", "1_000", "nan", "inf"):
|
|
assert python[VALUE_CASES.index(rejected)] is None, rejected
|
|
|
|
|
|
def test_viewer_falls_back_in_sorted_name_order(tmp_path):
|
|
render = _render_one(tmp_path, SELECTION_CASES["unsorted_file_order"])
|
|
assert render["selected"]["metric"] == "10"
|
|
assert render["headers"][2] == "10"
|
|
# Code-point order, as Python sorts: U+FF21 before U+1F600 (UTF-16 order differs).
|
|
assert _render_one(tmp_path, SELECTION_CASES["astral_vs_bmp_names"])["headers"][2] == "\uff21"
|
|
|
|
|
|
def test_duplicate_and_proto_keys_select_like_python(tmp_path):
|
|
"""JSON.parse and json.loads agree: a repeated key keeps its last value, and a
|
|
``__proto__`` key is an ordinary metric name."""
|
|
text = (
|
|
'{"model_name": "m", "baseline_metrics": {"a": 1, "__proto__": 1, "b": 1, "a": null},'
|
|
' "results": [{"strategy": "s", "component": "c", "description": "d",'
|
|
' "metrics": {"a": 2, "__proto__": 3, "b": 2}}]}'
|
|
)
|
|
expected = _python_impact_metric(json.loads(text), tmp_path)
|
|
assert expected == "__proto__"
|
|
render = _render_one(tmp_path, text)
|
|
assert render["selected"]["metric"] == expected
|
|
assert render["headers"][2] == "__proto__"
|
|
|
|
|
|
def test_suffix_collisions_chart_the_real_metric_change(tmp_path):
|
|
out = _run_viewer(tmp_path, {
|
|
"delta": [SELECTION_CASES["delta_suffix_collision"]],
|
|
"named": [SELECTION_CASES["metric_named_like_delta_column"]],
|
|
})
|
|
delta, named = out["delta"][0], out["named"][0]
|
|
assert delta["headers"][2] == "a_delta"
|
|
assert delta["rows"] == [["layer_removal", "layer_0", "3.0000", "+2.0000", "+200.0%"]]
|
|
assert named["headers"][2] == "x"
|
|
assert named["rows"] == [["layer_removal", "layer_0", "2.0000", "+1.0000", "+100.0%"]]
|
|
|
|
|
|
def test_whitespace_trim_is_linear_time(tmp_path):
|
|
"""A long interior whitespace run must not backtrack quadratically (#618 r3)."""
|
|
run = 200_000
|
|
texts = ["1" + " " * run + "x", " " * run + "x", "x" + " " * run, " " * run + "1" + " " * run,
|
|
# The numeric grammar itself: long digit runs that fail at the end.
|
|
"1" * run + "x", "." + "1" * run + "e", "1" * run + "." + "1" * run + "e+"]
|
|
timings = _run_harness(tmp_path, {}, timings=texts)["timings"]
|
|
assert timings is not None, "viewer must expose measuredValue"
|
|
assert [t["value"] for t in timings] == [None, None, None, 1, None, None, None]
|
|
# Generous ceiling: the old quadratic trim took tens of seconds at this size.
|
|
assert max(t["ms"] for t in timings) < 5000, timings
|
|
|
|
|
|
def test_python_numeric_strings_are_linear_time():
|
|
import time
|
|
|
|
from obliteratus.reporting.report import _metric_value
|
|
|
|
run = 200_000
|
|
begin = time.perf_counter()
|
|
assert _metric_value("1" + " " * run + "x") is None
|
|
assert _metric_value(" " * run + "1" + " " * run) == 1.0
|
|
assert _metric_value("1" * run + "x") is None
|
|
assert time.perf_counter() - begin < 5.0
|
|
|
|
|
|
BAD_FILES = {
|
|
"syntax": '{"model_name": "m", "baseline_metrics": {',
|
|
# Strict JSON is the shared input contract: save_json never writes NaN/Infinity.
|
|
"nan_token": '{"model_name": "m", "baseline_metrics": {"a": NaN, "b": 1}, "results": []}',
|
|
"read_error": None,
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("loader", [upload, drop], ids=["upload", "drop"])
|
|
@pytest.mark.parametrize("bad", sorted(BAD_FILES))
|
|
def test_failed_load_clears_previous_results_and_shows_error(tmp_path, loader, bad):
|
|
out = _run_viewer(tmp_path, {"case": [loader(json.dumps(GOOD)), loader(BAD_FILES[bad])]})
|
|
good, failed = out["case"]
|
|
assert good["error"] is None and len(good["rows"]) == 2 and good["chart"]
|
|
assert failed["error"] is None
|
|
assert failed["table"] == "" and failed["chart"] == "", "previous results must be cleared"
|
|
assert failed["shown"]
|
|
assert failed["stats"].startswith("Could not load this file")
|
|
assert failed["html_writes"] == []
|
|
|
|
|
|
def test_strict_json_contract_matches_save_json(tmp_path):
|
|
"""save_json writes strict JSON; non-finite values become null, never NaN tokens."""
|
|
report = AblationReport(model_name="m")
|
|
report.add_baseline({"a": float("nan"), "b": 1.0})
|
|
report.add_result(AblationResult("s", "c", "d", {"a": float("inf"), "b": 2.0}))
|
|
path = tmp_path / "results.json"
|
|
report.save_json(path)
|
|
text = path.read_text(encoding="utf-8")
|
|
assert "NaN" not in text and "Infinity" not in text
|
|
render = _render_one(tmp_path, text)
|
|
assert render["error"] is None and render["headers"][2] == "b"
|
|
|
|
|
|
def test_percent_change_matches_report(tmp_path):
|
|
"""Percent change uses |baseline| and is unavailable for a zero baseline, as in Python."""
|
|
from obliteratus.reporting.report import AblationReport as Report
|
|
|
|
negative = {"model_name": "m", "baseline_metrics": {"a": -2.0}, "results": _results({"a": -1.0})}
|
|
zero = {"model_name": "m", "baseline_metrics": {"a": 0.0}, "results": _results({"a": 1.0})}
|
|
out = _run_viewer(tmp_path, {"negative": [negative], "zero": [zero]})
|
|
assert out["negative"][0]["rows"] == [["layer_removal", "layer_0", "-1.0000", "+1.0000", "+50.0%"]]
|
|
assert out["zero"][0]["rows"] == [["layer_removal", "layer_0", "1.0000", "+1.0000", "unavailable"]]
|
|
for data, pct in ((negative, 50.0), (zero, None)):
|
|
report = Report(model_name="m")
|
|
report.add_baseline(data["baseline_metrics"])
|
|
report.add_result(AblationResult("s", "c", "d", data["results"][0]["metrics"]))
|
|
assert report._measured_changes()[0][1]["a"][1] == pct
|
|
|
|
|
|
@pytest.mark.parametrize("order", [("b", "a"), ("a", "b"), ("2", "b", "10", "a"), ("y", "10", "x")])
|
|
def test_live_run_replay_and_viewer_plot_the_same_fallback(tmp_path, monkeypatch, order):
|
|
"""run -> save_json -> obliteratus report -> viewer agree on the fallback metric."""
|
|
from types import SimpleNamespace
|
|
|
|
from obliteratus import cli
|
|
|
|
plotted = []
|
|
real = AblationReport.plot_impact
|
|
|
|
def spy(self, metric=None, output_path=None, **kwargs):
|
|
plotted.append(metric)
|
|
return real(self, metric=metric, output_path=output_path, **kwargs)
|
|
|
|
monkeypatch.setattr(AblationReport, "plot_impact", spy)
|
|
live = AblationReport(model_name="m", primary_metric="z")
|
|
live.add_baseline({name: 1.0 for name in order} | {"z": 1.0})
|
|
live.add_result(AblationResult("s", "c", "d", {name: 2.0 for name in order} | {"z": None}))
|
|
live_metric = live.save_plots(tmp_path / "live").impact_metric
|
|
live.save_json(tmp_path / "results.json")
|
|
cli._cmd_report(SimpleNamespace(results_json=str(tmp_path / "results.json"),
|
|
output_dir=str(tmp_path / "replay")))
|
|
text = (tmp_path / "results.json").read_text(encoding="utf-8")
|
|
viewer = _render_one(tmp_path, text)["headers"][2]
|
|
assert plotted == [live_metric, live_metric]
|
|
assert viewer == live_metric == min(order)
|
|
|
|
|
|
GOOD_A = {**GOOD, "results": [{**GOOD["results"][0], "component": "from_A"}]}
|
|
GOOD_B = {**GOOD, "results": [{**GOOD["results"][0], "component": "from_B"}]}
|
|
|
|
|
|
@pytest.mark.parametrize("first,second", [("upload", "upload"), ("drop", "drop"), ("upload", "drop"),
|
|
("drop", "upload")])
|
|
def test_out_of_order_reads_keep_the_latest_load(tmp_path, first, second):
|
|
a_ok, b_ok = json.dumps(GOOD_A), json.dumps(GOOD_B)
|
|
seqs = {
|
|
"success_after_success": [
|
|
{"start": first, "id": "A", "text": a_ok}, {"start": second, "id": "B", "text": b_ok},
|
|
{"finish": "B"}, {"finish": "A"}],
|
|
"success_after_error": [
|
|
{"start": first, "id": "A", "text": a_ok}, {"start": second, "id": "B", "fail": True},
|
|
{"finish": "B"}, {"finish": "A"}],
|
|
"parse_error_after_success": [
|
|
{"start": first, "id": "A", "text": "{bad"}, {"start": second, "id": "B", "text": b_ok},
|
|
{"finish": "B"}, {"finish": "A"}],
|
|
"read_error_after_success": [
|
|
{"start": first, "id": "A", "fail": True}, {"start": second, "id": "B", "text": b_ok},
|
|
{"finish": "B"}, {"finish": "A"}],
|
|
"in_order": [
|
|
{"start": first, "id": "A", "text": a_ok}, {"finish": "A"},
|
|
{"start": second, "id": "B", "text": b_ok}, {"finish": "B"}],
|
|
"demo_after_pending_read": [
|
|
{"start": first, "id": "A", "text": a_ok}, {"start": "demo"}, {"finish": "A"}],
|
|
}
|
|
out = _run_harness(tmp_path, {}, sequences=seqs)["sequences"]
|
|
for name in ("success_after_success", "parse_error_after_success", "read_error_after_success",
|
|
"in_order"):
|
|
assert out[name]["components"] == ["from_B"], name
|
|
assert out["success_after_error"]["components"] == []
|
|
assert out["success_after_error"]["stats"].startswith("Could not load this file")
|
|
assert "from_A" not in out["demo_after_pending_read"]["components"]
|
|
assert out["demo_after_pending_read"]["components"][0].startswith("layer_")
|
|
assert out["success_after_success"]["aborts"] == ["A"]
|
|
|
|
|
|
def test_stale_read_error_after_demo_load_keeps_the_demo(tmp_path):
|
|
"""Latest-load-wins reads (#618; coverage gap noted in #650): a read error that
|
|
arrives after a demo load must not replace the demo."""
|
|
seqs = {
|
|
f"{start}_error_then_demo": [
|
|
{"start": start, "id": "A", "fail": True}, {"start": "demo"}, {"finish": "A"}]
|
|
for start in ("upload", "drop")
|
|
}
|
|
out = _run_harness(tmp_path, {}, sequences=seqs)["sequences"]
|
|
for name, seq in out.items():
|
|
assert not seq["stats"].startswith("Could not load this file"), name
|
|
assert seq["components"] and seq["components"][0].startswith("layer_"), name
|
|
assert seq["aborts"] == ["A"], name
|
|
|
|
|
|
@pytest.mark.parametrize("start", ["upload", "drop"])
|
|
def test_read_that_throws_synchronously_shows_an_error(tmp_path, start):
|
|
"""#666 (was a #650 coverage-gap test for the #618 read logic): readAsText throwing
|
|
clears the previous results and shows an error instead of leaving them on screen,
|
|
and a later load still works."""
|
|
a_ok, b_ok = json.dumps(GOOD_A), json.dumps(GOOD_B)
|
|
seqs = {
|
|
"throw_after_success": [
|
|
{"start": start, "id": "A", "text": a_ok}, {"finish": "A"},
|
|
{"start": start, "id": "T", "throws": True}],
|
|
"throw_then_success": [
|
|
{"start": start, "id": "A", "text": a_ok}, {"finish": "A"},
|
|
{"start": start, "id": "T", "throws": True},
|
|
{"start": start, "id": "B", "text": b_ok}, {"finish": "B"}],
|
|
}
|
|
out = _run_harness(tmp_path, {}, sequences=seqs)["sequences"]
|
|
failed = out["throw_after_success"]
|
|
assert failed["thrown"] == []
|
|
assert failed["components"] == []
|
|
assert failed["stats"] == "Could not load this file: it could not be read."
|
|
assert out["throw_then_success"]["components"] == ["from_B"]
|
|
assert out["throw_then_success"]["thrown"] == []
|
|
|
|
|
|
# Exact binary ties (where toFixed rounds away from zero and Python to even), signs,
|
|
# near-ties that are not exact in binary, and ordinary values.
|
|
FORMAT_CASES = [
|
|
(12.25, 1), (-12.25, 1), (12.35, 1), (0.125, 2), (-0.125, 2), (0.375, 2), (0.03125, 4),
|
|
(-0.03125, 4), (2.5, 0), (3.5, 0), (0.5, 0), (-0.5, 0), (-2.5, 0), (1.005, 2), (0.15, 1),
|
|
(0.25, 1), (-0.25, 1), (1.0, 4), (0.0, 1), (-0.04, 1), (123456.78125, 4), (1e-7, 4),
|
|
(2.675, 2), (99.95, 1), (-99.95, 1), (1 / 3, 4), (1e15 + 0.5, 0), (9.5, 0),
|
|
]
|
|
|
|
|
|
def test_viewer_rounds_half_to_even_like_python(tmp_path):
|
|
"""#650: the viewer formats numbers as Python's format(value, ".Nf") does."""
|
|
viewer = _run_harness(tmp_path, {}, formats=FORMAT_CASES)["formats"]
|
|
assert viewer is not None, "viewer must expose toFixedHalfEven"
|
|
assert viewer == [format(value, f".{digits}f") for value, digits in FORMAT_CASES]
|
|
|
|
|
|
def test_percent_change_display_matches_the_heatmap_annotation(tmp_path):
|
|
"""#650: a 12.25% change shows as +12.2% in the viewer, as the heatmap (fmt=".1f") does."""
|
|
data = {"model_name": "m", "baseline_metrics": {"a": 400.0}, "results": _results({"a": 449.0})}
|
|
report = AblationReport(model_name="m")
|
|
report.add_baseline(data["baseline_metrics"])
|
|
report.add_result(AblationResult("s", "c", "d", data["results"][0]["metrics"]))
|
|
pct = report._measured_changes()[0][1]["a"][1]
|
|
assert pct == 12.25
|
|
render = _render_one(tmp_path, data)
|
|
assert render["rows"] == [["layer_removal", "layer_0", "449.0000", "+49.0000",
|
|
"+" + format(pct, ".1f") + "%"]]
|
|
assert render["rows"][0][4] == "+12.2%"
|
|
# Stats use two digits: 0.125 shows as 0.12, as Python formats it.
|
|
small = {"model_name": "m", "baseline_metrics": {"a": 0.125}, "results": _results({"a": 1.0})}
|
|
assert _render_one(tmp_path, small)["stats"].startswith("0.12Baseline a")
|
|
|
|
|
|
# Values Python formats specially: negative zero keeps its sign, and non-finite
|
|
# values (sent by name; JSON has no Infinity) print as inf/nan.
|
|
SPECIAL_FORMAT_CASES = [
|
|
(-0.0, 2), (-0.0, 4), (0.0, 2), ("Infinity", 2), ("-Infinity", 4), ("NaN", 1),
|
|
(-0.001, 2), (-0.125, 2),
|
|
]
|
|
|
|
|
|
def test_viewer_formats_negative_zero_and_non_finite_like_python(tmp_path):
|
|
"""#666: -0.0 shows as -0.00 and infinity as inf, as format(value, ".Nf") prints."""
|
|
viewer = _run_harness(tmp_path, {}, formats=SPECIAL_FORMAT_CASES)["formats"]
|
|
assert viewer == [format(float(value), f".{digits}f") for value, digits in SPECIAL_FORMAT_CASES]
|
|
|
|
|
|
def test_viewer_signs_negative_zero_and_infinite_changes_like_python(tmp_path):
|
|
"""#666: a -0.0 change shows as -0.0000 (not +-0.0000), an overflowing one as +inf."""
|
|
zero = {"model_name": "m", "baseline_metrics": {"a": 0.0}, "results": _results({"a": -0.0})}
|
|
huge = {"model_name": "m", "baseline_metrics": {"a": -1e308}, "results": _results({"a": 1e308})}
|
|
out = _run_viewer(tmp_path, {"zero": [zero], "huge": [huge]})
|
|
delta = -0.0 - 0.0
|
|
assert out["zero"][0]["rows"] == [
|
|
["layer_removal", "layer_0", format(-0.0, ".4f"), format(delta, "+.4f"), "unavailable"]]
|
|
assert out["zero"][0]["rows"][0][2:4] == ["-0.0000", "-0.0000"]
|
|
big_delta = 1e308 - -1e308
|
|
big_pct = big_delta / abs(-1e308) * 100
|
|
# Values of 1e21 or more are exempt (JS prints exponent notation); the change columns match.
|
|
assert out["huge"][0]["rows"][0][3:] == [format(big_delta, "+.4f"), format(big_pct, "+.1f") + "%"]
|
|
assert out["huge"][0]["rows"][0][3:] == ["+inf", "+inf%"]
|
|
|
|
|
|
def test_viewer_formatting_survives_engines_without_toFixed_100(tmp_path):
|
|
"""#666: where toFixed(100) throws (pre-ES2020), formatting falls back to toFixed."""
|
|
cases = FORMAT_CASES + SPECIAL_FORMAT_CASES
|
|
out = _run_harness(tmp_path, {}, formats=cases)
|
|
assert out["legacy_formats"] is not None, "viewer must expose toFixedHalfEven"
|
|
assert not any(str(v).startswith("threw") for v in out["legacy_formats"]), out["legacy_formats"]
|
|
python = [format(float(value), f".{digits}f") for value, digits in cases]
|
|
for legacy, native, expected in zip(out["legacy_formats"], out["native_formats"], python):
|
|
# Either Python's half-even result, or plain toFixed when the probe is unavailable.
|
|
assert legacy in (expected, native), (legacy, native, expected)
|
|
assert out["legacy_render"]["error"] is None
|
|
assert out["legacy_render"]["rows"] > 1
|