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
45 changes: 45 additions & 0 deletions pkg/parser/import_topological_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,48 @@ tools:
assert.Equal(t, "a-dependency.md", result.ImportedFiles[0])
assert.Equal(t, "z-parent.md", result.ImportedFiles[1])
}

func TestImportTopologicalSortStableAcrossRuns(t *testing.T) {
tempDir := testutil.TempDir(t, "import-topo-stable-*")

files := map[string]string{
"root.md": `---
imports:
- b.md
- a.md
---`,
"a.md": `---
imports:
- c.md
tools:
a-tool: {}
---`,
"b.md": `---
tools:
b-tool: {}
---`,
"c.md": `---
tools:
c-tool: {}
---`,
}

for filename, content := range files {
filePath := filepath.Join(tempDir, filename)
err := os.WriteFile(filePath, []byte(content), 0644)
require.NoError(t, err)
}

frontmatter := map[string]any{"imports": []string{"root.md"}}

var baseline []string
for i := range 5 {
result, err := parser.ProcessImportsFromFrontmatterWithManifest(frontmatter, tempDir, nil)
require.NoError(t, err)
if i == 0 {
baseline = append([]string(nil), result.ImportedFiles...)
continue
}
assert.Equal(t, baseline, result.ImportedFiles)
}
}
6 changes: 6 additions & 0 deletions pkg/workflow/compiler_activation_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"maps"
"slices"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -801,7 +802,12 @@ func (c *Compiler) buildMainJob(data *WorkflowData, activationJobCreated bool) (
// so the agent job gets them transitively through activation
// Custom jobs that depend on agent should run AFTER the agent job, not before it
if data.Jobs != nil {
jobNames := make([]string, 0, len(data.Jobs))
for jobName := range data.Jobs {
jobNames = append(jobNames, jobName)
}
sort.Strings(jobNames)
for _, jobName := range jobNames {
// Skip jobs.pre-activation (or pre_activation) as it's handled specially
if jobName == string(constants.PreActivationJobName) || jobName == "pre-activation" {
continue
Expand Down
3 changes: 3 additions & 0 deletions pkg/workflow/compiler_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/github/gh-aw/pkg/stringutil"
Expand Down Expand Up @@ -115,6 +116,7 @@ func (c *Compiler) getCustomJobsDependingOnPreActivation(customJobs map[string]a
}
return false
})
sort.Strings(deps)
compilerJobsLog.Printf("Found %d custom jobs depending on pre_activation: %v", len(deps), deps)
return deps
}
Expand All @@ -133,6 +135,7 @@ func (c *Compiler) getReferencedCustomJobs(content string, customJobs map[string
refs := sliceutil.FilterMapKeys(customJobs, func(jobName string, _ any) bool {
return strings.Contains(content, fmt.Sprintf("needs.%s.", jobName))
})
sort.Strings(refs)
if len(refs) > 0 {
compilerJobsLog.Printf("Found %d custom job references: %v", len(refs), refs)
}
Expand Down
Loading