Skip to content

feat(agent): system prompt templating and global instruction propagation#130

Draft
FelipeSuarez16 wants to merge 4 commits into
redpanda-data:mainfrom
FelipeSuarez16:worktree-system-prompt-templating
Draft

feat(agent): system prompt templating and global instruction propagation#130
FelipeSuarez16 wants to merge 4 commits into
redpanda-data:mainfrom
FelipeSuarez16:worktree-system-prompt-templating

Conversation

@FelipeSuarez16
Copy link
Copy Markdown

Description

This PR implements dynamic system prompt templating and a mechanism for propagating global instructions through agent trees, addressing issue #99.

Key Features

  1. Dynamic Prompt Templating:

    • Implements a regex-based template engine supporting {key}\ placeholders.
    • Supports alphanumeric keys, underscores, hyphens, and dots (e.g., {user.id}).
    • JSON-Safe: Designed to avoid collisions with standard JSON syntax.
    • Automatic Variables: Automatically injects {current_date}\ (UTC, ISO format) into all system prompts.
  2. Global Instructions Propagation:

    • Introduced \agent.ContextWithGlobalInstructions\ and \agent.GlobalInstructions.
    • Uses \context.Context\ to propagate directives across multi-agent trees (including \AgentTool\ calls) without polluting session state.
    • Instructions are structurally appended to the base prompt using a clear Markdown separator.
  3. Security & Robustness:

    • Prompt Injection Mitigation: Restricts template injection to primitive types (\string, \int, \tool) and sanitizes strings by stripping newlines.
    • Defensive Programming: Added nil checks for \InvocationMetadata\ and \Session\ references to prevent runtime panics.
  4. Standards & Documentation:

    • Comprehensive Design Document added at \docs/design/system-prompt-templating.md.
    • All new tests follow project conventions (using \ .Parallel()).
    • Added verification and integration scripts for both mocked and real local LLMs (Ollama).

Implementation Details

  • Core: New \agent/template.go\ and \agent/context.go.
  • Integration: Updated \LLMAgent.resolveSystemPrompt\ to orchestrate resolution before model calls.
  • Tests:
    • \agent/template_test.go: Unit tests for replacement logic and JSON safety.
    • \agent/llmagent/dynamic_prompt_test.go: Integration tests for metadata resolution.
    • \agent/llmagent/agenttool_propagation_test.go: End-to-end verification of global instruction flow through tool calls.

Verification

  • Local verification script: \verify_features.go\
  • Local Ollama test: \test_ollama.go\

Fixes #99

Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@FelipeSuarez16 FelipeSuarez16 marked this pull request as draft May 7, 2026 16:08
This change replaces the previous regex-based templating with a flexible
InstructionProvider callback. This delegates prompt generation to the user,
simplifying the SDK and avoiding hardcoded templating engines.

It also introduces WithGlobalInstruction for static directives and supports
context-based instruction propagation through the invocation tree.

Key changes:
- Add WithInstructionProvider and InstructionProvider type.
- Add WithGlobalInstruction functional option.
- Refactor resolveSystemPrompt to handle fallback and merging.
- Remove rejected regex templating engine.
- Add comprehensive tests for fallback and merging logic.
@FelipeSuarez16 FelipeSuarez16 force-pushed the worktree-system-prompt-templating branch from a797d7d to 69a2bf4 Compare May 7, 2026 19:24
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.

System prompt templating with per-invocation variable injection

1 participant