Skip to content

Add Agent Prompt template system with versioning and mode switching#154

Closed
esafwan wants to merge 6 commits into
developfrom
claude/prompt-templates-versioning-ydN3t
Closed

Add Agent Prompt template system with versioning and mode switching#154
esafwan wants to merge 6 commits into
developfrom
claude/prompt-templates-versioning-ydN3t

Conversation

@esafwan
Copy link
Copy Markdown
Contributor

@esafwan esafwan commented Feb 20, 2026

Summary

Introduces a comprehensive prompt template management system for agents, enabling reusable prompt templates with versioning, forking, and flexible prompt mode switching between local and template-based approaches.

Key Changes

New DocTypes

  • Agent Prompt: Immutable versioned prompt templates with support for:

    • Version history tracking via previous_version and prompt_group fields
    • Forking to create independent lineages
    • Visibility controls (Public/App/Private)
    • Category organization and tagging
    • System vs. user-managed prompts
  • Agent Prompt Category: Hierarchical categorization for prompt templates with icon and color support

Agent Enhancements

  • Added prompt_mode field to switch between "Local" (inline instructions) and "Template" (linked prompt) modes
  • Added template linking fields: agent_prompt, prompt_version_locked, template_version_at_attach, copied_from_prompt
  • Updated validation to support both prompt modes
  • Modified plan generation to use centralized prompt resolution

New API Module (huf/ai/prompt_api.py)

Whitelisted server-side operations for prompt management:

  • create_new_version(): Create immutable versions with automatic agent link updates for unlocked agents
  • fork_prompt(): Create independent prompt lineages
  • detach_from_template(): Convert template-mode agents to local mode with traceability
  • save_as_template(): Save agent instructions as reusable templates
  • attach_template(): Link agents to templates with optional version locking
  • get_version_history(): Retrieve full version lineage

Prompt Resolution (huf/ai/prompt_resolver.py)

Centralized resolve_prompt() function that:

  • Handles both Local and Template modes transparently
  • Supports version-locked agents (fixed to specific versions)
  • Automatically uses latest versions for unlocked agents
  • Provides consistent prompt resolution across sync runs, streaming, and orchestration

Integration Updates

  • Updated agent_integration.py, agent_scheduler.py, and orchestrator.py to use centralized prompt resolution instead of direct instructions field access

Implementation Details

  • Prompt versioning uses an immutable model where new versions create new documents rather than modifying existing ones
  • prompt_group field ties all versions of a prompt together for efficient history queries
  • Version locking allows agents to stay on specific prompt versions while others auto-upgrade
  • Backward compatible: agents default to "Local" mode using existing instructions field
  • System prompts are protected from user versioning

Introduce Agent Prompt Category as a standalone DocType for organizing
and grouping prompt templates. Supports hierarchical categories via
parent_category self-link, with icon and color fields for UI display.

This is the first step toward treating prompts as first-class reusable
artifacts in HUF, preparing for prompt marketplace and library UX.

https://claude.ai/code/session_01CotwQ8srHPD1MozVfF3RFM
Introduce Agent Prompt as a first-class reusable artifact in HUF.
Implements the immutable version model (Git-like) where each update
creates a new row linked via previous_version. Key design decisions:

- prompt_group: shared identifier across all versions of a lineage
- forked_from: tracks lineage when prompts are forked/copied
- is_latest: marks the current head version in each lineage
- visibility: Public/App/Private for future marketplace support
- is_system: protects HUF-managed prompts from user edits
- slug: URL-friendly identifier, auto-generated from title
- autoname: hash - supports immutable versioning without name conflicts

This model supports version pinning, prompt marketplace, cross-app
reuse, diffing, and audit trails.

https://claude.ai/code/session_01CotwQ8srHPD1MozVfF3RFM
Add prompt template support to Agent with backward-compatible defaults:

- prompt_mode: Select (Local/Template), defaults to "Local"
- agent_prompt: Link to Agent Prompt (visible in Template mode)
- prompt_version_locked: lock agent to a specific template version
- template_version_at_attach: records version at time of attachment
- copied_from_prompt: hidden traceability link for detach operations

Existing behavior is preserved: all current agents default to "Local"
mode and continue using the instructions field directly. The
instructions field gains depends_on for Local mode but remains
functional for all backend reads.

Validation updated to support both modes: Template mode requires
agent_prompt link, Local mode requires instructions text.

https://claude.ai/code/session_01CotwQ8srHPD1MozVfF3RFM
Add prompt_resolver.py as the single source of truth for resolving an
agent's effective prompt. All backend paths (sync runs, streaming,
triggers, orchestration) now go through resolve_prompt() instead of
reading agent_doc.instructions directly.

Resolution rules:
- Local mode: returns agent_doc.instructions (backward compatible)
- Template mode (unlocked): loads prompt_body from linked Agent Prompt
- Template mode (locked): walks the version lineage via prompt_group
  to find the exact version matching template_version_at_attach

Also updates AgentManager.create_agent() and Agent.generate_default_plan()
to use resolve_prompt() for consistent behavior.

https://claude.ai/code/session_01CotwQ8srHPD1MozVfF3RFM
Add prompt_api.py with server-side operations for the prompt template
lifecycle:

- create_new_version: immutable versioning — marks old as not-latest,
  creates new row, auto-updates unlocked agents
- fork_prompt: creates independent copy with forked_from lineage
- detach_from_template: copies resolved prompt to local, records
  copied_from_prompt for traceability
- save_as_template: creates Agent Prompt from agent's local instructions
- attach_template: switches agent to Template mode with optional
  version locking
- get_version_history: returns full version lineage via prompt_group

All methods are @frappe.whitelist() and follow Frappe patterns for
permissions and error handling.

https://claude.ai/code/session_01CotwQ8srHPD1MozVfF3RFM
Update remaining places that read agent_doc.instructions directly to
use resolve_prompt() instead:

- agent_scheduler.py: scheduled agent runs now resolve templates
- orchestrator.py: plan recreation uses resolved prompt
- agent.py on_update: detect prompt changes from template switches
- agent.py after_insert: pass resolved prompt to orchestration

This ensures agents in Template mode work correctly across all
execution paths: sync runs, streaming, scheduled triggers, and
orchestration planning.

https://claude.ai/code/session_01CotwQ8srHPD1MozVfF3RFM
@esafwan esafwan closed this Feb 20, 2026
@esafwan esafwan deleted the claude/prompt-templates-versioning-ydN3t branch February 20, 2026 02:18
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.

2 participants