Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(api): unquote dotenv values before stripping hash comments

santhrealbe7cf96383bb
diff --git a/agent/src/api/helpers.py b/agent/src/api/helpers.py
index fbec14dd0..7d6ad539f 100644
--- a/agent/src/api/helpers.py
+++ b/agent/src/api/helpers.py
@@ -133,10 +133,11 @@ def _ensure_agent_env_file(path: Path | None = None) -> Path:
def _strip_env_value(value: str) -> str:
"""Remove basic dotenv quotes and inline comments."""
value = value.strip()
+ # Unquote first so "secret # still" keeps the hash (dotenv quotes shield comments).
+ if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
+ return value[1:-1].strip()
if " #" in value:
value = value.split(" #", 1)[0].rstrip()
- if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
- value = value[1:-1]
return value.strip()
diff --git a/agent/tests/test_api_infrastructure.py b/agent/tests/test_api_infrastructure.py
index f1fdf87aa..b20ac045b 100644
--- a/agent/tests/test_api_infrastructure.py
+++ b/agent/tests/test_api_infrastructure.py
@@ -256,6 +256,24 @@ def test_strip_env_value_inline_comment():
assert helpers._strip_env_value("value # comment") == "value"
+def test_strip_env_value_quoted_hash_preserves_value():
+ """Quoted dotenv values may contain ' #'; do not treat as comment."""
+ assert helpers._strip_env_value('"secret # still-part-of-value"') == "secret # still-part-of-value"
+ assert helpers._strip_env_value("'secret # still-part-of-value'") == "secret # still-part-of-value"
+
+
+def test_read_write_env_quoted_hash_roundtrip(tmp_path):
+ env_file = tmp_path / ".env"
+ secret = "secret # still-part-of-value"
+ helpers._write_env_values(env_file, {"K": secret})
+ assert helpers._read_env_values(env_file)["K"] == secret
+ # Re-read the formatted line directly through strip
+ raw = env_file.read_text(encoding="utf-8")
+ assert ' #' in raw
+ line = [ln for ln in raw.splitlines() if ln.startswith("K=")][0]
+ assert helpers._strip_env_value(line.split("=", 1)[1]) == secret
+
+
# ============================================================================
# state._get_session_service writeback
# ============================================================================