Skip to content

fix(runtime): pass DAG level variables to executor config evaluation properly#1448

Merged
yottahmd merged 3 commits intomainfrom
mail-body-variable
Dec 6, 2025
Merged

fix(runtime): pass DAG level variables to executor config evaluation properly#1448
yottahmd merged 3 commits intomainfrom
mail-body-variable

Conversation

@yottahmd
Copy link
Copy Markdown
Collaborator

@yottahmd yottahmd commented Dec 6, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Special environment variables (DAG_NAME, DAG_RUN_ID, DAG_RUN_LOG_FILE) are now properly accessible in failure handlers during DAG execution.
  • Tests

    • Added integration tests to verify environment variable availability in handlers.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 6, 2025

Walkthrough

The PR refactors environment variable handling in the runtime package by replacing the VariablesMap() method with AllEnvs() and AllEnvsMap() functions that combine DAGContext, Envs, and Variables. A BaseEnv field is added to DAGContext. Call sites are updated to use the new API, and an integration test verifies that special environment variables are available in handlers.

Changes

Cohort / File(s) Summary
Environment API refactoring
internal/runtime/env.go
Replaces Env.VariablesMap() with Env.AllEnvs() returning a string slice of "key=value" entries combined from DAGContext, Envs, and Variables. Adds top-level helpers AllEnvs(ctx) and AllEnvsMap(ctx).
Environment test updates
internal/runtime/env_test.go
Updates tests to use new AllEnvsMap(ctx) API. Extends DAGContext with BaseEnv field for carrying base environment configuration through execution context.
Evaluation layer integration
internal/runtime/eval.go
Changes EvalObject call site to pass AllEnvsMap(ctx) instead of GetEnv(ctx).VariablesMap().
Evaluation test refactoring
internal/runtime/eval_test.go
Introduces setupTestContext() helper to initialize DAGContext with base environment. Refactors tests to use the new helper for consistent context setup.
Integration test
internal/integration/base_dag_test.go
Adds TestBaseDAGSpecialEnvVarsInHandler to verify that special environment variables (DAG_NAME, DAG_RUN_ID, DAG_RUN_LOG_FILE) are properly injected into failure handler execution.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Environment variable merging logic: The new AllEnvs() method combines multiple sources (DAGContext, Envs, Variables) with specific precedence semantics that should be verified
  • API surface changes: Removal of VariablesMap() and introduction of new AllEnvs()/AllEnvsMap() functions requires checking all call sites for correctness
  • DAGContext expansion: The addition of the BaseEnv field to DAGContext affects execution context threading through the system
  • Integration test validation: The new handler test should be reviewed to ensure it properly exercises the environment variable injection path

Poem

🐰 The variables now flow in harmony true,
AllEnvs combines what DAG_RUN once knew,
From context and environment, one path unfolds,
A handler receives what the system now holds!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 41.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing runtime to properly pass DAG-level variables to executor config evaluation, which aligns with the changes in env.go, eval.go, and test files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mail-body-variable

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
internal/integration/base_dag_test.go (1)

26-35: Optionally assert concrete values and harden handler command quoting

Right now you only check that DAG_NAME, DAG_RUN_ID, and DAG_RUN_LOG_FILE are present and non‑empty. For a stronger regression guard, consider parsing the file and asserting they match dag.Name, dagRunID, and logFile. Also, since outputFile is interpolated into a shell script, wrapping it in quotes (e.g. ">> %q" via fmt.Sprintf) would make the test resilient to paths with spaces.

Also applies to: 84-95

internal/runtime/eval_test.go (1)

7-26: Shared DAGContext/BaseEnv test setup is appropriate; update comment for clarity

setupTestContext gives the EvalObject-focused tests a properly initialized DAGContext (including BaseEnv), which fits the new AllEnvs(AllEnvsMap) behavior, and its reuse across tests keeps setups consistent. The file-level note about “these tests use execution.SetupDAGContext” is now partly outdated though; consider updating it to mention setupTestContext/WithDAGContext so future readers don’t misinterpret how the context is constructed.

Also applies to: 171-207, 214-221, 323-329, 548-556

internal/runtime/env.go (1)

56-67: AllEnvs(/AllEnvsMap) aggregation looks correct; consider documenting precedence

The new Env.AllEnvs + AllEnvsMap correctly layer DAGContext envs, step Envs, and Variables with “later overrides earlier”, matching TestEnv_AllEnvsMap (e.g. variables winning over Envs). To avoid confusion alongside the detailed precedence comment on EvalString and the UserEnvsMap doc, it would be helpful to add a brief precedence note here (and, if necessary, reconcile the UserEnvsMap comment with the actual overwrite behavior).

Also applies to: 379-396

internal/runtime/env_test.go (1)

13-19: AllEnvsMap tests match implementation; optional extra coverage for BaseEnv/DAG env

TestEnv_AllEnvsMap does a good job validating the merged map (variables + step envs + step name) and the intended precedence where variables override Envs. If you want even stronger guarantees, you could add one case that seeds BaseEnv (e.g. via config.NewBaseEnv([]string{"BASE_KEY=base"})) or DAG Env and asserts those keys also appear in AllEnvsMap, but the current coverage is already reasonable.

Also applies to: 21-129

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 306bfa3 and aadc9f8.

📒 Files selected for processing (5)
  • internal/integration/base_dag_test.go (1 hunks)
  • internal/runtime/env.go (2 hunks)
  • internal/runtime/env_test.go (3 hunks)
  • internal/runtime/eval.go (1 hunks)
  • internal/runtime/eval_test.go (6 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Backend entrypoint in cmd/ orchestrates the scheduler and CLI; runtime, persistence, and service layers sit under internal/* (for example internal/runtime, internal/persistence)
Keep Go files gofmt/goimports clean; use tabs, PascalCase for exported symbols (SchedulerClient), lowerCamelCase for locals, and Err... names for package-level errors
Repository linting relies on golangci-lint; prefer idiomatic Go patterns, minimal global state, and structured logging helpers in internal/common

Files:

  • internal/runtime/eval.go
  • internal/integration/base_dag_test.go
  • internal/runtime/eval_test.go
  • internal/runtime/env.go
  • internal/runtime/env_test.go
**/*_test.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*_test.go: Co-locate Go tests as *_test.go; favour table-driven cases and cover failure paths
Use stretchr/testify/require and shared fixtures from internal/test instead of duplicating mocks

Files:

  • internal/integration/base_dag_test.go
  • internal/runtime/eval_test.go
  • internal/runtime/env_test.go
🧠 Learnings (2)
📚 Learning: 2025-12-04T10:34:17.051Z
Learnt from: CR
Repo: dagu-org/dagu PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-04T10:34:17.051Z
Learning: Applies to **/*_test.go : Co-locate Go tests as `*_test.go`; favour table-driven cases and cover failure paths

Applied to files:

  • internal/integration/base_dag_test.go
  • internal/runtime/eval_test.go
  • internal/runtime/env_test.go
📚 Learning: 2025-12-04T10:34:17.051Z
Learnt from: CR
Repo: dagu-org/dagu PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-04T10:34:17.051Z
Learning: Applies to **/*_test.go : Use `stretchr/testify/require` and shared fixtures from `internal/test` instead of duplicating mocks

Applied to files:

  • internal/runtime/env_test.go
🧬 Code graph analysis (5)
internal/runtime/eval.go (1)
internal/runtime/env.go (1)
  • AllEnvsMap (386-396)
internal/integration/base_dag_test.go (1)
internal/core/spec/loader.go (1)
  • WithBaseConfig (42-46)
internal/runtime/eval_test.go (2)
internal/common/config/env.go (2)
  • NewBaseEnv (15-17)
  • BaseEnv (9-11)
internal/core/execution/context.go (2)
  • DAGContext (16-26)
  • WithDAGContext (173-175)
internal/runtime/env.go (2)
internal/core/execution/context.go (1)
  • DAGContext (16-26)
internal/cmd/context.go (1)
  • Context (38-53)
internal/runtime/env_test.go (5)
internal/runtime/env.go (3)
  • Env (24-54)
  • WithEnv (366-368)
  • AllEnvsMap (386-396)
internal/core/dag.go (1)
  • DAG (33-135)
internal/common/config/env.go (2)
  • NewBaseEnv (15-17)
  • BaseEnv (9-11)
internal/core/execution/context.go (2)
  • DAGContext (16-26)
  • WithDAGContext (173-175)
internal/core/step.go (1)
  • Step (13-77)
🪛 Gitleaks (8.30.0)
internal/runtime/env_test.go

[high] 49-49: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test on ubuntu-latest
🔇 Additional comments (2)
internal/integration/base_dag_test.go (1)

18-82: Integration coverage for handler env vars looks solid

The test setup and assertions correctly exercise a failing DAG with a base-config failure handler and confirm the handler runs and sees the special env vars; structure and use of test.Setup/agent wiring look good.

internal/runtime/eval.go (1)

24-28: EvalObject now correctly uses unified env aggregation

Switching EvalObject to pass AllEnvsMap(ctx) aligns object evaluation with the new combined env model (DAG context + step envs + variables) and matches the dedicated tests you added.

@yottahmd yottahmd merged commit d52bc9f into main Dec 6, 2025
5 checks passed
@yottahmd yottahmd deleted the mail-body-variable branch December 6, 2025 11:13
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.74%. Comparing base (306bfa3) to head (aadc9f8).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1448      +/-   ##
==========================================
- Coverage   59.75%   59.74%   -0.01%     
==========================================
  Files         187      187              
  Lines       21093    21095       +2     
==========================================
  Hits        12604    12604              
- Misses       7175     7176       +1     
- Partials     1314     1315       +1     
Files with missing lines Coverage Δ
internal/runtime/env.go 61.25% <100.00%> (+0.61%) ⬆️
internal/runtime/eval.go 88.88% <100.00%> (-0.59%) ⬇️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 306bfa3...aadc9f8. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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