[duplicate-code] 🔍 Duplicate prompt-step builders across workflow compiler

# 🔍 Duplicate Code Detected

*Analysis of commit 447b4957de8960e00fd2ac60b1ad6e16121bc90a*

**Assignee**: `@copilot`

## Summary

Multiple prompt-step helper methods introduced in commit 447b4957 share the same YAML emission pattern (writing a `run` block that appends prompt text to `$GITHUB_AW_PROMPT`). The duplication spans six files and adds maintenance risk when adjusting prompt plumbing. A second duplication exists inside `pkg/workflow/compiler.go`, where cache-memory and safe-output helpers repeat the same string-building logic with only the inner payload differing.

## Duplication Details

### Pattern 1: Prompt step YAML generation helpers
- **Severity**: Medium
- **Occurrences**: 6
- **Locations**:
  - `pkg/workflow/temp_folder.go` (lines 7-13)
  - `pkg/workflow/xpia.go` (lines 7-18)
  - `pkg/workflow/playwright_prompt.go` (lines 8-24)
  - `pkg/workflow/edit_tool_prompt.go` (lines 8-24)
  - `pkg/workflow/github_context.go` (lines 7-18)
  - `pkg/workflow/pr.go` (lines 5-35)
- **Code Sample**:
  ```go
  yaml.WriteString("      - name: " + stepName + "\n")
  yaml.WriteString("        env:\n")
  yaml.WriteString("          GITHUB_AW_PROMPT: /tmp/gh-aw/aw-prompts/prompt.txt\n")
  yaml.WriteString("        run: |\n")
  WritePromptTextToYAML(yaml, promptText, "          ")
  ```

### Pattern 2: Prompt section builders in compiler
- **Severity**: Low
- **Occurrences**: 2
- **Locations**:
  - `pkg/workflow/compiler.go` (lines 2796-2808)
  - `pkg/workflow/compiler.go` (lines 2811-2823)
- **Code Sample**:
  ```go
  yaml.WriteString("      - name: " + stepName + "\n")
  yaml.WriteString("        env:\n")
  yaml.WriteString("          GITHUB_AW_PROMPT: /tmp/gh-aw/aw-prompts/prompt.txt\n")
  yaml.WriteString("        run: |\n")
  yaml.WriteString("          cat >> $GITHUB_AW_PROMPT << 'EOF'\n")
  generator(yaml, config)
  yaml.WriteString("          EOF\n")
  ```

## Impact Analysis

- **Maintainability**: Updating prompt plumbing requires touching many sites; new prompt types invite more copy/paste, increasing the chance of small inconsistencies.
- **Bug Risk**: Behavioural tweaks (e.g., changing the destination env variable) risk diverging across helpers if one call site is missed.
- **Code Bloat**: Adds repeated boilerplate that obscures the actual prompt-specific logic and inflates future diffs.

## Refactoring Recommendations

1. **Introduce a shared helper**
   - Extract a `appendPromptStep(yaml *strings.Builder, name string, render func(*strings.Builder))` (or similar) to encapsulate the shared YAML scaffolding and reduce each helper to guard logic + prompt body. Estimated effort: 2-3 hours.
   - Benefits: Single point to adjust prompt destination/env wiring and clearer intent in each helper.
2. **Parameterise section builders**
   - Reuse the helper for the `cat >>` pattern, e.g. pass a renderer callback. Estimated effort: 1 hour.
   - Benefits: Keeps compiler prompt generation consistent and shortens future reviews.

## Implementation Checklist

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

## Analysis Metadata

- **Analyzed Files**: 6
- **Detection Method**: Serena semantic code analysis
- **Commit**: 447b4957de8960e00fd2ac60b1ad6e16121bc90a
- **Analysis Date**: 2025-10-16T14:52:00Z




> AI generated by [Duplicate Code Detector](https://github.com/githubnext/gh-aw/actions/runs/18565294566)

Labels: code-quality, automated-analysis
State: closed