Skip to content

[duplicate-code] Duplicate Code: Copilot agent template writers share identical logic #4254

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: Copilot Agent Template Writers

Analysis of commit 0f6a68f

Assignee: @copilot

Summary

ensureAgentFromTemplate and ensureCopilotInstructions in pkg/cli/copilot-agents.go both implement the same file-synchronization workflow (git root lookup, directory creation, template compare, write, verbose logging) with only path/template constants differing. This duplication spans ~40 lines and appears twice, making the code harder to maintain when behavior changes.

Duplication Details

Pattern: Copilot agent/copilot instructions file sync logic

  • Severity: Medium
  • Occurrences: 2
  • Locations:
    • pkg/cli/copilot-agents.go:11
    • pkg/cli/copilot-agents.go:61
  • Code Sample:
    func ensureAgentFromTemplate(agentFileName, templateContent string, verbose bool, skipInstructions bool) error {
        if skipInstructions {
            return nil
        }
        gitRoot, err := findGitRoot()
        if err != nil {
            return err
        }
        targetDir := filepath.Join(gitRoot, subdir)
        targetPath := filepath.Join(targetDir, targetName)
        if err := os.MkdirAll(targetDir, 0755); err != nil {
            return fmt.Errorf("failed to create %s directory: %w", subdir, err)
        }
        existingContent := readFileIfExists(targetPath)
        if strings.TrimSpace(existingContent) == strings.TrimSpace(templateContent) {
            logVerbose("%s is up-to-date", targetPath)
            return nil
        }
        return os.WriteFile(targetPath, []byte(templateContent), 0644)
    }

Impact Analysis

  • Maintainability: Any tweaks to the synchronization process (e.g., logging, permissions, skip rules) must be applied twice, increasing risk of drift.
  • Bug Risk: Divergence between the two copies could create inconsistent behavior for agents vs. instructions, leading to subtle setup bugs.
  • Code Bloat: 40+ duplicated lines inflate the file and make future refactors more error-prone.

Refactoring Recommendations

  1. Extract shared helper

    • Factor a single ensureFileMatchesTemplate(rootSubdir, fileName, template string, verbose, skip bool) helper that both functions can call.
    • Estimated effort: 1-2 hours.
    • Benefits: Single source of truth for template sync behavior, easier auditing and future changes.
  2. Centralize verbose messaging

    • Use formatted parameters so the helper can emit contextual create/update logs, avoiding repeated fmt.Printf blocks.
    • Estimated effort: <1 hour (follow-on once helper exists).

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 3
  • Detection Method: Serena semantic code analysis, manual comparison
  • Commit: 0f6a68f
  • Analysis Date: 2025-11-18T06:38:13Z

AI generated by Duplicate Code Detector

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions