Skip to content

Pass globals context to env block template rendering#221

Merged
anticomputer merged 5 commits intomainfrom
anticomputer/env-globals
Apr 17, 2026
Merged

Pass globals context to env block template rendering#221
anticomputer merged 5 commits intomainfrom
anticomputer/env-globals

Conversation

@anticomputer
Copy link
Copy Markdown
Contributor

Problem

Task env: blocks use swap_env() for template rendering, but swap_env only has access to the env() function -- not globals. This means {{ globals.X }} references in env blocks raise UndefinedError, even though they work fine in user_prompt blocks.

This matters for taskflows that need to pass global variables into container configuration (e.g. using a GHSA ID as a container persist key).

Fix

  • swap_env() now accepts an optional context dict
  • TmpEnv accepts and forwards context to swap_env()
  • The runner passes {'globals': global_variables} when constructing TmpEnv

Backward compatible: context defaults to None, all existing env() calls work unchanged.

swap_env and TmpEnv now accept an optional context dict so that
{{ globals.X }} references work in task env blocks, not just in
user_prompt templates. The runner passes globals through when
constructing TmpEnv.

Backward compatible: context defaults to None, existing env() calls
work unchanged.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Enables task env: block template rendering to access globals (in addition to env()), aligning env rendering capabilities with user_prompt templating.

Changes:

  • Extended swap_env() to accept an optional rendering context and pass it into Jinja rendering.
  • Updated TmpEnv to accept/forward the optional context when rendering environment variable templates.
  • Passed {"globals": global_variables} from the runner when constructing TmpEnv.
Show a summary per file
File Description
src/seclab_taskflow_agent/runner.py Passes globals into TmpEnv so task env: templates can reference globals.*.
src/seclab_taskflow_agent/env_utils.py Adds optional template context support to swap_env()/TmpEnv and renders env values with that context.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 3

Comment thread src/seclab_taskflow_agent/env_utils.py Outdated
Comment thread src/seclab_taskflow_agent/env_utils.py Outdated
Comment thread src/seclab_taskflow_agent/runner.py
- Clarify docstring: globals only available when caller provides context
- Filter reserved Jinja globals (e.g. env) from context to prevent
  callers from accidentally shadowing built-in template helpers
- Add test_env_utils.py with 9 tests covering: globals rendering,
  missing context raises, reserved key protection, env() backward
  compat, TmpEnv context forwarding, env restoration
Copilot AI review requested due to automatic review settings April 15, 2026 20:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Enables Jinja template rendering inside task env: blocks to access globals (matching user_prompt behavior) by threading a caller-provided context through swap_env()/TmpEnv.

Changes:

  • Add optional context parameter to swap_env() and forward it through TmpEnv.
  • Pass {"globals": global_variables} when constructing TmpEnv in the runner.
  • Add unit tests covering globals rendering and reserved-key behavior.
Show a summary per file
File Description
tests/test_env_utils.py Adds coverage for swap_env/TmpEnv with globals context and reserved-key filtering.
src/seclab_taskflow_agent/runner.py Ensures task env-block rendering receives globals via TmpEnv(..., context=...).
src/seclab_taskflow_agent/env_utils.py Implements optional template context in swap_env and plumbs it through TmpEnv.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 3

Comment thread src/seclab_taskflow_agent/env_utils.py
Comment thread src/seclab_taskflow_agent/env_utils.py
Comment thread tests/test_env_utils.py Outdated
Copilot AI review requested due to automatic review settings April 15, 2026 21:02
@anticomputer anticomputer force-pushed the anticomputer/env-globals branch from ea07aa1 to 2c05b9c Compare April 15, 2026 21:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes Jinja template rendering in task env: blocks so they can reference global variables (e.g. {{ globals.X }}), matching the behavior already supported in user_prompt rendering.

Changes:

  • Extend swap_env() to accept an optional template context and pass it through TmpEnv.
  • Update the runner to provide {"globals": global_variables} when rendering task env blocks.
  • Add tests covering globals rendering, reserved key behavior, and TmpEnv rollback.
Show a summary per file
File Description
src/seclab_taskflow_agent/env_utils.py Adds context-aware Jinja rendering for env strings and forwards context through TmpEnv; adds rollback behavior on failure.
src/seclab_taskflow_agent/runner.py Passes taskflow globals into TmpEnv so task env: blocks can render {{ globals.* }}.
tests/test_env_utils.py New tests for swap_env()/TmpEnv with globals context and rollback behavior.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment on lines 49 to +52
except jinja2.UndefinedError as e:
# Convert Jinja undefined to LookupError for compatibility
raise LookupError(str(e))
except jinja2.TemplateError:
# Not a template or failed to render, return as-is
return s
except jinja2.TemplateError as e:
raise LookupError(f"Template rendering failed for: {s!r}: {e}")
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

Catching jinja2.TemplateError and re-raising LookupError is a behavior change from the previous implementation (which returned the original string on template syntax/render errors). This can break existing configs that contain literal {{/{% sequences or other non-Jinja brace usage, and it also changes downstream behavior where callers treat LookupError as “missing env var” (e.g. some code deletes the entry on LookupError). If backward compatibility is required, consider preserving the old behavior for TemplateError (return s as-is) or introducing an explicit “strict” mode so invalid templates don’t get conflated with missing variables.

Copilot uses AI. Check for mistakes.
- TmpEnv.__enter__ rolls back already-set env vars if swap_env raises
  mid-loop, preventing leaked env state on enter failures
- Remove silent TemplateError swallow: malformed templates now raise
  LookupError with the source string and error detail instead of
  silently passing through as literals
- Convert tests to plain pytest classes (drop unittest.TestCase)
- Add test_tmpenv_rollback_on_error for partial modification safety
@anticomputer anticomputer force-pushed the anticomputer/env-globals branch from 2c05b9c to a8fcc23 Compare April 16, 2026 14:05
@anticomputer anticomputer merged commit f139ac8 into main Apr 17, 2026
9 checks passed
@anticomputer anticomputer deleted the anticomputer/env-globals branch April 17, 2026 18:12
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.

3 participants