Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(api): backfill adapter defaults into channel config values

shadowinlife31b667d98d74
diff --git a/agent/src/api/channels_config_routes.py b/agent/src/api/channels_config_routes.py
index 0ac757d4d..8c92ec2c1 100644
--- a/agent/src/api/channels_config_routes.py
+++ b/agent/src/api/channels_config_routes.py
@@ -125,6 +125,23 @@ def _stored_section(name: str) -> dict[str, Any]:
return dict(section) if isinstance(section, dict) else {}
+def _effective_section(name: str, section: dict[str, Any]) -> dict[str, Any]:
+ """Return the stored section backfilled with adapter defaults.
+
+ A never-configured channel has no on-disk section; without defaults the
+ generic form seeds empty strings and typed fields (``int``, ``Literal``)
+ reject the untouched form on Test/Save. Secret masking is unaffected:
+ defaults are empty, so ``secrets`` still reflects only the stored state.
+ """
+ try:
+ defaults = load_channel_class(name).default_config()
+ except Exception: # noqa: BLE001 - an unloadable adapter keeps the raw section
+ return section
+ if not isinstance(defaults, dict):
+ return section
+ return {**defaults, **section}
+
+
def _patch_of(name: str, body: dict[str, Any]) -> dict[str, Any]:
"""Filter a body patch: an empty string on a secret key keeps the stored value."""
return {
@@ -162,7 +179,7 @@ def _live_status(name: str) -> dict[str, Any]:
def _channel_entry(name: str, section: dict[str, Any], status_map: dict[str, Any]) -> dict[str, Any]:
"""Build one channel's GET-shape entry; secrets are masked, never returned."""
- values, secrets = split_values_secrets(name, section)
+ values, secrets = split_values_secrets(name, _effective_section(name, section))
available = bool(status_map.get("available", False))
return {
"display_name": str(status_map.get("display_name") or name),
diff --git a/agent/tests/test_channels_config_api.py b/agent/tests/test_channels_config_api.py
index ec275569d..3248f5944 100644
--- a/agent/tests/test_channels_config_api.py
+++ b/agent/tests/test_channels_config_api.py
@@ -233,6 +233,71 @@ def test_get_reports_runtime_running_from_live_status(tmp_path: Path, monkeypatc
assert payload["runtime_running"] is True
+def test_get_backfills_adapter_defaults_for_unconfigured_channel(
+ tmp_path: Path, monkeypatch
+) -> None:
+ """An empty on-disk section still yields the adapter's default values.
+
+ Regression (found by live QQ E2E): a never-configured channel returned
+ empty ``values``, so the generic form seeded "" into typed fields
+ (``msg_format`` Literal, ``download_chunk_size`` int) and any Test/Save
+ died on validation_error before the fix.
+ """
+ client, _ = _client(tmp_path, monkeypatch, channels={})
+
+ qq = client.get("/channels/config").json()["channels"]["qq"]
+
+ assert qq["values"]["msg_format"] == "plain"
+ assert qq["values"]["download_chunk_size"] == 262144
+ assert qq["values"]["download_max_bytes"] == 209715200
+ assert qq["values"]["app_id"] == ""
+ assert qq["values"]["enabled"] is False
+ # Secret masking still reflects stored state only: nothing is configured.
+ assert qq["secrets"]["secret"] == {"set": False, "masked": ""}
+
+
+def test_pristine_form_roundtrip_validates_for_typed_fields(
+ tmp_path: Path, monkeypatch
+) -> None:
+ """GET values echoed back as a form patch must pass Test validation.
+
+ Mirrors the frontend's ``buildPatch`` for a pristine (never-configured)
+ QQ form: every non-secret field is sent back as the string/list/bool the
+ generic widgets hold. Before the defaults backfill this produced
+ ``validation_error: msg_format, download_chunk_size, download_max_bytes``.
+ """
+ client, _ = _client(tmp_path, monkeypatch, channels={})
+ entry = client.get("/channels/config").json()["channels"]["qq"]
+
+ 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["app_id"] = "1905653354"
+ patch["secret"] = "form-typed-secret-xyz987654321"
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(200, json={"access_token": "probe-token-do-not-leak"})
+
+ requests = _inject_mock_transport(monkeypatch, handler)
+
+ response = client.post("/channels/qq/test", json={"config": patch})
+
+ assert response.status_code == 200
+ body = response.json()
+ assert body["ok"] is True, body
+ assert body["code"] == "ok"
+ assert len(requests) == 1
+ assert "form-typed-secret-xyz987654321" not in response.text
+
+
def test_display_config_path_relativizes_home() -> None:
home = Path.home()