fix(cli): load the agent's .env into locally run ACP and worker processes - #515
Open
michaelxu2288 wants to merge 2 commits into
Open
michaelxu2288 wants to merge 2 commits into
michaelxu2288 wants to merge 2 commits into
Conversation
michaelxu2288
added a commit
to michaelxu2288/scale-agentex-python
that referenced
this pull request
Sep 10, 2026
…apping the key The worker is a separate process and agents run does not load the project .env into it today, so the LITELLM_API_KEY -> OPENAI_API_KEY mapping ran against an empty environment. Call load_dotenv() first (as acp.py already does), so the template works on its own; scaleapi#515 makes the CLI load .env for both processes as well. Claude-Session: https://claude.ai/code/session_01HCVKnA7LeJZ44nxZz1uzF3
Comment on lines
+476
to
+478
| if key in os.environ or key in manifest_env: | ||
| continue | ||
| env[key] = value |
There was a problem hiding this comment.
Dotenv overrides manifest settings
A .env entry can now overwrite manifest-derived runtime settings such as AGENT_NAME, ACP_PORT, WORKFLOW_TASK_QUEUE, and HEALTH_CHECK_PORT. These values are added through env_vars, but manifest_env contains only the explicit agent.env mapping. 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 .env to replace built-in local defaults.
Knowledge Base Used: Command-line workflows
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agentex/lib/cli/handlers/run_handlers.py
Line: 476-478
Comment:
**Dotenv overrides manifest settings**
A `.env` entry can now overwrite manifest-derived runtime settings such as `AGENT_NAME`, `ACP_PORT`, `WORKFLOW_TASK_QUEUE`, and `HEALTH_CHECK_PORT`. These values are added through `env_vars`, but `manifest_env` contains only the explicit `agent.env` mapping. 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 `.env` to replace built-in local defaults.
**Knowledge Base Used:** [Command-line workflows](https://app.greptile.com/scale-ai/-/custom-context/knowledge-base/scaleapi/scale-agentex-python/-/docs/command-line-workflows.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
stainless-app
Bot
force-pushed
the
next
branch
2 times, most recently
from
September 18, 2026 21:47
bc51c52 to
761833e
Compare
…sses The docs say a .env next to manifest.yaml is loaded automatically for local development, but nothing did: agentex agents run built the child environment from os.environ plus the manifest, and EnvironmentVariables.refresh() looks for .env two directories above the installed module (site-packages), never the agent folder. Values in .env only reached a process when some import happened to call load_dotenv() first (litellm does, on import). That is why the OpenAI Agents Temporal template worked (its model client is built lazily after imports) while the Pydantic AI one failed at import time with openai.OpenAIError: Missing credentials. Merge <manifest dir>/.env into the environment handed to both the ACP and worker subprocesses, without overriding variables already set in the shell (python-dotenv semantics). Add tests for merge, shell precedence, and the no-file / no-manifest_dir cases. Claude-Session: https://claude.ai/code/session_01HCVKnA7LeJZ44nxZz1uzF3
Loading .env before env_vars meant env.update(env_vars) clobbered any overlapping key, so a custom REDIS_URL or TEMPORAL_ADDRESS in .env had no effect. Apply .env after the defaults, below the manifest env block and the shell, and keep ENVIRONMENT pinned to development; add a regression test. Claude-Session: https://claude.ai/code/session_01HCVKnA7LeJZ44nxZz1uzF3
michaelxu2288
added a commit
to michaelxu2288/scale-agentex-python
that referenced
this pull request
Oct 1, 2026
…apping the key The worker is a separate process and agents run does not load the project .env into it today, so the LITELLM_API_KEY -> OPENAI_API_KEY mapping ran against an empty environment. Call load_dotenv() first (as acp.py already does), so the template works on its own; scaleapi#515 makes the CLI load .env for both processes as well. Claude-Session: https://claude.ai/code/session_01HCVKnA7LeJZ44nxZz1uzF3
michaelxu2288
force-pushed
the
fix/cli-load-dotenv-local-run
branch
from
October 1, 2026 09:40
0a24841 to
9d2815a
Compare
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
getting_started/project_structure.mdsays a.envnext tomanifest.yaml"is automatically loaded when placed alongside your manifest.yaml file, but only for local development". Nothing does that today:agentex agents runbuilds the child environment fromos.environplus the manifest (create_agent_environment).EnvironmentVariables.refresh()loads.envfromPath(__file__).parents[2], which issite-packagesfor an installed SDK, never the agent folder, and.env.localfromcwd.parent.ProjectConfigLoader._load_envonly feeds config templating.Values from
.envreach a process only when some import happens to callload_dotenv()first.litellmdoes, on import (litellm/__init__.py), which is why the Temporal + OpenAI Agents template appears to work (its model client is built lazily inside the activity, after imports) while Temporal + Pydantic AI fails at import time ofproject/agent.pywithopenai.OpenAIError: Missing credentials. Verified with a print at the top of both workers: OpenAI Agents worker seesLITELLM_API_KEYonly after its imports; the Pydantic AI worker never does.Fix
create_agent_environment(manifest, manifest_dir)merges<manifest dir>/.env(python-dotenvdotenv_values, already a dependency) into the environment handed to both the ACP and worker subprocesses, without overriding variables already set in the shell (python-dotenv semantics).run_agentpassesmanifest_file.parent. 16-line diff plus three tests (merge, shell precedence, no file / no manifest_dir) that render a real manifest via theinithelpers.Verification
tests/lib/cli/test_run_handlers_env.py: 3 passed; streaming handler tests still pass; ruff clean.LITELLM_API_KEYin.env, patched SDK installed into its venv: before, no reply and Missing credentials; after, reply in 4.7 s, zero worker errors.Pairs with #514 ("fix(templates): map LITELLM_API_KEY to OPENAI_API_KEY in Temporal workers"): once
.envis in the process environment from the start, that mapping at the top ofrun_worker.pybecomes deterministic instead of depending on import order.The PR is not ready to merge while
.envcan replace manifest-derived settings and deployed agents are missing from metadata-filtered lists.Fix with agent prompt
Summary
Local agent runs now load
.envfrom besidemanifest.yamlinto both child processes, and ACP task creation waits for its handler before replying. The PR also changes the production API URL and updates release, contributor, and security-scan workflows.main-based workflow.Diagram
%%{init: {'theme': 'neutral'}}%% flowchart LR M[manifest.yaml] --> E[Local environment] D[.env] --> E S[Shell variables] --> E E --> A[ACP process] E --> W[Worker process]Reviews (3) · Last reviewed commit: "fix(cli): apply .env after the built-in ..."