"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(factors): surface survivorship_bias disclosure in the HTML bench report
Clara Bonbf2864c050d2
diff --git a/agent/src/tools/alpha_bench_tool.py b/agent/src/tools/alpha_bench_tool.pyindex a5cf035ac..582f5fb26 100644--- a/agent/src/tools/alpha_bench_tool.py+++ b/agent/src/tools/alpha_bench_tool.py@@ -744,6 +744,8 @@ th { background: #f0f0f0; }.meta { color: #666; font-size: .9em; margin-bottom: 1.5em; }.formula { font-family: monospace; background: #f4f4f4; padding: .25em .5em; }.skipped { color: #a33; font-size: .9em; }+.bias-warning { background: #fff4e5; border: 1px solid #e0a800; border-left-width: 4px;+ color: #7a4d00; padding: .75em 1em; margin-bottom: 1.5em; font-size: .95em; }"""_JINJA_TEMPLATE = """<!doctype html>@@ -758,6 +760,13 @@ _JINJA_TEMPLATE = """<!doctype html>Generated {{ generated_at }} · Universe {{ universe }} ·Period {{ period }} · {{ n_alphas_tested }} tested, {{ n_skipped }} skipped</div>+{% if meta and meta.survivorship_bias %}+<div class="bias-warning">+ <strong>Survivorship bias:</strong> universe membership is the current constituent+ list{% if meta.constituent_source %} ({{ meta.constituent_source }}{% if meta.constituent_source_date %}, as of {{ meta.constituent_source_date }}{% endif %}){% endif %},+ so delisted and removed names are absent. IC statistics in this report are biased upward.+</div>+{% endif %}<h2>Top {{ top|length }} by IR</h2><table>@@ -851,6 +860,22 @@ def _render_html_manual(ctx: dict[str, Any]) -> str:_esc(ctx["period"]),f" · {int(ctx['n_alphas_tested'])} tested, {int(ctx['n_skipped'])} skipped","</div>",+ ]+ _meta = ctx.get("meta") or {}+ if _meta.get("survivorship_bias"):+ source = _meta.get("constituent_source")+ as_of = _meta.get("constituent_source_date")+ provenance = ""+ if source:+ provenance = f" ({_esc(source)}"+ provenance += f", as of {_esc(as_of)})" if as_of else ")"+ parts.append(+ "<div class=\"bias-warning\"><strong>Survivorship bias:</strong> universe "+ f"membership is the current constituent list{provenance}, so delisted and "+ "removed names are absent. IC statistics in this report are biased upward."+ "</div>"+ )+ parts += [f"<h2>Top {len(ctx['top'])} by IR</h2><table>","<tr><th>#</th><th>Alpha ID</th><th>Zoo</th><th>Theme</th>""<th>IC mean</th><th>IC std</th><th>IR</th><th>IC+ ratio</th><th>N</th></tr>",diff --git a/agent/tests/test_alpha_bench_meta_survivorship.py b/agent/tests/test_alpha_bench_meta_survivorship.pyindex 2fb778b65..ef6bea61f 100644--- a/agent/tests/test_alpha_bench_meta_survivorship.py+++ b/agent/tests/test_alpha_bench_meta_survivorship.py@@ -10,7 +10,9 @@ that key, so the CLI/HTML path silently dropped the disclosure.from __future__ import annotationsimport argparse+import htmlimport json+from pathlib import Pathimport pytest@@ -136,3 +138,89 @@ def test_meta_key_absent_from_envelope_and_context_when_loader_has_none(capsys,envelope = _envelope(cap.out)assert "meta" not in envelopeassert "meta" not in _captured_html_context+++# ---------------------------------------------------------------------------+# Rendered-output tests.+#+# The context-level tests above assert on the dict handed to the renderer, with+# _render_html stubbed out. That leaves the renderers themselves unreached: the+# disclosure could be (and was) dropped inside _render_html while every test+# above stayed green. These tests assert on the rendered HTML string instead.+# ---------------------------------------------------------------------------++import src.tools.alpha_bench_tool as tool++_RENDER_CTX = {+ "csp": tool._CSP,+ "css": tool._REPORT_CSS,+ "generated_at": "2026-01-01T00:00:00+00:00",+ "universe": "sp500",+ "period": "2020-2025",+ "n_alphas_tested": 1,+ "n_skipped": 0,+ "top": [+ {+ "id": "alpha001",+ "zoo": "alpha101",+ "theme": ["momentum"],+ "ic_mean": 0.05,+ "ic_std": 0.01,+ "ir": 0.9,+ "ic_positive_ratio": 0.7,+ "ic_count": 100,+ "formula_latex": "x",+ }+ ],+ "failures": [],+ "strict": False,+}+++def _ctx(meta=None):+ ctx = dict(_RENDER_CTX)+ ctx["top"] = [dict(_RENDER_CTX["top"][0])]+ if meta is not None:+ ctx["meta"] = meta+ return ctx++[email protected]("render", [tool._render_html, tool._render_html_manual])+def test_renderers_emit_survivorship_disclosure(render):+ html_out = render(_ctx(dict(_UNIVERSE_META)))+ assert "Survivorship bias" in html_out+ assert "biased upward" in html_out+ # the provenance the loader recorded reaches the reader, not just the flag+ # (escaped: the sp500 source string contains "S&P", which must render as S&P)+ assert html.escape(_UNIVERSE_META["constituent_source"]) in html_out+ assert _UNIVERSE_META["constituent_source_date"] in html_out++[email protected]("render", [tool._render_html, tool._render_html_manual])+def test_renderers_omit_disclosure_when_no_meta(render):+ assert "Survivorship bias" not in render(_ctx())++[email protected]("render", [tool._render_html, tool._render_html_manual])+def test_renderers_omit_disclosure_when_flag_false(render):+ meta = dict(_UNIVERSE_META, survivorship_bias=False)+ assert "Survivorship bias" not in render(_ctx(meta))++[email protected]("render", [tool._render_html, tool._render_html_manual])+def test_renderers_escape_untrusted_provenance(render):+ meta = dict(_UNIVERSE_META, constituent_source="<script>alert(1)</script>")+ html_out = render(_ctx(meta))+ assert "<script>alert(1)</script>" not in html_out+ assert "<script>" in html_out+++def test_written_report_file_carries_the_disclosure(capsys, monkeypatch, tmp_path, _reg):+ """End-to-end: the artifact a user keeps, with no renderer stubbed."""+ monkeypatch.setattr(tool, "_default_output_dir", lambda: tmp_path)+ rc, cap = _run(monkeypatch, capsys, _ns(), _result(meta=dict(_UNIVERSE_META)))+ assert rc == 0+ report_path = _envelope(cap.out)["report_path"]+ written = Path(report_path).read_text(encoding="utf-8")+ assert "Survivorship bias" in written+ assert "biased upward" in written