fix(formula): ignore undeclared handlebars in description text (gt-ky9loa)

When VarDefs exists (from a cooked formula), variables found in text but
NOT declared in VarDefs are now skipped rather than treated as required.
This allows formulas to use handlebars-style placeholders in descriptions
as documentation for LLM agents without them being parsed as required
input variables.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
ruby 2026-01-29 19:05:44 -08:00 • committed by Aaron Leiby
commit 9e807505c0
2 changed files with 110 additions and 10 deletions

View file

@ -796,25 +796,25 @@ func extractAllVariables(subgraph *TemplateSubgraph) []string {
func extractRequiredVariables(subgraph *TemplateSubgraph) []string {
allVars := extractAllVariables(subgraph)
// If no VarDefs, assume all variables are required
if subgraph.VarDefs == nil || len(subgraph.VarDefs) == 0 {
// If no VarDefs, assume all variables are required (legacy template behavior)
if subgraph.VarDefs == nil {
return allVars
}
// Filter to only required variables (no default and marked as required, or not defined in VarDefs)
// VarDefs exists (from a cooked formula) - only declared variables matter.
// Variables in text but NOT in VarDefs are ignored - they're documentation
// handlebars meant for LLM agents, not formula input variables (gt-ky9loa).
var required []string
for _, v := range allVars {
def, exists := subgraph.VarDefs[v]
// A variable is required if:
// 1. It's not defined in VarDefs at all, OR
// 2. It's defined with Required=true and no Default, OR
// 3. It's defined with no Default (even if Required is false)
if !exists {
required = append(required, v)
} else if def.Default == "" {
// Not a declared formula variable - skip (documentation handlebars)
continue
}
// A declared variable is required if it has no default
if def.Default == "" {
required = append(required, v)
}
// If exists and has default, it's not required
}
return required
}

View file

@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/formula"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
)
@ -828,3 +829,102 @@ func TestLoadTemplateSubgraphWithManyChildren(t *testing.T) {
}
})
}
// TestExtractRequiredVariables_IgnoresUndeclaredVars tests that handlebars in
// description text that are NOT defined in VarDefs are ignored (gt-ky9loa).
func TestExtractRequiredVariables_IgnoresUndeclaredVars(t *testing.T) {
tests := []struct {
name string
issues []*types.Issue
varDefs map[string]formula.VarDef
wantRequired []string
}{
{
name: "declared var without default is required",
issues: []*types.Issue{
{Title: "Deploy {{component}}", Description: "Deploy the component"},
},
varDefs: map[string]formula.VarDef{
"component": {Required: true},
},
wantRequired: []string{"component"},
},
{
name: "declared var with default is not required",
issues: []*types.Issue{
{Title: "Deploy {{component}}", Description: "Deploy the component"},
},
varDefs: map[string]formula.VarDef{
"component": {Default: "api"},
},
wantRequired: []string{},
},
{
name: "undeclared var in description is ignored when VarDefs exists",
issues: []*types.Issue{
{
Title: "Generate report",
Description: "Output format:\n**Ready**: {{ready_count}}\n**Done**: {{done_count}}",
},
},
varDefs: map[string]formula.VarDef{
// VarDefs exists but doesn't include ready_count or done_count
// These are documentation handlebars, not formula variables
},
wantRequired: []string{},
},
{
name: "mix of declared and undeclared vars",
issues: []*types.Issue{
{
Title: "Deploy {{component}} to {{env}}",
Description: "Shows: {{status_count}} items processed",
},
},
varDefs: map[string]formula.VarDef{
"component": {Required: true},
"env": {Default: "prod"},
// status_count is NOT declared - it's documentation
},
wantRequired: []string{"component"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
subgraph := &TemplateSubgraph{
Issues: tt.issues,
VarDefs: tt.varDefs,
}
got := extractRequiredVariables(subgraph)
// Convert to map for easier comparison
gotMap := make(map[string]bool)
for _, v := range got {
gotMap[v] = true
}
wantMap := make(map[string]bool)
for _, v := range tt.wantRequired {
wantMap[v] = true
}
if len(got) != len(tt.wantRequired) {
t.Errorf("extractRequiredVariables() = %v, want %v", got, tt.wantRequired)
return
}
for _, v := range tt.wantRequired {
if !gotMap[v] {
t.Errorf("extractRequiredVariables() missing expected var %q, got %v", v, got)
}
}
for _, v := range got {
if !wantMap[v] {
t.Errorf("extractRequiredVariables() has unexpected var %q, want %v", v, tt.wantRequired)
}
}
})
}
}