Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(registry): enforce the NaN contract on declared dependencies (#1376)

cgycorey2f288662e055
diff --git a/agent/src/factors/registry.py b/agent/src/factors/registry.py
index 271a4e2dd..1e33058d7 100644
--- a/agent/src/factors/registry.py
+++ b/agent/src/factors/registry.py
@@ -352,6 +352,19 @@ class Registry:
except Exception as exc: # noqa: BLE001 — isolate compute failure
raise RegistryError(f"{alpha_id}: compute() raised: {exc}") from exc
+ # Enforce NaN contract: any bar where a declared dependency is missing
+ # must produce NaN in the output, regardless of the alpha's internal logic.
+ ref = panel.get("close")
+ if ref is not None:
+ mask = pd.DataFrame(True, index=ref.index, columns=ref.columns)
+ for col in meta.get("columns_required", []):
+ mask = mask & panel[col].notna()
+ for col in meta.get("extras_required", []):
+ mask = mask & panel[col].notna()
+ if meta.get("requires_sector"):
+ mask = mask & panel["sector"].notna()
+ if isinstance(result, pd.DataFrame):
+ result = result.where(mask)
return self._validate_output(alpha_id, result, panel)
def _load_module(self, alpha: Alpha) -> ModuleType:
diff --git a/agent/tests/factors/test_registry.py b/agent/tests/factors/test_registry.py
index 2f9bacab6..ec4560384 100644
--- a/agent/tests/factors/test_registry.py
+++ b/agent/tests/factors/test_registry.py
@@ -254,3 +254,17 @@ def test_export_manifest_shape(mini_zoo: Path) -> None:
assert "generated_at" in m
assert m["zoos"][0]["zoo_id"] == "fakezoo"
assert len(m["zoos"][0]["alphas"]) == 4
+
+
+def test_registry_masks_nan_on_declared_dependencies(mini_zoo: Path) -> None:
+ reg = Registry(zoo_root=mini_zoo)
+ panel = _panel()
+ # NaN in a required column should propagate to output
+ panel["close"].iloc[2, 0] = np.nan
+ out = reg.compute("fakezoo_001", panel)
+ assert np.isnan(out.iloc[2, 0])
+ # Also test with NaN in another required column
+ panel = _panel()
+ panel["open"].iloc[1, 1] = np.nan
+ out = reg.compute("fakezoo_001", panel)
+ assert np.isnan(out.iloc[1, 1])