fix: strip quotes, comments, whitespace and export prefixes in .env loader

RA
3h ago
+166-2

What

load_environment() in jev_ultrafast/demo.py parsed .env with a bare split("=", 1) and os.environ.setdefault(key, value), so standard dotenv conventions silently corrupted values:

  • TYPESAFE_API_KEY="tsk-abc123" stored the key with literal quotes, so requests send Authorization: Bearer "tsk-abc123" and the provider answers 401.
  • TEXT_MODEL_API_KEY=sk-xyz # my key stored sk-xyz # my key.
  • TEXT_MODEL= mercury-2.5 kept the surrounding whitespace.
  • export TYPESAFE_MODEL=jev-latest created a variable literally named export TYPESAFE_MODEL; the real key stayed unset.

Fix

Each line now goes through parse_env_line(), which trims the line, drops a leading export , trims the key and value, cuts # comments that sit outside quotes, and strips one layer of matching surrounding quotes. Comment/blank lines and the existing setdefault-only-if-unset semantics are unchanged. scripts/smoke.py imports the same function, so it is fixed too.

Tests

tests/test_env_loader.py — an offline pytest contract (same style as tests/test_agent.py) that compiles the real load_environment() source verbatim out of demo.py and pins the issue's examples: quoted values, comments after quoted values, comments inside quotes, export lines, unquoted values, and simple unchanged cases.

$ python3 -m pytest tests/test_env_loader.py -q
8 passed in 0.15s

On unfixed main (fix stashed), the same file fails the six cases the issue reports:

FAILED test_issue_examples
FAILED test_export_prefix_creates_no_export_variable
FAILED test_quoted_value_keeps_inner_spaces_and_hashes
FAILED test_comment_after_quoted_value
FAILED test_single_quoted_value
FAILED test_unquoted_values_are_trimmed_only
6 failed, 2 passed in 0.19s

Fixes #217


Summary by cubic

Fixes the .env loader in load_environment() so standard dotenv conventions no longer corrupt values. Previously literal quotes were kept in values (breaking bearer-token auth), inline comments and surrounding whitespace became part of the value, and export KEY=... lines created a variable literally named export KEY. Values now go through parse_env_line(), which trims the line, drops a leading export followed by any whitespace, strips one layer of matching surrounding quotes, and cuts # comments that sit outside quotes and follow whitespace; quoted values keep inner spaces and hashes, and backslash-escaped quotes don't close a double-quoted value. setdefault (existing env wins) semantics are unchanged, and scripts/smoke.py is fixed automatically since it imports the same function.

Tests

  • Adds tests/test_env_loader.py, an offline pytest contract compiling the real loader source from demo.py; it pins the #217 examples plus export-whitespace, escaped-quote, and comment-prefix edge cases.