Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): normalize blank nullable config fields

zeus2298ee4016408d5
diff --git a/agent/src/api/channels_config_routes.py b/agent/src/api/channels_config_routes.py
index c8b841509..d91681e44 100644
--- a/agent/src/api/channels_config_routes.py
+++ b/agent/src/api/channels_config_routes.py
@@ -165,6 +165,26 @@ def _patch_of(name: str, body: dict[str, Any]) -> dict[str, Any]:
}
+def _normalize_nullable_blanks(name: str, patch: dict[str, Any]) -> dict[str, Any]:
+ """Map blank form values back to None for nullable config defaults.
+
+ The generic web form renders text-like None values as an empty string.
+ Without this normalization, echoing a pristine form back to the API can
+ make nullable Literal fields invalid (for example Email post_action).
+ """
+ try:
+ defaults = load_channel_class(name).default_config()
+ except Exception: # noqa: BLE001 - validation reports unloadable adapters later
+ return patch
+ if not isinstance(defaults, dict):
+ return patch
+ return {
+ key: None
+ if isinstance(value, str) and value == "" and defaults.get(key) is None
+ else value
+ for key, value in patch.items()
+ }
+
def _clear_flags(extra: dict[str, Any] | None) -> list[str]:
"""Return the ``clear_<field>`` targets from a request's extra keys."""
return [
@@ -336,7 +356,7 @@ async def _hot_apply(name: str, section: dict[str, Any] | None) -> str:
async def _apply_update(name: str, payload: ChannelConfigUpdateRequest) -> dict[str, Any]:
"""Validate → optional enable-transition probe → write → hot apply."""
stored = _stored_section(name)
- patch = _patch_of(name, payload.config)
+ patch = _normalize_nullable_blanks(name, _patch_of(name, payload.config))
clears = _clear_flags(payload.model_extra)
merged = {**stored, **patch}
for key in clears:
@@ -386,7 +406,7 @@ async def _apply_update(name: str, payload: ChannelConfigUpdateRequest) -> dict[
async def _run_test(name: str, body: dict[str, Any] | None, clears: list[str]) -> dict[str, Any]:
"""Probe credentials on an ephemeral instance; never persists anything."""
stored = _stored_section(name)
- patch = _patch_of(name, body or {})
+ patch = _normalize_nullable_blanks(name, _patch_of(name, body or {}))
merged = {**stored, **patch}
for key in clears:
merged.pop(key, None)
diff --git a/agent/tests/test_channels_config_api.py b/agent/tests/test_channels_config_api.py
index 2569b7cd0..a37751e51 100644
--- a/agent/tests/test_channels_config_api.py
+++ b/agent/tests/test_channels_config_api.py
@@ -402,6 +402,39 @@ def test_feishu_pristine_form_roundtrip_validates_for_typed_fields(
assert "probe-token-do-not-leak" not in response.text
+def test_email_pristine_form_roundtrip_normalizes_nullable_text_fields(
+ tmp_path: Path, monkeypatch
+) -> None:
+ """The generic form must not turn nullable Email fields into invalid blanks."""
+ client, path = _client(
+ tmp_path, monkeypatch, channels={"email": _email_section()}
+ )
+ entry = client.get("/channels/config").json()["channels"]["email"]
+
+ patch: dict[str, Any] = {}
+ for field in entry["fields"]:
+ if field["secret"]:
+ continue
+ value = entry["values"].get(field["key"])
+ if field["type"] == "list":
+ patch[field["key"]] = value if isinstance(value, list) else []
+ elif field["type"] == "bool":
+ patch[field["key"]] = bool(value)
+ else:
+ patch[field["key"]] = "" if value is None else str(value)
+
+ patch["max_attachments_per_email"] = "4"
+
+ response = client.put("/channels/config/email", json={"config": patch})
+
+ assert response.status_code == 200, response.text
+ on_disk = json.loads(path.read_text(encoding="utf-8"))["channels"]["email"]
+ assert on_disk["max_attachments_per_email"] == "4"
+ assert on_disk["post_action"] is None
+ assert on_disk["post_action_move_mailbox"] is None
+ assert EMAIL_IMAP_PASSWORD not in response.text
+ assert EMAIL_SMTP_PASSWORD not in response.text
+
def test_display_config_path_relativizes_home() -> None:
home = Path.home()