Skip to content

Commit f41703c

Browse files
fix(cli): plug parent-env REDIS_URL leak and add coverage (MAY-1086)
Address staff-engineer review on #365: - Pop REDIS_URL from the inherited env when redis_enabled is false. The previous implementation only avoided seeding a default, so a stale REDIS_URL exported by the parent shell still leaked into the agent process and reproduced the same silent hang the opt-out was meant to prevent. - Drop the unreachable `local_development is None` short-circuit; the dict literal above already dereferences manifest.local_development with a # type: ignore[union-attr], so the None branch can never run. - Add tests/lib/cli/test_run_handlers.py covering the three branches: default seeds REDIS_URL, opt-out with clean parent env clears it, opt-out with a parent-shell REDIS_URL still clears it. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent f10b378 commit f41703c

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/agentex/lib/cli/handlers/run_handlers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,12 @@ def create_agent_environment(manifest: AgentManifest) -> dict[str, str]:
374374
# Gate the default REDIS_URL on an explicit manifest flag. Agents that don't use
375375
# adk.messages/adk.streaming can set local_development.redis_enabled: false to avoid
376376
# the localhost:6379 default, which otherwise causes silent request hangs when no
377-
# local Redis is reachable (MAY-1086).
378-
if manifest.local_development is None or manifest.local_development.redis_enabled:
377+
# local Redis is reachable (MAY-1086). When opting out, also drop any REDIS_URL the
378+
# parent shell may have exported, so the opt-out actually guarantees a clean env.
379+
if manifest.local_development.redis_enabled: # type: ignore[union-attr]
379380
env_vars["REDIS_URL"] = "redis://localhost:6379"
381+
else:
382+
env.pop("REDIS_URL", None)
380383

381384
if manifest.agent.agent_input_type:
382385
env_vars["AGENT_INPUT_TYPE"] = manifest.agent.agent_input_type

tests/lib/cli/test_run_handlers.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Tests for run_handlers.create_agent_environment — REDIS_URL gating (MAY-1086)."""
2+
3+
from __future__ import annotations
4+
5+
import pytest
6+
7+
from agentex.lib.cli.handlers.run_handlers import create_agent_environment
8+
from agentex.lib.sdk.config.agent_manifest import AgentManifest
9+
10+
11+
@pytest.fixture
12+
def manifest_path() -> str:
13+
"""A real tutorial manifest with acp_type=async and default redis_enabled."""
14+
return "examples/tutorials/10_async/00_base/110_pydantic_ai/manifest.yaml"
15+
16+
17+
class TestCreateAgentEnvironmentRedisGating:
18+
def test_default_seeds_redis_url(self, manifest_path: str, monkeypatch: pytest.MonkeyPatch):
19+
"""With redis_enabled unset (default true), CLI seeds the localhost REDIS_URL."""
20+
monkeypatch.delenv("REDIS_URL", raising=False)
21+
manifest = AgentManifest.from_yaml(file_path=manifest_path)
22+
assert manifest.local_development is not None
23+
assert manifest.local_development.redis_enabled is True
24+
25+
env = create_agent_environment(manifest)
26+
27+
assert env.get("REDIS_URL") == "redis://localhost:6379"
28+
29+
def test_opt_out_clears_redis_url_when_parent_env_clean(
30+
self, manifest_path: str, monkeypatch: pytest.MonkeyPatch
31+
):
32+
"""With redis_enabled=false and no parent REDIS_URL, REDIS_URL is absent."""
33+
monkeypatch.delenv("REDIS_URL", raising=False)
34+
manifest = AgentManifest.from_yaml(file_path=manifest_path)
35+
assert manifest.local_development is not None
36+
manifest.local_development.redis_enabled = False
37+
38+
env = create_agent_environment(manifest)
39+
40+
assert "REDIS_URL" not in env
41+
42+
def test_opt_out_clears_redis_url_when_parent_env_has_one(
43+
self, manifest_path: str, monkeypatch: pytest.MonkeyPatch
44+
):
45+
"""With redis_enabled=false, a stale parent-shell REDIS_URL must not leak through."""
46+
monkeypatch.setenv("REDIS_URL", "redis://leftover.from.parent.shell:6379")
47+
manifest = AgentManifest.from_yaml(file_path=manifest_path)
48+
assert manifest.local_development is not None
49+
manifest.local_development.redis_enabled = False
50+
51+
env = create_agent_environment(manifest)
52+
53+
assert "REDIS_URL" not in env

0 commit comments

Comments
 (0)