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): add LAST30DAYS_SKIP_KEYCHAIN so tests can seal the Keychain source (keychain-only port of #1050) (#1090)

Matt Van Horn41fcb4d59d49
diff --git a/CONFIGURATION.md b/CONFIGURATION.md
index 69602daa..294a3f9c 100644
--- a/CONFIGURATION.md
+++ b/CONFIGURATION.md
@@ -347,6 +347,31 @@ Write `LAST30DAYS_KEYCHAIN_ALIASES` as a single-line JSON value in `.env`.
Multiline JSON formatting is not supported because `.env` files are parsed
line-by-line.
+#### Disabling the Keychain source
+
+Set `LAST30DAYS_SKIP_KEYCHAIN=1` to switch the Keychain source off entirely,
+making the loader a no-op on macOS as well:
+
+```bash
+LAST30DAYS_SKIP_KEYCHAIN=1 uv run pytest tests/test_footer_nudge_suppression.py
+```
+
+Scope it to the tests that need a sealed Keychain rather than the whole suite:
+the full run should keep exercising the positive-path Keychain tests.
+
+This exists mainly for tests and reproductions that assert on
+"no credentials configured" behaviour. Clearing `os.environ` and pointing
+`LAST30DAYS_CONFIG_DIR` at nothing is not sufficient on a machine with items
+stored under `last30days-<KEY>`: Keychain is a third, independent source, so a
+stored key can quietly satisfy a lookup the test expected to fail — and the
+test then fails on a contributor's Mac while passing in Linux CI, where the
+loader already no-ops.
+
+Unlike `LAST30DAYS_KEYCHAIN_ALIASES`, this switch is read from the process
+environment only and never from a `.env` file. It gates a credential source
+consulted *while* the config is being assembled, so a file-sourced value would
+be read too late to take effect.
+
### Bluesky app-password format and search host
`BSKY_APP_PASSWORD` should be a 19-char app password in `xxxx-xxxx-xxxx-xxxx` format (lowercase alphanumeric, three hyphens). Generate one at <https://bsky.app/settings/app-passwords>. The AT Protocol's `createSession` endpoint also accepts your main account login password, but that's bad hygiene — main passwords have no scope (an app password can be limited to non-DM access) and can't be revoked individually.
diff --git a/changelog.d/1050.fixed.md b/changelog.d/1050.fixed.md
new file mode 100644
index 00000000..e9149258
--- /dev/null
+++ b/changelog.d/1050.fixed.md
@@ -0,0 +1 @@
+`tests/test_footer_nudge_suppression.py::test_bare_run_emits_web_promo` failed on macOS for contributors with a `last30days-BRAVE_API_KEY` Keychain item, while passing in Linux CI. The test sealed two credential sources — it stripped the paid web keys from `os.environ` and set `LAST30DAYS_CONFIG_DIR=""` — but macOS Keychain is a third, independent source, so `BRAVE_API_KEY` was still resolved, `native_web_backend` was set, `_missing_sources_for_promo()` returned `None`, and the asserted `BRAVE_API_KEY` nudge was never printed. `_load_keychain()` gains a `LAST30DAYS_SKIP_KEYCHAIN` opt-out (process-environment only, since it gates a source consulted while the config is assembled), and the test now sets it. Engine behaviour is unchanged when the switch is unset.
diff --git a/skills/last30days/scripts/lib/env.py b/skills/last30days/scripts/lib/env.py
index 0d10eb81..0606a996 100644
--- a/skills/last30days/scripts/lib/env.py
+++ b/skills/last30days/scripts/lib/env.py
@@ -51,6 +51,13 @@ KEYCHAIN_SERVICE_PREFIX = "last30days-"
# A string value is shorthand for {"service": "..."} with the current user.
KEYCHAIN_ALIASES_ENV = "LAST30DAYS_KEYCHAIN_ALIASES"
+# Opt-out switch for the Keychain source. Set truthy to make _load_keychain a
+# no-op on Darwin too. Tests that assert on "no credentials configured"
+# behaviour need this: stripping os.environ and pointing LAST30DAYS_CONFIG_DIR
+# at nothing still leaves Keychain as a third source, so on a contributor's Mac
+# a stored key can silently satisfy a lookup the test meant to see fail.
+KEYCHAIN_DISABLE_ENV = "LAST30DAYS_SKIP_KEYCHAIN"
+
# Single source of truth for which credentials the Keychain loader looks up.
# The setup-keychain.sh helper mirrors this list and is held in sync via
# tests/test_env_keychain.py::test_keychain_keys_match_setup_script.
@@ -264,7 +271,16 @@ def _load_keychain(keys: list[str], aliases: dict[str, list[dict[str, str]]] | N
``LAST30DAYS_KEYCHAIN_ALIASES``. Lookup failures are silent — Keychain is
the lowest-priority source and is meant to be additive over `.env` files
and process environment.
+
+ Set ``LAST30DAYS_SKIP_KEYCHAIN`` truthy to disable the source entirely. It
+ is read from the process environment only, never from a config file: it
+ gates a credential source that is consulted *while* the config is being
+ assembled, so a file-sourced value would be read too late to have any
+ effect.
"""
+ if _truthy(os.environ.get(KEYCHAIN_DISABLE_ENV)):
+ return {}
+
import platform
if platform.system() != "Darwin":
return {}
diff --git a/tests/test_env_keychain.py b/tests/test_env_keychain.py
index 27717d13..72cec721 100644
--- a/tests/test_env_keychain.py
+++ b/tests/test_env_keychain.py
@@ -41,6 +41,45 @@ def _run_result(returncode: int, stdout: str = "") -> subprocess.CompletedProces
return subprocess.CompletedProcess(args=[], returncode=returncode, stdout=stdout, stderr="")
+def test_load_keychain_returns_empty_when_disable_switch_set(monkeypatch):
+ """The opt-out must win on Darwin with `security` present and a key stored.
+
+ Deliberately mocked past the platform/binary early-returns above: if the
+ switch were dropped, neither of those would cover this case and this goes red.
+ """
+ monkeypatch.setenv(env.KEYCHAIN_DISABLE_ENV, "1")
+ with mock.patch("platform.system", return_value="Darwin"), \
+ mock.patch("shutil.which", return_value="/usr/bin/security"), \
+ mock.patch("subprocess.run", return_value=_run_result(0, "should-not-be-read")) as run:
+ assert env._load_keychain(["XAI_API_KEY"]) == {}
+ # Proves the opt-out short-circuits BEFORE any lookup, not merely that the
+ # returned dict came back empty.
+ run.assert_not_called()
+
+
+def test_load_keychain_reads_key_when_disable_switch_absent(monkeypatch):
+ """Counterexample for the test above: same mocks, switch off -> the key IS read.
+
+ Without this pair, `_load_keychain(...) == {}` could pass for the wrong
+ reason and nobody would notice.
+ """
+ monkeypatch.delenv(env.KEYCHAIN_DISABLE_ENV, raising=False)
+ with mock.patch("platform.system", return_value="Darwin"), \
+ mock.patch("shutil.which", return_value="/usr/bin/security"), \
+ mock.patch("subprocess.run", return_value=_run_result(0, "xai-secret")):
+ assert env._load_keychain(["XAI_API_KEY"]) == {"XAI_API_KEY": "xai-secret"}
+
+
+def test_load_keychain_disable_switch_ignores_falsy_values(monkeypatch):
+ """`LAST30DAYS_SKIP_KEYCHAIN=0` / empty must NOT disable the source."""
+ for falsy in ("0", "", "false", "no"):
+ monkeypatch.setenv(env.KEYCHAIN_DISABLE_ENV, falsy)
+ with mock.patch("platform.system", return_value="Darwin"), \
+ mock.patch("shutil.which", return_value="/usr/bin/security"), \
+ mock.patch("subprocess.run", return_value=_run_result(0, "xai-secret")):
+ assert env._load_keychain(["XAI_API_KEY"]) == {"XAI_API_KEY": "xai-secret"}, falsy
+
+
def test_parse_keychain_aliases_accepts_string_and_object_forms():
raw = (
'{"XAI_API_KEY":"existing-xai-api-key",'
diff --git a/tests/test_footer_nudge_suppression.py b/tests/test_footer_nudge_suppression.py
index 9874a5af..abc15ed8 100644
--- a/tests/test_footer_nudge_suppression.py
+++ b/tests/test_footer_nudge_suppression.py
@@ -33,6 +33,13 @@ class FooterNudgeSuppressionTests(unittest.TestCase):
# BRAVE/EXA/SERPER/PARALLEL key doesn't make grounding "available"
# and suppress the promo we're checking for.
"LAST30DAYS_CONFIG_DIR": "",
+ # Keychain is a THIRD credential source, independent of the env
+ # stripping below and of LAST30DAYS_CONFIG_DIR. On a contributor's
+ # Mac a stored `last30days-BRAVE_API_KEY` item sets
+ # native_web_backend, _missing_sources_for_promo returns None, and
+ # this test fails for a reason that has nothing to do with promo
+ # selection. Seal that source too.
+ "LAST30DAYS_SKIP_KEYCHAIN": "1",
# Pin X as available so _missing_sources_for_promo selects "web"
# (otherwise the "x" promo wins and the BRAVE_API_KEY string never
# appears).
@@ -48,6 +55,14 @@ class FooterNudgeSuppressionTests(unittest.TestCase):
# Run from a tmpdir so _find_project_env() can't walk up into any
# .claude/last30days.env above the repo on the contributor's machine.
with tempfile.TemporaryDirectory() as tmp:
+ # pass(1) is a FOURTH credential source (_load_pass): a stored
+ # last30days/BRAVE_API_KEY entry leaks in exactly like the
+ # Keychain item sealed above. pass honors PASSWORD_STORE_DIR, so
+ # point it at an empty store inside the tmpdir — every lookup
+ # misses without touching the contributor's real store.
+ empty_pass_store = Path(tmp) / "empty-pass-store"
+ empty_pass_store.mkdir()
+ env["PASSWORD_STORE_DIR"] = str(empty_pass_store)
return subprocess.run(
cmd, capture_output=True, text=True, encoding="utf-8", env=env, cwd=tmp,
)