Skip to content

fix(cli): load the agent's .env into locally run ACP and worker processes - #515

Open
michaelxu2288 wants to merge 2 commits into
scaleapi:mainfrom
michaelxu2288:fix/cli-load-dotenv-local-run
Open

michaelxu2288 wants to merge 2 commits into
scaleapi:mainfrom
michaelxu2288:fix/cli-load-dotenv-local-run

Conversation

@michaelxu2288

@michaelxu2288 michaelxu2288 commented Sep 10, 2026 •

Copy link
Copy Markdown

Problem

getting_started/project_structure.md says a .env next to manifest.yaml "is automatically loaded when placed alongside your manifest.yaml file, but only for local development". Nothing does that today:

  • agentex agents run builds the child environment from os.environ plus the manifest (create_agent_environment).
  • EnvironmentVariables.refresh() loads .env from Path(__file__).parents[2], which is site-packages for an installed SDK, never the agent folder, and .env.local from cwd.parent.
  • ProjectConfigLoader._load_env only feeds config templating.

Values from .env reach a process only when some import happens to call load_dotenv() first. litellm does, 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 of project/agent.py with openai.OpenAIError: Missing credentials. Verified with a print at the top of both workers: OpenAI Agents worker sees LITELLM_API_KEY only after its imports; the Pydantic AI worker never does.

Fix

create_agent_environment(manifest, manifest_dir) merges <manifest dir>/.env (python-dotenv dotenv_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_agent passes manifest_file.parent. 16-line diff plus three tests (merge, shell precedence, no file / no manifest_dir) that render a real manifest via the init helpers.

Verification

  • tests/lib/cli/test_run_handlers_env.py: 3 passed; streaming handler tests still pass; ruff clean.
  • Live, Temporal + Pydantic AI scaffold with only LITELLM_API_KEY in .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 .env is in the process environment from the start, that mapping at the top of run_worker.py becomes deterministic instead of depending on import order.

RetriggerConfidence Score: 3/5

The PR is not ready to merge while .env can replace manifest-derived settings and deployed agents are missing from metadata-filtered lists.

Fix All in CursorFindings

  1. P1 Dotenv overrides manifest settings ▶
Fix with agent prompt
### Issue 1
src/agentex/lib/cli/handlers/run_handlers.py:476-478
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.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Local agent runs now load .env from beside manifest.yaml into 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.

  • Keeps local process settings consistent while respecting shell and manifest overrides.
  • Makes a created task available before callers send events.
  • Moves release and repository automation to the 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]
Loading

Reviews (3) · Last reviewed commit: "fix(cli): apply .env after the built-in ..."

Comment thread src/agentex/lib/cli/handlers/run_handlers.py Outdated
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Fix in Cursor Fix in Claude Code Fix in Codex

@stainless-app
stainless-app Bot force-pushed the next branch 2 times, most recently from bc51c52 to 761833e Compare September 18, 2026 21:47
…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
michaelxu2288 force-pushed the fix/cli-load-dotenv-local-run branch from 0a24841 to 9d2815a Compare October 1, 2026 09:40
@michaelxu2288
michaelxu2288 changed the base branch from next to main October 1, 2026 09:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant