-
Notifications
You must be signed in to change notification settings - Fork 10
fix(cli): load the agent's .env into locally run ACP and worker processes #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelxu2288
wants to merge
2
commits into
scaleapi:next
Choose a base branch
from
michaelxu2288:fix/cli-load-dotenv-local-run
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """Tests for the environment `agentex agents run` hands to the ACP and worker processes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from agentex.lib.cli.commands.init import ( | ||
| TemplateType, | ||
| get_project_context, | ||
| create_project_structure, | ||
| ) | ||
| from agentex.lib.cli.handlers.run_handlers import create_agent_environment | ||
| from agentex.lib.sdk.config.agent_manifest import load_agent_manifest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project_dir(tmp_path: Path) -> Path: | ||
| """A scaffolded Sync ACP project, so the manifest is a real one.""" | ||
| answers = { | ||
| "template_type": TemplateType.SYNC, | ||
| "project_path": str(tmp_path), | ||
| "agent_name": "env-agent", | ||
| "agent_directory_name": "env-agent", | ||
| "description": "An Agentex agent", | ||
| "use_uv": True, | ||
| } | ||
| context = get_project_context(answers, tmp_path, Path("../../")) | ||
| context["template_type"] = TemplateType.SYNC.value | ||
| context["use_uv"] = True | ||
| create_project_structure(tmp_path, context, TemplateType.SYNC, use_uv=True) | ||
| return tmp_path / context["project_name"] | ||
|
|
||
|
|
||
| def test_dotenv_next_to_manifest_is_loaded(project_dir: Path, monkeypatch: pytest.MonkeyPatch): | ||
| """Values in <manifest dir>/.env reach the subprocess environment.""" | ||
| (project_dir / ".env").write_text("FROM_DOTENV=1\nLITELLM_API_KEY=sk-test\n") | ||
| monkeypatch.delenv("FROM_DOTENV", raising=False) | ||
| monkeypatch.delenv("LITELLM_API_KEY", raising=False) | ||
| manifest = load_agent_manifest(file_path=str(project_dir / "manifest.yaml")) | ||
|
|
||
| env = create_agent_environment(manifest, manifest_dir=project_dir) | ||
|
|
||
| assert env["FROM_DOTENV"] == "1" | ||
| assert env["LITELLM_API_KEY"] == "sk-test" | ||
| assert env["ENVIRONMENT"] == "development" | ||
|
|
||
|
|
||
| def test_shell_variables_win_over_dotenv(project_dir: Path, monkeypatch: pytest.MonkeyPatch): | ||
| """A variable already exported in the shell is not overridden by .env.""" | ||
| (project_dir / ".env").write_text("PRESET=from-dotenv\n") | ||
| monkeypatch.setenv("PRESET", "from-shell") | ||
| manifest = load_agent_manifest(file_path=str(project_dir / "manifest.yaml")) | ||
|
|
||
| env = create_agent_environment(manifest, manifest_dir=project_dir) | ||
|
|
||
| assert env["PRESET"] == "from-shell" | ||
|
|
||
|
|
||
| def test_missing_dotenv_and_no_manifest_dir_are_fine(project_dir: Path, monkeypatch: pytest.MonkeyPatch): | ||
| """No .env file, or no manifest_dir given, still builds a normal environment.""" | ||
| monkeypatch.delenv("FROM_DOTENV", raising=False) | ||
| manifest = load_agent_manifest(file_path=str(project_dir / "manifest.yaml")) | ||
|
|
||
| assert "FROM_DOTENV" not in create_agent_environment(manifest, manifest_dir=project_dir) | ||
| assert "FROM_DOTENV" not in create_agent_environment(manifest) | ||
|
|
||
|
|
||
| def test_dotenv_overrides_builtin_local_defaults_but_not_environment(project_dir: Path, monkeypatch: pytest.MonkeyPatch): | ||
| """.env may point local runs at a custom Redis/Temporal; ENVIRONMENT stays development.""" | ||
| (project_dir / ".env").write_text("REDIS_URL=redis://custom:6380\nTEMPORAL_ADDRESS=temporal.internal:7233\nENVIRONMENT=production\n") | ||
| for key in ("REDIS_URL", "TEMPORAL_ADDRESS", "ENVIRONMENT"): | ||
| monkeypatch.delenv(key, raising=False) | ||
| manifest = load_agent_manifest(file_path=str(project_dir / "manifest.yaml")) | ||
|
|
||
| env = create_agent_environment(manifest, manifest_dir=project_dir) | ||
|
|
||
| assert env["REDIS_URL"] == "redis://custom:6380" | ||
| assert env["TEMPORAL_ADDRESS"] == "temporal.internal:7233" | ||
| assert env["ENVIRONMENT"] == "development" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
.enventry can now overwrite manifest-derived runtime settings such asAGENT_NAME,ACP_PORT,WORKFLOW_TASK_QUEUE, andHEALTH_CHECK_PORT. These values are added throughenv_vars, butmanifest_envcontains only the explicitagent.envmapping. A conflicting dotenv value therefore replaces the manifest-derived value and can start the ACP or worker with the wrong identity, port, task queue, or health-check configuration. Preserve all manifest-derived keys while still allowing.envto replace built-in local defaults.Knowledge Base Used: Command-line workflows
Prompt To Fix With AI