Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(env): read export-prefixed dotenv keys consistently (#1152)

OSideMediaa5eadeb9cf8b
diff --git a/CONFIGURATION.md b/CONFIGURATION.md
index e1b33967..ac1a3a5a 100644
--- a/CONFIGURATION.md
+++ b/CONFIGURATION.md
@@ -116,7 +116,7 @@ The skill reads keys from a `.env` file. Two locations are supported:
Override the global location with `LAST30DAYS_CONFIG_DIR=/path` (or `LAST30DAYS_CONFIG_DIR=""` for no-config mode). File permissions should be `600` on POSIX hosts - the engine warns on every run if they aren't.
-**File syntax.** One `KEY=value` per line; whitespace around the key and value is trimmed. A line starting with `#` is a comment, and a `#` preceded by whitespace after an unquoted value starts a trailing comment (`RATE=1 # req/sec` stores `1`). A `#` glued to the value is literal (`TOKEN=abc#123` stores `abc#123`). Wrap a value in matching `"` or `'` to keep spaces or a `#` verbatim (`NAME="Jane # Doe"` stores `Jane # Doe`); no backslash escapes or `$VAR` expansion are processed. An empty value is ignored (the one exception is `LAST30DAYS_YT_PLAYER_CLIENT=`, where empty is a deliberate disable), so a secret can never be set to `""`.
+**File syntax.** One `KEY=value` per line; whitespace around the key and value is trimmed. A leading `export ` is accepted and ignored (`export RATE=1` sets `RATE`). A line starting with `#` is a comment, and a `#` preceded by whitespace after an unquoted value starts a trailing comment (`RATE=1 # req/sec` stores `1`). A `#` glued to the value is literal (`TOKEN=abc#123` stores `abc#123`). Wrap a value in matching `"` or `'` to keep spaces or a `#` verbatim (`NAME="Jane # Doe"` stores `Jane # Doe`); no backslash escapes or `$VAR` expansion are processed. An empty value is ignored (except for `LAST30DAYS_YT_PLAYER_CLIENT=` and `LAST30DAYS_MEMORY_DIR=`, where empty is a deliberate disable), so a secret can never be set to `""`.
The project-scoped file is useful for **intentional per-client setups**: drop a `.claude/last30days.env` into each client folder (`SCRAPECREATORS_API_KEY`, `INCLUDE_SOURCES`, `LAST30DAYS_MEMORY_DIR`, `BSKY_HANDLE`, etc), then opt in with `LAST30DAYS_TRUST_PROJECT_CONFIG=1` from your shell or `~/.config/last30days/.env`. Folder-mode hosts such as Codex desktop do not trust hidden project config by default, and discovery stops at the git root so unrelated parent folders cannot silently influence runs. An untrusted repo's `.claude/last30days.env` is not read.
diff --git a/changelog.d/930.fixed.md b/changelog.d/930.fixed.md
new file mode 100644
index 00000000..ec09a04f
--- /dev/null
+++ b/changelog.d/930.fixed.md
@@ -0,0 +1 @@
+`.env` lines written with the shell's `export` prefix (`export KEY=value`) now set `KEY`; before, they created a key literally named `export KEY` and the setting was silently ignored. The setup wizard also treats an `export KEY=...` line as the key being set, so it no longer appends a second copy.
diff --git a/skills/last30days/scripts/lib/env.py b/skills/last30days/scripts/lib/env.py
index aab035d4..23cc2db4 100644
--- a/skills/last30days/scripts/lib/env.py
+++ b/skills/last30days/scripts/lib/env.py
@@ -260,6 +260,23 @@ def _strip_inline_comment(value: str) -> str:
return value
+# ``export KEY=value`` is the shell spelling people paste into .env files, and
+# python-dotenv and docker compose accept it too. The prefix only counts when
+# whitespace and a key follow it, so a key literally named ``export`` is kept.
+_EXPORT_PREFIX = re.compile(r'export\s+')
+
+
+def env_line_key(lhs: str) -> str:
+ """Return the key named by the left-hand side of a ``KEY=value`` line.
+
+ Shared with the setup wizard's .env writers so that reading and writing
+ agree on which key a hand-written line sets.
+ """
+ key = lhs.strip()
+ match = _EXPORT_PREFIX.match(key)
+ return key[match.end():] if match else key
+
+
def load_env_file(path: Path) -> dict[str, str]:
"""Load environment variables from a file."""
env = {}
@@ -283,7 +300,7 @@ def load_env_file(path: Path) -> dict[str, str]:
continue
if '=' in line:
key, _, value = line.partition('=')
- key = key.strip()
+ key = env_line_key(key)
value = _strip_inline_comment(value).strip()
# Remove quotes if present
if value and value[0] in ('"', "'") and value[-1] == value[0]:
diff --git a/skills/last30days/scripts/lib/setup_wizard.py b/skills/last30days/scripts/lib/setup_wizard.py
index 43cf90ed..c2e17fb2 100644
--- a/skills/last30days/scripts/lib/setup_wizard.py
+++ b/skills/last30days/scripts/lib/setup_wizard.py
@@ -527,6 +527,13 @@ def _open_secret_append(path: Path):
return os.fdopen(fd, "a", encoding="utf-8")
+def _env_line_key(stripped: str) -> str:
+ """Return the key a stripped ``KEY=value`` line sets, as the loader reads it."""
+ from . import env as _env
+
+ return _env.env_line_key(stripped.split("=", 1)[0])
+
+
def _replace_env_line(env_path: Path, content: str, key_name: str, value: str) -> bool:
"""Rewrite every ``key_name=`` line of ``content`` with ``value`` as a 0o600 secret.
@@ -534,16 +541,25 @@ def _replace_env_line(env_path: Path, content: str, key_name: str, value: str) -
over the original, so the secret never has a readable window and a
crash mid-write leaves the old file intact.
"""
+ def _is_key(stripped: str) -> bool:
+ return bool(
+ stripped and not stripped.startswith("#") and "=" in stripped
+ and _env_line_key(stripped) == key_name
+ )
+
+ stripped_lines = [line.strip() for line in content.splitlines()]
+ # Keep a hand-written ``export`` -- on any of the duplicates being
+ # collapsed -- so a shell that sources the file still exports the key.
+ exported = any(
+ _is_key(s) and s.split("=", 1)[0].strip() != key_name for s in stripped_lines
+ )
new_line = f"{key_name}={_format_env_value(value)}"
+ if exported:
+ new_line = f"export {new_line}"
lines = []
replaced = False
- for line in content.splitlines():
- stripped = line.strip()
- is_key = (
- stripped and not stripped.startswith("#") and "=" in stripped
- and stripped.split("=", 1)[0].strip() == key_name
- )
- if is_key:
+ for line, stripped in zip(content.splitlines(), stripped_lines):
+ if _is_key(stripped):
if not replaced:
lines.append(new_line)
replaced = True
@@ -629,7 +645,7 @@ def write_setup_config(
for line in existing_content.splitlines():
stripped = line.strip()
if stripped and not stripped.startswith("#") and "=" in stripped:
- key = stripped.split("=", 1)[0].strip()
+ key = _env_line_key(stripped)
existing_keys.add(key)
lines_to_add = []
@@ -694,7 +710,7 @@ def write_api_key(
for line in existing_content.splitlines():
stripped = line.strip()
if stripped and not stripped.startswith("#") and "=" in stripped:
- if stripped.split("=", 1)[0].strip() == key_name:
+ if _env_line_key(stripped) == key_name:
if replace:
return _replace_env_line(env_path, existing_content, key_name, api_key)
return True # Already configured; do not duplicate
diff --git a/tests/test_env_export_prefix.py b/tests/test_env_export_prefix.py
new file mode 100644
index 00000000..156be420
--- /dev/null
+++ b/tests/test_env_export_prefix.py
@@ -0,0 +1,143 @@
+"""``export KEY=value`` lines in a .env file set KEY (#930).
+
+The loader and the setup wizard's writers must agree on which key a line
+names: if only the loader understood ``export``, the wizard would miss a
+hand-set ``export KEY=...`` line and append a duplicate that overrides it.
+"""
+
+import pytest
+
+from lib import env, setup_wizard
+
+
+def _write(tmp_path, text):
+ env_path = tmp_path / ".env"
+ env_path.write_text(text, encoding="utf-8")
+ env_path.chmod(0o600)
+ return env_path
+
+
+def _load(tmp_path, text):
+ return env.load_env_file(_write(tmp_path, text))
+
+
[email protected]("sep", [" ", "\t", " "])
+def test_export_prefix_is_dropped_from_key(tmp_path, sep):
+ assert _load(tmp_path, f"export{sep}FOO=bar\n") == {"FOO": "bar"}
+
+
+def test_export_prefix_combines_with_quotes_and_inline_comment(tmp_path):
+ loaded = _load(tmp_path, 'export NAME="Jane # Doe" # annotation\n')
+ assert loaded == {"NAME": "Jane # Doe"}
+
+
+def test_exported_settings_reach_get_config(tmp_path, monkeypatch):
+ env_path = _write(tmp_path, "export FUN_LEVEL=high\n")
+ monkeypatch.setenv("LAST30DAYS_CONFIG_DIR", str(tmp_path))
+ monkeypatch.delenv("FUN_LEVEL", raising=False)
+ monkeypatch.setattr(env, "CONFIG_DIR", tmp_path)
+ monkeypatch.setattr(env, "CONFIG_FILE", env_path)
+ monkeypatch.setattr(env, "_load_keychain", lambda *args, **kwargs: {})
+ monkeypatch.setattr(env, "_load_pass", lambda *args, **kwargs: {})
+ monkeypatch.chdir(tmp_path)
+
+ assert env.get_config()["FUN_LEVEL"] == "high"
+
+
[email protected]("value", ["$HOME", "$(printf sentinel)", "`printf sentinel`"])
+def test_export_does_not_evaluate_shell_syntax(tmp_path, value):
+ assert _load(tmp_path, f"export RAW={value}\n") == {"RAW": value}
+
+
+def test_export_preserves_empty_disable_values_but_ignores_empty_secrets(tmp_path):
+ loaded = _load(
+ tmp_path,
+ 'export LAST30DAYS_YT_PLAYER_CLIENT= # disabled\n'
+ 'export LAST30DAYS_MEMORY_DIR=""\n'
+ 'export XAI_API_KEY= # not configured\n',
+ )
+ assert loaded == {"LAST30DAYS_YT_PLAYER_CLIENT": "", "LAST30DAYS_MEMORY_DIR": ""}
+
+
[email protected]("line, key", [
+ ("export=literal\n", "export"),
+ ("export =literal\n", "export"),
+ ("exporter=1\n", "exporter"),
+ ("EXPORT FOO=1\n", "EXPORT FOO"),
+ ("set FOO=1\n", "set FOO"),
+])
+def test_export_is_only_a_prefix_when_a_key_follows(tmp_path, line, key):
+ assert list(_load(tmp_path, line)) == [key]
+
+
+def test_write_api_key_sees_export_line_as_present(tmp_path):
+ env_path = _write(tmp_path, "export SCRAPECREATORS_API_KEY=hand_set\n")
+
+ assert setup_wizard.write_api_key(env_path, "sc_new_value") is True
+
+ assert env_path.read_text() == "export SCRAPECREATORS_API_KEY=hand_set\n"
+ assert env.load_env_file(env_path)["SCRAPECREATORS_API_KEY"] == "hand_set"
+
+
+def test_replace_rewrites_export_line_and_keeps_the_prefix(tmp_path):
+ env_path = _write(tmp_path, "export X_BEARER_TOKEN=stale\nOTHER=1\n")
+
+ setup_wizard.write_api_key(env_path, "fresh", key_name="X_BEARER_TOKEN", replace=True)
+
+ # Rewritten in place, not appended, so the stale secret is gone; the
+ # prefix stays so a shell that sources the file still exports the key.
+ assert env_path.read_text() == "export X_BEARER_TOKEN=fresh\nOTHER=1\n"
+ assert env.load_env_file(env_path) == {"X_BEARER_TOKEN": "fresh", "OTHER": "1"}
+
+
+ "X_BEARER_TOKEN=old\nexport X_BEARER_TOKEN=older\n",
+ "export X_BEARER_TOKEN=old\nX_BEARER_TOKEN=older\n",
+])
+def test_replace_keeps_the_prefix_when_any_duplicate_is_exported(tmp_path, text):
+ env_path = _write(tmp_path, text)
+
+ setup_wizard.write_api_key(env_path, "fresh", key_name="X_BEARER_TOKEN", replace=True)
+
+ # The duplicates collapse to one line; dropping the prefix would stop a
+ # shell that sources the file from exporting the key it used to export.
+ assert env_path.read_text() == "export X_BEARER_TOKEN=fresh\n"
+
+
+def test_replace_does_not_add_the_prefix_to_a_plain_line(tmp_path):
+ env_path = _write(tmp_path, "X_BEARER_TOKEN =stale\n")
+
+ setup_wizard.write_api_key(env_path, "fresh", key_name="X_BEARER_TOKEN", replace=True)
+
+ assert env_path.read_text() == "X_BEARER_TOKEN=fresh\n"
+
+
+def test_write_setup_config_sees_export_line_as_present(tmp_path):
+ env_path = _write(tmp_path, "export SETUP_COMPLETE=true\n")
+
+ assert setup_wizard.write_setup_config(env_path) is True
+
+ assert env_path.read_text() == "export SETUP_COMPLETE=true\n"
+
+
+def test_write_setup_config_rotates_exported_consent_and_preserves_browser(tmp_path):
+ env_path = _write(
+ tmp_path,
+ "export SETUP_COMPLETE=true\n"
+ "export FROM_BROWSER=firefox\n"
+ "export BROWSER_CONSENT=true\n"
+ "BROWSER_CONSENT=true\n",
+ )
+
+ assert setup_wizard.write_setup_config(
+ env_path, from_browser="chrome", browser_consent=False,
+ ) is True
+
+ assert env_path.read_text() == (
+ "export SETUP_COMPLETE=true\n"
+ "export FROM_BROWSER=firefox\n"
+ "export BROWSER_CONSENT=false\n"
+ )
+ assert env.load_env_file(env_path) == {
+ "SETUP_COMPLETE": "true", "FROM_BROWSER": "firefox", "BROWSER_CONSENT": "false",
+ }