Skip to content

feat(ai): the prompt loader renders Jinja with project values #4633

Closed

Description

@dan-petty

Problem or Motivation

Values enter prompts in 4 ways: str.format (4 sites), chained str.replace (3 sites; one uses {{name}} markers in fill_prompt, src/devops_cli/roadmap/implement/implement_shell_policy.py:541), f-string concatenation (4 sites) and constants in Python. No prompt can take a repository fact. There are 37 load_task_prompt calls in 24 files. A probe shows that 67 of 72 task and persona files render byte-identical through a Jinja2 SandboxedEnvironment with StrictUndefined.

Proposed Solution & Architectural Design

  • Change load_task_prompt(name, **context) to render through one cached Jinja2 SandboxedEnvironment: StrictUndefined, keep_trailing_newline, no trim_blocks or lstrip_blocks, and autoescape off.
  • Add no new function and no ChoiceLoader. Read no prompt from a target repository, because a target that overrides a prompt can change the job rules.
  • All 37 call sites stay. Only the prompts that hold a project fact get context: stage_change, stage_split, stage_tools_research, implement_step and alternatives.
  • roadmap_implement_step.md gets its tables through context. Remove fill_prompt and its str.replace.
  • The personas reader and the benchmark reader stay plain text, because 3 benchmark files hold GitHub ${{ }} expressions.
  • stage_plan_record.prompt_version hashes the raw template plus the context values, so a call with no context does not raise.

Target Subsystem

AI & Agentic Review Pipeline (src/devops_cli/ai/task_loader.py).

Existing Tool Considered

Jinja2 3.1.6 is already a dependency, and its SandboxedEnvironment is the standard way to render a template with limited access. string.Template has no conditional blocks. LangChain PromptTemplate adds a dependency and uses Jinja2 or f-strings too. Cost of the wiring: about 40 lines and 1 test, about 150,000 tokens and 2 hours to build, about 3,000 tokens a month to keep.

Acceptance Criteria

  • A test renders each prompt name that the code uses, with its real context, and asserts no UndefinedError.
  • A test: for each prompt with no variables, the rendered text equals the file text.
  • A test: the rendered implement step prompt equals the fill_prompt output of today for devops-cli.
  • git grep -n 'fill_prompt\|PROGRAMS_PLACEHOLDER' src gives 0.
  • jinja2 stays the only template dependency (3.1.6).

Dependencies

#4630 (a missing prompt raises).

Source: #4627

Proposed design

Problem statement

Prompts take their values in four ways, and no prompt can take a repository fact. The change renders a task prompt through one cached Jinja2 SandboxedEnvironment when its caller passes a context, gives the implement step prompt its three tables through that context, and leaves a prompt with no context unrendered so the call-budget and benchmark readers keep their text. The missing-prompt behavior of #4630 is already in the tree; the project facts of the other four prompts are a follow-up.

Acceptance criteria

Requirement: Jinja2 renders a task prompt with context

load_task_prompt SHALL render a task prompt through one cached Jinja2 SandboxedEnvironment when the caller passes a context.

Scenario: A prompt takes a context value

  • WHEN A caller passes a value for a prompt that holds {{ value }}.
  • THEN The returned text holds the value in place of the marker.

Requirement: A missing value raises

The environment MUST raise jinja2.UndefinedError when a prompt names a value that the caller's context does not hold.

Scenario: A prompt names an unset value

  • WHEN A caller passes a context that holds another value but not missing to a prompt that holds {{ missing }}.
  • THEN load_task_prompt raises jinja2.UndefinedError.

Requirement: A prompt with no variables keeps its text

A prompt with no variables MUST return the text that the current loader returns.

Scenario: Each prompt with no variables

  • WHEN A test loads each prompt that holds no {{ }} marker.
  • THEN The returned text equals the text that the current loader returns for that prompt.

Requirement: Each used prompt loads with its context

A test MUST load each prompt name that the code uses with the context that its caller passes.

Scenario: Load every used prompt

  • WHEN A test loads each prompt name with its caller's context.
  • THEN No UndefinedError is raised.

Requirement: The implement step prompt gets its tables

The implement step prompt SHALL get its three tables through context.

Scenario: The implement step prompt gets its tables

  • WHEN The code builds the implement step prompt for devops-cli.
  • THEN The text holds the table rows that the caller passes as context.

Requirement: fill_prompt is removed

The source MUST NOT hold fill_prompt or PROGRAMS_PLACEHOLDER.

Scenario: Search the source

  • WHEN A search runs git grep -n 'fill_prompt\|PROGRAMS_PLACEHOLDER' src.
  • THEN The search gives no match.

Requirement: The implement step prompt matches today's output

The rendered implement step prompt MUST equal the output of today's fill_prompt for devops-cli.

Scenario: Compare with the recorded output

  • WHEN A test renders the implement step prompt for devops-cli with the new loader.
  • THEN The text equals the recorded fill_prompt output.

Requirement: Prompt version covers the context

stage_plan_record.prompt_version SHALL hash the raw template text and the context values.

Scenario: Two contexts give two versions

  • WHEN Two calls hash the same prompt with different context values.
  • THEN The two prompt versions differ, and a call with no context does not raise.

Requirement: A prompt read with no context keeps its markers

load_task_prompt MUST return the stripped file text without rendering when the caller passes no context.

Scenario: The call-budget and GitHub prompts keep their markers

  • WHEN A test loads roadmap_stage_tools.md and benchmark_embedding_eval_pairs.md with no context.
  • THEN The texts hold {{plan_calls_advised}} and ${{ github.workflow }}-${{ github.ref }}, and no UndefinedError is raised.

Requirement: No new template dependency

The change MUST NOT add a template dependency, and Jinja2 stays at version 3.1.6.

Scenario: Read the dependency list

  • WHEN A test reads the project dependency list.
  • THEN Jinja2 3.1.6 is present and no template library is added.

Existing Tool Considered

No alternatives check has run. Jinja2 3.1.6 is pinned in pyproject.toml:23 and is the project's chosen template engine (src/devops_cli/ai/knowledge_base/devops_cli/libraries/pyyaml_jinja2.md:25, :45); src/devops_cli/core/templating.py:43 already builds a cached SandboxedEnvironment with StrictUndefined. Jinja2 covers rendering with strict missing values, but it lacks prompt-aware context validation and prompt version hashing, and it reads GitHub ${{ }} expressions as Jinja, so the loader returns a prompt raw when no context is passed. The wiring adds about 40 lines and 2 tests. The written cost is about 150,000 tokens and 2 hours to build, and about 3,000 tokens a month to keep.

Key questions

  • Does the project already render templates through a cached Jinja2 SandboxedEnvironment?: Yes. src/devops_cli/core/templating.py:43 defines a cached template_environment() that returns a SandboxedEnvironment with StrictUndefined at src/devops_cli/core/templating.py:57. [sources: src/devops_cli/core/templating.py:43, src/devops_cli/core/templating.py:55, src/devops_cli/core/templating.py:57]
  • Where does the prompt filling code live today?: fill_prompt is at src/devops_cli/roadmap/implement/implement_shell_policy.py:551; it replaces the three {{ }} placeholders at src/devops_cli/roadmap/implement/implement_shell_policy.py:240 to :242 with str.replace at src/devops_cli/roadmap/implement/implement_shell_policy.py:554 to :556. [sources: src/devops_cli/roadmap/implement/implement_shell_policy.py:551, src/devops_cli/roadmap/implement/implement_shell_policy.py:240, src/devops_cli/roadmap/implement/implement_shell_policy.py:554]
  • Which template engine renders the task prompts?: Jinja2 3.1.6 through a SandboxedEnvironment. It is pinned at pyproject.toml:23, the project already builds one at src/devops_cli/core/templating.py:43, and the knowledge base names it the selected engine. (best practice) [sources: pyproject.toml:23, src/devops_cli/core/templating.py:43, src/devops_cli/ai/knowledge_base/devops_cli/libraries/pyyaml_jinja2.md:25]
    • Chosen: Jinja2 SandboxedEnvironment. It is already a dependency and the project already uses it with StrictUndefined.
    • Rejected: string.Template. It has no conditional blocks and no strict undefined.
    • Rejected: LangChain PromptTemplate. It adds a dependency and still uses Jinja2 or f-strings.
    • Rejected: pydantic_ai.template. The issue scopes the change to load_task_prompt and its markdown files.
  • Where does the Jinja2 environment live?: A module-level cached SandboxedEnvironment in src/devops_cli/ai/task_loader.py, built like template_environment() but with no loader and no trimming. (best practice) [sources: src/devops_cli/core/templating.py:43, src/devops_cli/ai/task_loader.py:16]
    • Chosen: module-level environment in task_loader. It renders prompt text and keeps the loader self-contained.
    • Rejected: reuse template_environment(). It loads JSON scaffolding templates through a FileSystemLoader and trims blocks.
    • Rejected: a new loader class. The issue says add no new function and no ChoiceLoader.
  • Which settings does the environment use?: StrictUndefined, keep_trailing_newline, no trim_blocks or lstrip_blocks, and autoescape off. The first two match template_environment(); no trimming keeps the byte-identical output, and autoescape is off because the prompts are Markdown. (best practice) [sources: src/devops_cli/core/templating.py:49, src/devops_cli/core/templating.py:57, src/devops_cli/core/templating.py:60, src/devops_cli/ai/task_loader.py:25]
    • Chosen: StrictUndefined, keep_trailing_newline, no trimming, no autoescape. It matches the current loader output and raises on a missing value.
    • Rejected: default undefined. A missing value would render empty and hide a defect.
    • Rejected: trim_blocks and lstrip_blocks. They would change the text of prompts that render byte-identical today.
    • Rejected: autoescape on. Prompts are Markdown text, not HTML.
  • Which prompts get context?: The implement step prompt gets its three tables through context in this change. The stage_change, stage_split, stage_tools_research and alternatives prompts get their project facts in a follow-up issue: the issue names no fact for them, and they hold no {{ }} marker today. (best practice) [sources: src/devops_cli/roadmap/implement/implement_shell_policy.py:240, src/devops_cli/ai/tasks/roadmap_implement_step.md:49, src/devops_cli/roadmap/implement/implement_step.py:423]
    • Chosen: implement step now, four prompts in a follow-up. The implement step has named facts and a recorded output; the other four have no fact to pass.
    • Rejected: all five prompts now. The issue names no project fact for the other four, so the work cannot be built as written.
    • Rejected: no prompt gets context. The implement step tables are the need of the issue.
  • Do the personas and benchmark readers change?: No. src/devops_cli/ai/personas/__init__.py:38 and src/devops_cli/ai/benchmark/tasks.py:12 read plain text. The third benchmark file with GitHub expressions, src/devops_cli/ai/tasks/benchmark_embedding_eval_pairs.md:66, is read through load_task_prompt at src/devops_cli/ai/benchmark/embedding_tasks.py:75; it stays raw because the caller passes no context. (best practice) [sources: src/devops_cli/ai/personas/init.py:38, src/devops_cli/ai/benchmark/tasks.py:12, src/devops_cli/ai/benchmark/embedding_tasks.py:75, src/devops_cli/ai/tasks/benchmark_embedding_eval_pairs.md:66]
    • Chosen: keep them plain text or raw. The GitHub ${{ }} expressions would be read as Jinja.
    • Rejected: render them through Jinja2. It would break the three benchmark files that hold GitHub expressions.
  • What does stage_plan_record.prompt_version hash?: The raw template text plus the context values. Today it hashes the texts that load_task_prompt returns at src/devops_cli/roadmap/stage/stage_plan_record.py:195; with no context the loader returns the stripped raw template at src/devops_cli/ai/task_loader.py:25, so the hash adds the context values and does not raise. (best practice) [sources: src/devops_cli/roadmap/stage/stage_plan_record.py:186, src/devops_cli/roadmap/stage/stage_plan_record.py:195, src/devops_cli/ai/task_loader.py:25]
    • Chosen: hash the raw template text and the context values. It needs no render, so a call with no context does not raise, and a context change gives a new version.
    • Rejected: hash the rendered text. The render can raise and changes with the context.
    • Rejected: hash the template text only. Two different contexts would share a version.
  • Does load_task_prompt render a prompt when the caller passes no context?: No. With no context it returns the stripped file text. The call-budget prompts at src/devops_cli/ai/tasks/roadmap_stage_tools.md:17 and src/devops_cli/ai/tasks/roadmap_stage_verify_tools.md:7, the embedding eval pairs at src/devops_cli/ai/benchmark/embedding_tasks.py:75, and the peer grader's single-brace markers at src/devops_cli/ai/benchmark/runner.py:634 keep working; a caller that passes a context gets StrictUndefined. (best practice) [sources: src/devops_cli/roadmap/stage/stage_tools.py:91, src/devops_cli/roadmap/stage/stage_tools.py:96, src/devops_cli/ai/tasks/roadmap_stage_tools.md:17, src/devops_cli/ai/benchmark/embedding_tasks.py:75, src/devops_cli/ai/benchmark/runner.py:634]
    • Chosen: render only when the caller passes a context. It keeps the call-budget and benchmark readers working and matches the issue's 'a call with no context does not raise'.
    • Rejected: always render and add a raw read. It changes more call sites and adds a parameter that the issue does not ask for.
    • Rejected: render by a list of prompt names. A name list duplicates the loader and drifts from the call sites.
  • Is refactor(ai): delete orphan prompts and the prompt fallbacks #4630 delivered?: Yes. load_task_prompt raises TaskPromptNotFoundError at src/devops_cli/ai/task_loader.py:23, and the delivered spec is openspec/specs/ai-task-prompts/spec.md:9. The change therefore needs no open dependency. [sources: src/devops_cli/ai/task_loader.py:23, openspec/specs/ai-task-prompts/spec.md:9, openspec/changes/archive/2026-11-11-4630-delete-orphan-prompts-and-the-prompt/proposal.md:11]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-17: in-use.
The repository already uses this tool.
Tool: Jinja2 (https://pypi.org/project/Jinja2), BSD License, last activity 2025-03-05, not maintained.

Activity

  1. added
    source/agent
    on Nov 11, 2026
  2. added
    type/feature
    scope/ai
    status/new
    value/medium
    effort/low
    on Nov 11, 2026
  3. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/feature. The feat prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. It improves maintainability of 37 prompt call sites but does not block users or fix a defect.
    • Value: Medium. It replaces four fragile string methods with one testable renderer and removes fill_prompt.
    • Effort: Low. The body states about 40 lines, 1 test, and 2 hours of work.
    • Related: feat(cli): template rendering engine sandboxing, pre-compiled ast caching & variable validation research #325, which asks for a different change. Both use Jinja2 SandboxedEnvironment, but #325 targets manifest and Dockerfile rendering while the candidate targets AI task prompt rendering in task_loader.py.
  4. added
    priority/p2-medium
    on Nov 11, 2026
  5. added
    status/ready
    and removed
    status/new
    on Nov 14, 2026
  6. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 0410772 (see proposed design in issue body).

    Moved to new items: #5638.

  7. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: The issue's need is to render task prompts with project values through a cached template engine with strict missing-value errors, and Jinja2's SandboxedEnvironment with StrictUndefined is exactly that engine: the searches confirmed Jinja2 is an open, maintained project (3.1.6, BSD-3-Clause) that the repository already depends on and already builds a sandboxed environment from. Its conditional blocks and strict undefined cover the rendering need, so the remaining work (cached loader, version hashing, raw reads) is project wiring rather than a missing tool.

    The repository already uses this tool.

    Tool: Jinja2 (https://pypi.org/project/Jinja2)

    • License: BSD License
    • Last activity: 2025-03-05
    • Maintained: no

    Covers:

    • Renders templates with {{ value }} markers and conditional blocks
    • SandboxedEnvironment limits template access; StrictUndefined raises UndefinedError on a missing value
    • keep_trailing_newline and no trimming keep byte-identical output for prompts with no variables
    • Already a project dependency pinned at 3.1.6, so no new template dependency is added

    Lacks:

    • No prompt-aware context validation beyond undefined-name errors
    • No prompt version hashing of the raw template plus context values
    • Reads GitHub ${{ }} expressions as Jinja, so the loader must return a prompt raw when no context is passed
    • The cached environment, the load_task_prompt change and the fill_prompt removal are project code, not provided by Jinja2

    Tools considered:

    Searches run:

    • Jinja2 SandboxedEnvironment StrictUndefined render prompt templates Python, results: 5
    • Python template engine render markdown prompt with strict undefined variables, results: 5
    • Jinja2 template engine documentation, results: 5

    Sources:

    Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.

  8. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 17, 2026
  9. removed
    status/in-review
    on Nov 17, 2026
  10. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-17 14:05 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/aisource/agenttype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions