Skip to content

fix: load .env before reading HOST/PORT/UVICORN_WORKERS in cmd_serve - #45

Merged
OkiriGabriel merged 5 commits into
open-devops-agent:mainfrom
Nitjsefnie-OSC:fix/uvicorn-env-41
Aug 17, 2026
Merged

OkiriGabriel merged 5 commits into
open-devops-agent:mainfrom
Nitjsefnie-OSC:fix/uvicorn-env-41

Conversation

@Nitjsefnie

Copy link
Copy Markdown
Contributor

Summary
cmd_serve read HOST, PORT and UVICORN_WORKERS from the environment but never called load_dotenv() — the only load happened in api/server.py, which uvicorn imports after the values were already read. .env settings are now loaded before the reads, so the documented variables actually reach the server.

Related Issues and Pull Requests
Fixes #41

Changes

  • devops_agent/cli.py: load_dotenv() at the top of cmd_serve (with the module-top import). Precedence unchanged and now consistent: override=False keeps exported shell env above .env, and the existing args.host or os.getenv(...) fallbacks keep --host/--port/--workers flags above both
  • .env.example: the SERVER-section caveat written before this fix ("export them rather than relying on .env alone") is now false — rewritten to state the actual precedence
  • New tests/test_cli_serve_env.py (4 tests) driving the real cli.main() with only uvicorn.run stubbed

Testing
Test-first: the .env test is red against the un-fixed code, green after. Full suite locally: only the pre-existing test_mcp.py pin failure — which #38 fixed upstream since; on current main the suite is green except the PagerDuty/.env.example drift (see the separate issue). Adversarially verified, including the load-order claim and a no-regression check on the other subcommands.

Footer
Generated by Kimi K3 (brief, implementation, review, verification)

Nitjsefnie and others added 5 commits August 5, 2026 18:39
…oured

cmd_serve read HOST, PORT and UVICORN_WORKERS from the environment, but
the only load_dotenv() on the serve path lived in api/server.py, which
uvicorn imports from the string "api.server:app" after those reads.
The three settings in .env were therefore ignored and the hardcoded
defaults always won.

Call load_dotenv() at the top of cmd_serve, before the os.getenv reads.
Precedence is unchanged: exported environment variables still beat .env
(load_dotenv does not override) and --host/--port/--workers beat both.
Update the .env.example comment that documented the old caveat.

Fixes open-devops-agent#41

Co-Authored-By: Kimi K3 <[email protected]>
Brings in upstream's .env.example entries for PAGERDUTY_URGENCY_*,
fixing tests/test_env_example_coverage.py.

Co-Authored-By: Kimi K3 <[email protected]>
CI's dummy-env step creates a .env before pytest, which tripped the
fixture's not-exists assertion. Save any pre-existing file's bytes and
restore them after each test; remove the file only when none existed,
preserving isolation for the absent-file case.

Co-Authored-By: Kimi K3 <[email protected]>
Simulates the installed package living outside the directory containing
.env: cwd is changed to a fresh invocation directory and the module stays
in its installed location. Bare load_dotenv() anchors its upward search at
the calling module, so the cwd .env is ignored after installation.

Fails against production unchanged:
  tests/test_cli_serve_env.py::test_serve_reads_dotenv_from_invocation_cwd
  AssertionError: assert '127.0.0.1' == '203.0.113.7'

Co-Authored-By: Kimi K3 <[email protected]>
Bare load_dotenv() anchors its upward search at the calling module, so
after a normal installation (site-packages) a user's .env in the process
cwd is never found and uvicorn starts on HOST/PORT/UVICORN_WORKERS
defaults. Load Path.cwd() / ".env" explicitly, keeping python-dotenv's
default non-override semantics: already-set environment values still win.

Co-Authored-By: Kimi K3 <[email protected]>
@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Reviewed — CI is green across all checks. Loading .env before HOST/PORT/UVICORN_WORKERS in cmd_serve is the right fix. Merging to main.

@OkiriGabriel
OkiriGabriel merged commit 86fc865 into open-devops-agent:main Aug 17, 2026
10 checks passed
@Nitjsefnie
Nitjsefnie deleted the fix/uvicorn-env-41 branch August 19, 2026 14:50
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.

[BUG] HOST, PORT and UVICORN_WORKERS are ignored when set in .env

2 participants