Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(valuation): preserve literal text in workbook exports (#1949)

Rudy Mizrahi Celekli349b8d335bff
diff --git a/agent/src/quantlib/valuation/artifact.py b/agent/src/quantlib/valuation/artifact.py
index 772a5d8e2..df6432289 100644
--- a/agent/src/quantlib/valuation/artifact.py
+++ b/agent/src/quantlib/valuation/artifact.py
@@ -1008,6 +1008,16 @@ def diff_artifacts(baseline: ModelArtifact, updated: ModelArtifact) -> ArtifactD
# ---------------------------------------------------------------------------
+def _save_value_workbook(wb: Workbook, path: Path) -> None:
+ """Preserve text literally: these exports contain values, not formulas."""
+ for sheet in wb:
+ for row in sheet:
+ for cell in row:
+ if isinstance(cell.value, str):
+ cell.data_type = "s"
+ wb.save(str(path))
+
+
def _bold_row(ws: Worksheet, row: int = 1) -> None:
"""Bold every cell in one worksheet row (typically a header row)."""
for cell in ws[row]:
@@ -1205,7 +1215,7 @@ def export_dcf_workbook(
out_path = Path(path)
out_path.parent.mkdir(parents=True, exist_ok=True)
- wb.save(str(out_path))
+ _save_value_workbook(wb, out_path)
return out_path
@@ -1325,7 +1335,7 @@ def export_comps_workbook(artifact: ModelArtifact, path: str | Path) -> Path:
out_path = Path(path)
out_path.parent.mkdir(parents=True, exist_ok=True)
- wb.save(str(out_path))
+ _save_value_workbook(wb, out_path)
return out_path
@@ -1442,7 +1452,7 @@ def export_three_statement_workbook(artifact: ModelArtifact, path: str | Path) -
out_path = Path(path)
out_path.parent.mkdir(parents=True, exist_ok=True)
- wb.save(str(out_path))
+ _save_value_workbook(wb, out_path)
return out_path
diff --git a/agent/tests/quantlib/valuation/test_artifact.py b/agent/tests/quantlib/valuation/test_artifact.py
index 87c18d67e..3a5c80942 100644
--- a/agent/tests/quantlib/valuation/test_artifact.py
+++ b/agent/tests/quantlib/valuation/test_artifact.py
@@ -930,3 +930,66 @@ class TestModelArtifactConstruction:
capital_structure_basis="target", discounting_convention="mid_year",
terminal_value_method="perpetuity_growth",
)
+
[email protected]("text", ["= 2% inflation + 1% real growth", "#N/A"])
+def test_dcf_workbook_preserves_assumption_text(tmp_path, text):
+ """An exported justification is literal provenance, not a spreadsheet expression."""
+ import xml.etree.ElementTree as ET
+ from zipfile import ZipFile
+
+ inputs = _dcf_inputs()
+ inputs["terminal_growth"] = Assumption(
+ "terminal_growth", 0.03, basis=text, source=text
+ )
+ artifact = _build_dcf_artifact(inputs)
+ path = export_dcf_workbook(
+ artifact,
+ tmp_path / "valuation.xlsx",
+ sensitivity_grid=pd.DataFrame([[42.0]], index=[0.1], columns=[0.03]),
+ )
+ with ZipFile(path) as archive:
+ xml = ET.fromstring(archive.read("xl/worksheets/sheet5.xml"))
+ ns = {"s": "http://schemas.openxmlformats.org/spreadsheetml/2006/main"}
+ for coordinate in ("D9", "E9"):
+ cell = xml.find(f".//s:c[@r='{coordinate}']", ns)
+ assert cell is not None
+ assert cell.get("t") == "inlineStr"
+ assert cell.find("s:f", ns) is None
+ assert cell.find("s:is/s:t", ns).text == text
+ for data_only in (False, True):
+ workbook = openpyxl.load_workbook(path, data_only=data_only)
+ try:
+ sheet = workbook["Assumptions & Data Quality"]
+ assert sheet["D9"].value == text
+ assert sheet["E9"].value == text
+ assert sheet["C9"].value == 0.03
+ assert sheet["C9"].data_type == "n"
+ assert sheet["D10"].value == "Median EV/EBITDA of the comparable peer set"
+ assert workbook["Sensitivity"]["B2"].value == 42.0
+ finally:
+ workbook.close()
+
+
[email protected]("name", ["= Comparable Holdings", "#N/A"])
+def test_comps_workbook_preserves_peer_names(tmp_path, name):
+ from dataclasses import replace
+
+ target = _comps_target()
+ peers = _comps_peers()
+ peers[0] = replace(peers[0], name=name)
+ result = run_comps(target, peers, calendarisation_policy="ltm")
+ artifact = build_comps_artifact(
+ target=target,
+ peers=peers,
+ result=result,
+ calendarisation_policy="ltm",
+ generated_at=FIXED_TIME,
+ )
+ path = export_comps_workbook(artifact, tmp_path / "comps.xlsx")
+ for data_only in (False, True):
+ workbook = openpyxl.load_workbook(path, data_only=data_only)
+ try:
+ assert workbook["Peer Detail"]["A2"].value == name
+ assert workbook["Peer Detail"]["A2"].data_type == "s"
+ finally:
+ workbook.close()