Skip to content

[file-diet] Refactor pkg/workflow/safe_outputs_config.go (1,254 lines) into focused modules #44295

Description

@github-actions

Overview

The file pkg/workflow/safe_outputs_config.go has grown to 1,254 lines, making it difficult to maintain and test. This task involves refactoring it into smaller, focused files with improved test coverage.

Current State

  • File: pkg/workflow/safe_outputs_config.go
  • Size: 1,254 lines
  • Direct test file: None (coverage comes from compiler_safe_outputs_config_test.go at 3,523 lines — excellent indirect coverage)
  • Test-to-source ratio: ~2.8× (indirect)
  • Functions: 8 functions; extractSafeOutputsConfig spans ~760 lines alone
Full File Analysis

Section Breakdown

Lines Content
1–168 Type Definitions — 6 struct types: BaseSafeOutputConfig, SafeOutputsConfig, SafeOutputMessagesConfig, MentionsConfig, SecretMaskingConfig, SafeOutputStepConfig
169–210 Architecture comment block for schema generation
211–970 extractSafeOutputsConfig() — monolithic method (~760 lines) that parses all 40+ output-handler types plus 20+ global config fields inline
972–1070 parseBaseSafeOutputConfig() and parseSamplesValue() — common base field parsing
1090–1254 Runtime config buildersaddHandlerManagerConfigEnvVar, buildMentionsHandlerConfig, dispatch copy helpers, getEngineAgentFileInfo

Complexity Hotspots

  • extractSafeOutputsConfig (line 211): ~760 lines, handles 40+ handler types and 20+ global fields (allowed-domains, staged, env, github-token, max-patch-size, max-patch-files, threat-detection, runs-on, timeout-minutes, messages, mentions, footer, group-reports, report-failure-as-issue, failure-issue-repo, max-bot-mentions, steps, id-token, concurrency-group, needs, environment, jobs, scripts, actions, github-app).
  • Integer parsing for max-patch-size, max-patch-files, and timeout-minutes duplicates the same type-switch pattern three times in the same function.
  • addHandlerManagerConfigEnvVar (line 1090): ~90 lines, builds runtime env config — logically unrelated to YAML extraction.

Duplicate Patterns

The file contains three near-identical type-switch blocks (int/int64/uint64/float64) for parsing integer YAML fields — all within extractSafeOutputsConfig.

Refactoring Strategy

Proposed File Splits

  1. safe_outputs_config_types.go

    • Types: BaseSafeOutputConfig, SafeOutputsConfig, SafeOutputMessagesConfig, MentionsConfig, SecretMaskingConfig, SafeOutputStepConfig
    • Logger variable: safeOutputsConfigLog
    • Architecture doc comment
    • Estimated LOC: ~170
  2. safe_outputs_config_extraction.go

    • Functions: (*Compiler).extractSafeOutputsConfig, (*Compiler).parseBaseSafeOutputConfig, parseSamplesValue
    • Responsibility: YAML frontmatter → structured Go config
    • Estimated LOC: ~580 (after extracting global-field parsing into helpers)
  3. safe_outputs_config_global.go

    • New helper: extractGlobalConfigFields() (or individual extractPatchConfig, extractTimeoutConfig, extractEnvConfig helpers)
    • Deduplicates the three repeated integer type-switch blocks in extractSafeOutputsConfig
    • Responsibility: Parsing global safe-outputs config knobs (non-handler fields)
    • Estimated LOC: ~200
  4. safe_outputs_config_runtime.go

    • Functions: (*Compiler).addHandlerManagerConfigEnvVar, buildMentionsHandlerConfig, safeOutputsWithDispatchTargetRepo, safeOutputsWithDispatchTargetRef, (*Compiler).getEngineAgentFileInfo
    • Responsibility: Runtime handler-manager config building and JSON serialization
    • Estimated LOC: ~175

Shared Utilities

Extract the repeated integer type-switch (int/int64/uint64/float64 → bounded int) into:

  • safe_outputs_config_global.go: parseBoundedIntField(configMap, key, log) — returns (int, bool)

Interface Abstractions

No new interfaces required; the existing AgentFileProvider interface is already used by getEngineAgentFileInfo.

Test Coverage Plan

The existing test coverage is already high. After splitting:

  1. safe_outputs_config_types_test.go (optional)

    • Verify YAML tag round-trips for SafeOutputsConfig and BaseSafeOutputConfig
    • Estimated: ~50 lines
  2. safe_outputs_config_global_test.go

    • parseBoundedIntField: integer, int64, uint64, float64, NaN/Inf, truncation, out-of-range
    • extractGlobalConfigFields: staged, env, github-token, allowed-domains, footer, group-reports
    • Target coverage: >80%
  3. Existing tests stay validcompiler_safe_outputs_config_test.go (3,523 lines) provides comprehensive end-to-end coverage and should pass unchanged after a pure structural split.

Implementation Guidelines

  1. Preserve Behavior: Ensure all existing functionality works identically
  2. Maintain Exports: Keep public API unchanged (exported types and functions)
  3. Deduplicate First: Extract the repeated integer type-switch into parseBoundedIntField before splitting files
  4. Incremental Changes: Split one module at a time — types → global helpers → runtime
  5. Run Tests Frequently: Verify make test-unit passes after each split
  6. Update Imports: Ensure all import paths are correct (all files stay in package workflow)
  7. Document Changes: Add comments explaining module boundaries

Acceptance Criteria

  • safe_outputs_config.go is removed/split into focused files
  • Each new file is under 500 lines
  • make test-unit passes
  • Repeated integer type-switch is deduplicated
  • No breaking changes to public API
  • make lint passes
  • make build succeeds
Additional Context
  • Repository Guidelines: Follow patterns in .github/agents/developer.instructions.agent.md
  • Code Organization: Prefer many small files grouped by functionality (see pkg/workflow/ — hundreds of focused files)
  • Testing: Match existing test patterns in pkg/workflow/*_test.go
  • Companion files already split: safe_outputs_config_helpers.go (61 lines), safe_outputs_config_generation.go (302 lines), safe_outputs_parser.go (90 lines) — this file is the remaining oversized piece

Priority: Medium
Effort: Large (40+ handler types, 8 functions, requires careful deduplication)
Expected Impact: Improved maintainability, easier navigation, deduplicated integer parsing logic

Generated by 🧹 Daily File Diet · 148.6 AIC · ⌖ 17.4 AIC · ⊞ 6.8K ·

  • expires on Jul 10, 2026, 5:18 AM UTC-08:00

Metadata

Metadata

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions