Skip to content

feat: add global .env file support#8

Merged
shrwnsan merged 1 commit into
devfrom
feat/global-env
Jan 9, 2026
Merged

feat: add global .env file support#8
shrwnsan merged 1 commit into
devfrom
feat/global-env

Conversation

@shrwnsan
Copy link
Copy Markdown
Owner

@shrwnsan shrwnsan commented Jan 8, 2026

Summary

Implements global .env file support at ~/.agentbox/.env that loads before project-specific .env files, enabling centralized configuration for API keys and custom inference endpoints.

Closes #6

Changes

  • agentbox: Load ~/.agentbox/.env first, then PROJECT_DIR/.env
  • Project env vars override global vars (Docker's last-wins precedence)
  • Added log message for discoverability when global .env is loaded
  • README.md: Document global .env usage with examples

Use Case

Configure third-party inference providers (e.g., non-Anthropic inference services) without needing to set credentials per-project:

# ~/.agentbox/.env
ANTHROPIC_BASE_URL=https://api.example.com/v1
ANTHROPIC_API_KEY=your-shared-key-here

Behavior

  1. Global .env loaded first (if exists)
  2. Project .env loaded second (if exists)
  3. Project values override global values

Alignment with Project Philosophy

  • Simplicity First: ~10 lines of code, no complex configuration
  • Automatic Behavior: No prompts, just works if file exists
  • Fail Gracefully: Silent skip if file doesn't exist

🤖 Generated by Claude Code - GLM 4.7

Add support for ~/.agentbox/.env that loads before project-specific
.env files. This enables centralized configuration for API keys and
custom inference endpoints across all projects.

Changes:
- Load ~/.agentbox/.env first, then PROJECT_DIR/.env
- Project env vars override global vars (Docker's last-wins behavior)
- Add log message for discoverability when global .env is loaded
- Document global .env usage in README with examples

Co-Authored-By: GLM <zai-org@users.noreply.github.com>
@shrwnsan
Copy link
Copy Markdown
Owner Author

shrwnsan commented Jan 8, 2026

Code Review for PR #8: feat: add global .env file support

Summary

This PR implements a global .env file feature that loads environment variables from ~/.agentbox/.env before project-specific .env files. The implementation is clean, minimal, and follows the project's philosophy of simplicity and automatic behavior. The changes are well-scoped, properly documented, and align with existing code patterns.

Overall Assessment: ✅ Approve

Key Strengths:

  • Minimal implementation (~10 lines of code)
  • Follows Docker's last-wins precedence for env var overrides
  • Proper documentation with clear examples
  • Maintains backward compatibility (fails gracefully if global .env doesn't exist)
  • Clear logging for discoverability

Key Concerns:

  • None blocking - PR is ready to merge

Detailed Feedback

1. Understanding & Intent

What is this PR trying to accomplish?
✅ Clear: Adds support for a global .env file at ~/.agentbox/.env that loads before project-specific .env files, enabling centralized configuration for API keys and custom inference endpoints.

Does the title and description clearly communicate the purpose?
✅ Excellent: The PR description is comprehensive, explaining:

  • The what and why
  • Use cases with examples
  • Loading order and override behavior
  • Alignment with project philosophy

Are the changes scoped appropriately, or should this be split?
✅ Well-scoped: Only 2 files changed with 21 additions / 2 deletions. This is a focused, single-purpose change.

Does the PR link to relevant issues/tickets?
✅ Yes: References issue #6


2. Code Quality Assessment

Architecture & Design

Is the solution well-architected?
✅ Yes. The design follows Docker's --env-file behavior where later files override earlier ones, which is standard and predictable.

Appropriate abstraction level:
✅ The implementation is at the right level of abstraction - it's a simple file existence check with array accumulation, matching the existing pattern for project .env loading.

Good separation of concerns:
✅ The env file loading logic is contained within the run_container() function where Docker args are built, maintaining cohesive responsibility.

Follows existing patterns:
✅ Excellent. The code mirrors the existing project .env pattern:

local env_file_args=()
if [[ -f "$path" ]]; then
    env_file_args+=(--env-file "$path")
    log_info "..."
fi
Code Correctness

Logic correctness:
✅ The code correctly:

  1. Checks for global .env existence first
  2. Adds it to env_file_args array
  3. Then checks for project .env (if exists, overrides)
  4. Both files use Docker's --env-file flag which provides last-wins precedence

Edge cases:
✅ Well handled:

  • File doesn't exist: Silent skip (graceful failure)
  • Both files exist: Project overrides global (correct behavior)
  • Only global exists: Works correctly
  • Only project exists: Works correctly (backward compatible)
  • Neither exists: No error, continues normally

Potential bugs:
None found. The implementation is straightforward and leverages Docker's built-in behavior.

Code Style & Consistency

Does the code follow the project's style guidelines?
✅ Yes:

  • Uses local variables for function scope
  • Follows bash array syntax: env_file_args+=(...)
  • Consistent spacing and indentation
  • Uses [[ ]] for file tests (bash best practice)

Is it consistent with existing code in the file?
✅ Perfectly consistent with the existing project .env handling pattern it's based on.

Naming:
✅ Clear and descriptive:

  • agentbox_env - clearly indicates it's the agentbox-specific env file
  • env_file_args - matches existing variable naming

Comments:
✅ Appropriate:

  • Comments explain the loading order and override behavior
  • Updated comment for project .env to clarify it overrides global
  • Comments are concise and informative
Security & Safety

Security concerns:
✅ No security issues:

  • Uses [[ -f "$path" ]] which safely tests for regular files
  • Variables are properly quoted
  • No eval or unsafe command execution
  • Env file paths are controlled by the user (not user input)

Resource management:
✅ No issues - this just adds arguments to a Docker command, no resources to clean up.


3. Testing Coverage

Are there tests for the new functionality?
⚠️ Major: The project appears to have no automated tests (no test files found in directory). This is a pre-existing issue, not introduced by this PR.

Do existing tests still pass?
N/A - No test suite exists in the project.

Are test cases meaningful?
N/A - No tests.

Missing test scenarios:
Since there's no test suite, manual testing should verify:

  1. Global .env only - variables load correctly
  2. Project .env only - backward compatibility maintained
  3. Both files - project overrides global
  4. Neither file - no errors, normal operation
  5. Malformed .env files - Docker handles this gracefully

Note: The lack of tests is a project-wide concern, not specific to this PR. The implementation is simple enough that manual testing is sufficient.


4. Documentation & Impact

Code documentation:
✅ Excellent:

  • Clear inline comments explaining loading order
  • Log messages provide runtime feedback
  • Comments align with the project's sparse-comment philosophy

User-facing documentation:
✅ Outstanding:

  • README.md updated with dedicated section
  • Clear explanation of loading order (numbered list)
  • Practical example provided
  • Explains the override behavior
  • Mentions the use case (shared credentials)

Breaking changes:
✅ None - fully backward compatible. If global .env doesn't exist, behavior is unchanged.

Deprecations:
✅ None.


5. Performance & Scalability

Performance considerations:
✅ Excellent:

  • O(1) file existence checks
  • Minimal overhead (two additional file tests)
  • No loops or complex operations
  • No performance impact when files don't exist

Resource usage:
✅ Negligible:

  • Only adds 2-3 bash variable operations
  • No additional processes spawned
  • No I/O beyond file existence tests

6. Maintainability & Readability

Readability:
✅ The code is exceptionally clear:

# Load global .env file first (PROJECT_DIR/.env will override)
local env_file_args=()
local agentbox_env="${HOME}/.agentbox/.env`
if [[ -f "$agentbox_env" ]]; then
    env_file_args+=(--env-file "$agentbox_env")
    log_info "Global .env loaded from ~/.agentbox/.env"
fi

Any developer familiar with bash can understand this immediately.

Extensibility:
✅ The pattern is easily extensible:

  • Array accumulation allows adding more env files in the future
  • Clear separation between global and project-specific loading
  • Could easily add additional locations (e.g., /etc/agentbox/.env) if needed

7. Specific Line-by-Line Feedback

File: agentbox

Lines 376-383 - ✅ Excellent implementation

# Load global .env file first (PROJECT_DIR/.env will override)
local env_file_args=()
local agentbox_env="${HOME}/.agentbox/.env`
if [[ -f "$agentbox_env" ]]; then
    env_file_args+=(--env-file "$agentbox_env")
    log_info "Global .env loaded from ~/.agentbox/.env`
fi
  • Clear comment explaining behavior
  • Descriptive variable names
  • Proper quoting
  • Informative log message

Lines 386-389 - ✅ Good update to existing code

# Load project-specific .env (overrides global .env)
if [[ -f "$PROJECT_DIR/.env" ]]; then
    env_file_args+=(--env_file "$PROJECT_DIR/.env")
    log_info "Project .env file found and will be loaded into container"
fi
  • Updated comment to clarify override behavior
  • More specific log message distinguishes from global .env

Minor nitpick (cosmetic only):
The log message "Project .env file found and will be loaded into container" is slightly verbose and inconsistent with the global .env message. Consider:

log_info "Project .env loaded from $PROJECT_DIR/.env"

However, this is purely cosmetic and the current message is acceptable.

File: README.md

Lines 138-149 - ✅ Excellent documentation

  • Clear section header
  • Numbered list explains loading order
  • Practical example provided
  • Override behavior clearly stated

The documentation perfectly follows the project's philosophy:

  • Assumes knowledgeable developer
  • Documents agentbox-specific knowledge
  • Provides genuinely helpful information without verbosity
  • Clear and concise

What's Done Well

  1. Minimal implementation: The feature adds only ~10 lines of code, embodying the "Simplicity First" philosophy
  2. Automatic behavior: No prompts or configuration needed - just works if the file exists
  3. Graceful failure: Silent skip if global .env doesn't exist, maintaining backward compatibility
  4. Clear documentation: README updates are concise, practical, and well-structured
  5. Discoverability: Log messages inform users when env files are loaded
  6. Correct precedence: Uses Docker's last-wins behavior, which is standard and predictable
  7. Code consistency: Perfectly matches existing patterns in the codebase
  8. Thoughtful design: Global loads first, project overrides - the logical and expected behavior

Action Items

Required Before Merge

None

Recommended Improvements

  1. [Minor] README.md:387 - Consider making log messages more consistent:

    log_info "Project .env loaded from $PROJECT_DIR/.env"

    This matches the pattern used for global .env and is more concise.

  2. [Project-wide] Testing - Consider adding basic integration tests for the project. Even simple smoke tests would help catch regressions. This is not a blocker for this PR but would benefit the project long-term.

  3. [Future enhancement] Error handling - Consider validating .env file syntax and providing clearer errors if Docker rejects malformed env files. This could be a future improvement.


Verdict

This PR is well-crafted and ready to merge. The implementation is clean, minimal, and perfectly aligned with the project's philosophy of simplicity and automatic behavior. The code is correct, well-documented, and maintains backward compatibility.

Great work on:

  • Keeping the implementation minimal and focused
  • Providing clear, concise documentation
  • Following existing code patterns consistently
  • Ensuring graceful backward compatibility
  • Adding discoverability through log messages

The minor log message consistency suggestion is purely cosmetic and doesn't block merge. This PR exemplifies the project's development philosophy perfectly.

Status: ✅ Ready to merge


Reviewed by: Droid - GLM 4.7

@shrwnsan
Copy link
Copy Markdown
Owner Author

shrwnsan commented Jan 8, 2026

Code Review: Global .env Support

Approve - This PR is ready to merge.

Strengths

  • Simple, focused implementation (~11 lines) that avoids over-engineering
  • Correct environment variable precedence: global loads first, project overrides
  • Well-placed documentation with clear use case examples
  • Updated log messages provide good discoverability
  • Backwards compatible; feature is opt-in

Code Quality

  • Proper file existence checks
  • Correct Docker --env-file ordering behavior
  • Edge cases handled gracefully (both files optional, silent skip if missing)
  • Follows existing code patterns and naming conventions

Documentation

Clear explanation of load order with practical example for third-party inference providers. Documentation placement and messaging help users discover and understand the feature.


Reviewed by: Amp Code

@shrwnsan shrwnsan merged commit 88c3be8 into dev Jan 9, 2026
0 of 2 checks passed
@shrwnsan shrwnsan deleted the feat/global-env branch January 9, 2026 02:51
@shrwnsan
Copy link
Copy Markdown
Owner Author

shrwnsan commented Jan 9, 2026

✅ PR Merged Successfully

Merged to: dev branch

Summary

This PR adds global .env file support at ~/.agentbox/.env that loads before project-specific .env files, enabling centralized configuration for API keys and custom inference endpoints.

Changes

  • Load ~/.agentbox/.env first, then PROJECT_DIR/.env
  • Project env vars override global vars (Docker's last-wins precedence)
  • Added log message for discoverability when global .env is loaded
  • README.md documented with usage examples

Issues Resolved


🤖 Generated by Claude Code - GLM 4.7

shrwnsan added a commit that referenced this pull request Jan 10, 2026
Add support for ~/.agentbox/.env that loads before project-specific
.env files. This enables centralized configuration for API keys and
custom inference endpoints across all projects.

Changes:
- Load ~/.agentbox/.env first, then PROJECT_DIR/.env
- Project env vars override global vars (Docker's last-wins behavior)
- Add log message for discoverability when global .env is loaded
- Document global .env usage in README with examples

Co-authored-by: GLM <zai-org@users.noreply.github.com>
shrwnsan added a commit that referenced this pull request Jan 10, 2026
Add support for ~/.agentbox/.env that loads before project-specific
.env files. This enables centralized configuration for API keys and
custom inference endpoints across all projects.

Changes:
- Load ~/.agentbox/.env first, then PROJECT_DIR/.env
- Project env vars override global vars (Docker's last-wins behavior)
- Add log message for discoverability when global .env is loaded
- Document global .env usage in README with examples

Co-authored-by: GLM <zai-org@users.noreply.github.com>
shrwnsan added a commit that referenced this pull request Jan 11, 2026
…etchgqc#43)

* fix: restrict GitHub Actions wildcard permissions to current PR (#4)

Scope gh pr comment permissions to only the PR being reviewed
by using PR number variable instead of wildcard. Prevents
compromised workflow token from commenting on arbitrary PRs.

Co-authored-by: GLM <zai-org@users.noreply.github.com>

* feat: add global .env file support (#8)

Add support for ~/.agentbox/.env that loads before project-specific
.env files. This enables centralized configuration for API keys and
custom inference endpoints across all projects.

Changes:
- Load ~/.agentbox/.env first, then PROJECT_DIR/.env
- Project env vars override global vars (Docker's last-wins behavior)
- Add log message for discoverability when global .env is loaded
- Document global .env usage in README with examples

Co-authored-by: GLM <zai-org@users.noreply.github.com>
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.

feat: support global .env file in AgentBox directory

1 participant