Skip to content

Sort jobs alphabetically by name in generated lockfiles#1878

Merged
pelikhan merged 5 commits intomainfrom
copilot/sort-jobs-by-name
Oct 17, 2025
Merged

Sort jobs alphabetically by name in generated lockfiles#1878
pelikhan merged 5 commits intomainfrom
copilot/sort-jobs-by-name

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 17, 2025

Summary

This PR ensures that jobs in generated .lock.yml workflow files are sorted alphabetically by name, providing consistent and predictable output regardless of the order jobs are created internally during compilation.

Problem

Previously, jobs were emitted in the order they were added to the JobManager (insertion order). This meant that the same workflow could produce different lockfile outputs depending on internal compiler logic, making it harder to review changes and track differences.

For example, jobs might appear as:

jobs:
  pre_activation:
  activation:
  agent:
  create_issue:
  add_comment:

Solution

Modified the JobManager in pkg/workflow/jobs.go to maintain the jobOrder slice in sorted alphabetical order at all times. The AddJob() method now sorts the slice after each job addition, ensuring jobs consistently appear in alphabetical order:

jobs:
  activation:
  add_comment:
  agent:
  create_issue:
  pre_activation:

Changes

Core Implementation

  • pkg/workflow/jobs.go:
    • Updated AddJob() to sort jobOrder after each job addition using sort.Strings()
    • Simplified RenderToYAML() to iterate through the pre-sorted jobOrder slice
    • Updated jobOrder field comment to reflect it's kept in sorted alphabetical order

Testing

  • pkg/workflow/jobs_test.go: Added test case verifying jobs are sorted alphabetically even when added in non-alphabetical order (zebra, alpha, charlie, beta → alpha, beta, charlie, zebra)
  • pkg/workflow/stop_time_check_job_test.go: Updated test assertions to work with alphabetical ordering where agent now appears before pre_activation in the output

Impact

  • ✅ All 34 workflows in the repository compile successfully with alphabetically sorted jobs
  • ✅ All unit and integration tests pass
  • ✅ Output is now deterministic and easier to review in diffs
  • ✅ No functional changes to workflow execution (job dependencies remain unchanged)
  • ✅ Maintains invariant that jobOrder is always sorted, eliminating need for on-demand sorting

Example

From the poem-bot workflow, jobs are now consistently ordered:

  1. activation
  2. add_comment
  3. add_labels
  4. agent
  5. create_issue
  6. create_pr_review_comment
  7. create_pull_request
  8. detection
  9. missing_tool
  10. pre_activation
  11. push_to_pull_request_branch
  12. update_issue
  13. update_reaction
  14. upload_assets
Original prompt

Make sure the compiler sorts the jobs by name before emitting them in the lockfile. make sure jobOrder is sorted.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 17, 2025 18:41
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Sort jobs by name in lockfile Sort jobs alphabetically by name in generated lockfiles Oct 17, 2025
Copilot AI requested a review from pelikhan October 17, 2025 18:49
@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot sort the jobOrder after each update.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 17, 2025

@copilot sort the jobOrder after each update.

Done. Updated AddJob() to sort jobOrder after each addition and simplified RenderToYAML() to use the pre-sorted slice. Commit: 8886029 → (new commit)

@pelikhan pelikhan marked this pull request as ready for review October 17, 2025 19:08
@pelikhan pelikhan merged commit bf9e175 into main Oct 17, 2025
14 checks passed
@pelikhan pelikhan deleted the copilot/sort-jobs-by-name branch October 17, 2025 19:13
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