Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pkg/workflow/compiler_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (

var compilerJobsLog = logger.New("workflow:compiler_jobs")

// Pre-compiled regexes for performance (avoid recompilation in hot paths)
var (
// runtimeImportMacroRe matches runtime-import macros: {{#runtime-import filepath}} or {{#runtime-import? filepath}}
runtimeImportMacroRe = regexp.MustCompile(`\{\{#runtime-import\??[ \t]+([^\}]+)\}\}`)
)

// This file contains job building functions extracted from compiler.go
// These functions are responsible for constructing the various jobs that make up
// a compiled agentic workflow, including activation, main, safe outputs, and custom jobs.
Expand Down Expand Up @@ -449,11 +455,8 @@ func containsRuntimeImports(markdownContent string) bool {
return false
}

// Pattern: {{#runtime-import filepath}} or {{#runtime-import? filepath}}
// Match any runtime-import macro
macroPattern := `\{\{#runtime-import\??[ \t]+([^\}]+)\}\}`
macroRe := regexp.MustCompile(macroPattern)
matches := macroRe.FindAllStringSubmatch(markdownContent, -1)
// Use pre-compiled regex from package level for performance
matches := runtimeImportMacroRe.FindAllStringSubmatch(markdownContent, -1)
for _, match := range matches {
if len(match) > 1 {
filepath := strings.TrimSpace(match[1])
Expand Down
14 changes: 9 additions & 5 deletions pkg/workflow/expression_extraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (

var expressionExtractionLog = logger.New("workflow:expression_extraction")

// Pre-compiled regexes for performance (avoid recompilation in hot paths)
var (
// expressionExtractionRegex matches GitHub Actions expressions: ${{ ... }}
// Uses (?s) flag for dotall mode, non-greedy matching
expressionExtractionRegex = regexp.MustCompile(`\$\{\{(.*?)\}\}`)
)

// ExpressionMapping represents a mapping between a GitHub expression and its environment variable
type ExpressionMapping struct {
Original string // The original ${{ ... }} expression
Expand Down Expand Up @@ -40,11 +47,8 @@ func NewExpressionExtractor() *ExpressionExtractor {
func (e *ExpressionExtractor) ExtractExpressions(markdown string) ([]*ExpressionMapping, error) {
expressionExtractionLog.Printf("Extracting expressions from markdown: content_length=%d", len(markdown))

// Regular expression to match GitHub Actions expressions: ${{ ... }}
// Use (?s) flag for dotall mode, non-greedy matching
expressionRegex := regexp.MustCompile(`\$\{\{(.*?)\}\}`)

matches := expressionRegex.FindAllStringSubmatch(markdown, -1)
// Use pre-compiled regex from package level for performance
matches := expressionExtractionRegex.FindAllStringSubmatch(markdown, -1)
expressionExtractionLog.Printf("Found %d expression matches", len(matches))

for _, match := range matches {
Expand Down
10 changes: 8 additions & 2 deletions pkg/workflow/repo_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (

var repoMemoryLog = logger.New("workflow:repo_memory")

// Pre-compiled regexes for performance (avoid recompilation in hot paths)
var (
// branchPrefixValidPattern matches valid branch prefix characters (alphanumeric, hyphens, underscores)
branchPrefixValidPattern = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
)

// RepoMemoryConfig holds configuration for repo-memory functionality
type RepoMemoryConfig struct {
BranchPrefix string `yaml:"branch-prefix,omitempty"` // branch prefix (default: "memory")
Expand Down Expand Up @@ -74,8 +80,8 @@ func validateBranchPrefix(prefix string) error {
}

// Check for alphanumeric and branch-friendly characters (alphanumeric, hyphens, underscores)
validPattern := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
if !validPattern.MatchString(prefix) {
// Use pre-compiled regex from package level for performance
if !branchPrefixValidPattern.MatchString(prefix) {
return fmt.Errorf("branch-prefix must contain only alphanumeric characters, hyphens, and underscores, got '%s'", prefix)
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/workflow/template_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ import (

var templateValidationLog = logger.New("workflow:template_validation")

// Pre-compiled regexes for performance (avoid recompilation in hot paths)
var (
// templateRegionPattern matches template conditional blocks with their content
// Uses (?s) for dotall mode, .*? (non-greedy) with \s* to handle expressions with or without trailing spaces
templateRegionPattern = regexp.MustCompile(`(?s)\{\{#if\s+.*?\s*\}\}(.*?)\{\{/if\}\}`)
)

// validateNoIncludesInTemplateRegions checks that import directives
// are not used inside template conditional blocks ({{#if...}}{{/if}})
func validateNoIncludesInTemplateRegions(markdown string) error {
templateValidationLog.Print("Validating that imports are not inside template regions")

// Find all template regions by matching {{#if...}}...{{/if}} blocks
// This regex matches template conditional blocks with their content
// Uses .*? (non-greedy) with \s* to handle expressions with or without trailing spaces
templateRegionPattern := regexp.MustCompile(`(?s)\{\{#if\s+.*?\s*\}\}(.*?)\{\{/if\}\}`)

// Use pre-compiled regex from package level for performance
matches := templateRegionPattern.FindAllStringSubmatch(markdown, -1)
templateValidationLog.Printf("Found %d template regions to validate", len(matches))

Expand Down
Loading