Refactor safe-outputs handler config using builder pattern and registry#11779
Refactor safe-outputs handler config using builder pattern and registry#11779
Conversation
…dler registry - Create handlerConfigBuilder with fluent API for building handler configs - Implement handler registry pattern to eliminate code duplication - Replace 400+ lines of repetitive if-blocks with registry iteration - Reduce file from 644 to 536 lines (16.8% reduction) - All tests passing, linter clean Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Validated with make recompile: 140 workflows compiled successfully - All handler configuration tests passing - Linter clean, formatting validated - Build successful - File reduction: 644 → 536 lines (16.8% reduction, 108 lines saved) Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
| env: | ||
| GH_AW_AGENT_OUTPUT: ${{ env.GH_AW_AGENT_OUTPUT }} | ||
| GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG: "{\"add_comment\":{\"max\":10},\"create_discussion\":{\"expires\":168,\"max\":2},\"create_issue\":{\"group\":true,\"labels\":[\"cookie\"],\"max\":5},\"missing_data\":{},\"missing_tool\":{}}" | ||
| GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG: "{\"add_comment\":{\"max\":10},\"create_discussion\":{\"expires\":168,\"max\":2},\"create_issue\":{\"group\":true,\"labels\":[\"cookie\"],\"max\":5}}" |
There was a problem hiding this comment.
@copilot review why missing_data and missing_tool are missing and fix it
There was a problem hiding this comment.
Fixed in commit 5343de8. The issue was that missing_tool and missing_data are auto-enabled by default when safe-outputs is present, so they need to be included in the handler config even when they have empty configurations. Updated the registry iteration logic to always include these handlers if they exist (even with empty config {}).
Include missing_tool and missing_data handlers even with empty configs
because they are auto-enabled by default when safe-outputs is present.
The original code added these handlers with empty configs ({}), which
was important for backward compatibility and default behavior.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Replace hardcoded handler name checks with autoEnabledHandlers map for better maintainability and extensibility. Simplifies logic and makes it easier to add new auto-enabled handlers in the future. Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
@copilot recompile and inspect changes in lock files |
Recompiled all 130 workflow lock files to include missing_data and
missing_tool handlers in the GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG.
These handlers are auto-enabled by default when safe-outputs is present,
allowing agents to report missing tools and data. Each handler now appears
with empty config ({}) in all workflows.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Recompiled in commit 79710e8. All 130 workflow lock files updated. Key Change: Added Pattern: // Before
{"create_issue": {...}}
// After
{"create_issue": {...}, "missing_data": {}, "missing_tool": {}}Examples:
These handlers are auto-enabled by default to allow agents to report missing tools/data, even with empty configs. |
Refactoring Complete: compiler_safe_outputs_config.go
Summary
Successfully refactored
pkg/workflow/compiler_safe_outputs_config.goto eliminate code duplication using builder pattern and handler registry, with proper handling of auto-enabled handlers.Implementation Details
✅ Phase 1: Handler Configuration Builder
handlerConfigBuildertype with fluent APIAddIfPositive,AddIfNotEmpty,AddIfTrue,AddStringSlice,AddBoolPtr,AddBoolPtrOrDefault,AddStringPtr,AddDefault✅ Phase 2: Handler Registry Pattern
handlerRegistrymapping handler names to builder functionsprojectHandlerRegistryfor project-specific handlersautoEnabledHandlersconstant map for handlers that must always be included✅ Phase 3: Main Function Refactoring
addHandlerManagerConfigEnvVar(previously 532 lines of repetitive if-blocks)✅ Phase 4: Project Handler Refactoring
addProjectHandlerManagerConfigEnvVar✅ Phase 5: Complete Validation
make fmt- Code formatted successfullymake lint- All validations passed (0 issues)make build- Binary compiles successfully✅ Phase 6: Bug Fixes & Improvements
autoEnabledHandlersconstant map to avoid hardcoded strings✅ Phase 7: Lock File Recompilation
Results
Line Count Reduction:
Lock File Updates:
{}for auto-enabled handlersSuccess Criteria Achieved:
make testpasses all testsmake lintreports no issuesmake recompilesuccessfully regenerated all workflowsMaintainability Improvements
Before (Repetitive Pattern):
After (Registry Pattern):
Benefits:
Testing
All handler configuration tests passing:
TestAddHandlerManagerConfigEnvVar- All scenarios passingTestHandlerConfigMaxValues- Validates max value configurationTestHandlerConfigAllowedLabels- Validates label configurationTestHandlerConfigBooleanFields- Validates boolean field handlingTestHandlerConfigUpdateFields- Validates update field configurationTestHandlerConfigTargetRepo- Validates target-repo configurationTestHandlerConfigPatchSize- Validates patch size configurationTestEmptySafeOutputsConfig- Validates empty config handlingVerified Behavior
{}) properly included for auto-enabled handlersOriginal prompt
This section details on the original issue you should resolve
<issue_title>[Code Quality] Refactor compiler_safe_outputs_config.go to Eliminate 400 Lines of Repetition</issue_title>
<issue_description>## Description
The file
pkg/workflow/compiler_safe_outputs_config.go(644 lines) contains highly repetitive code for configuring safe output handlers. Each of the 20+ handler types follows an identical pattern, resulting in approximately 400 lines of duplicated code (62% of the file).This violates the DRY (Don't Repeat Yourself) principle and makes the code harder to maintain, test, and extend.
Current Pattern (Repeated 20+ Times)
Problems
addHandlerManagerConfigEnvVaris 532 lines (way over 50-line guideline)Suggested Refactoring
Option 1: Handler Registry Pattern
Option 2: Reflection-Based Builder (More Dynamic)
Files Affected
pkg/workflow/compiler_safe_outputs_config.go(primary refactoring target)pkg/workflow/compiler_safe_outputs_config_test.go(may need test updates)pkg/workflow/handler_builder.go(for builder pattern)Success Criteria
make testpasses all testsmake lintreports no issuesmake recompilesuccessfully regenerates all workflowsImpact Analysis
Benefits:
Risks:
Estimated Effort
1 day (8 hours)
Source
Extracted from Daily Compiler Code Quality Report githubnext/gh-aw#11729 - Analysis of compiler_safe_outputs_config.go (2026-01-25)
Priority
High - Significant code duplication (62% of file) impacts maintain...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.