You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: eliminate three duplicate code patterns in launcher, config, and server packages (#1585)
- [x] Pattern 1 (#1540): Extract `launchStdioConnection` helper in
`internal/launcher/launcher.go`
- [x] Pattern 2 (#1541): Add `GetAPIKey()` to `*config.Config` and
replace 3 occurrences in `root.go`
- [x] Pattern 3 (#1542): Add `writeJSONResponse` to `http_helpers.go`
and replace occurrences in `handlers.go`, `health.go`, `routed.go`
- [x] Add unit tests for `GetAPIKey()` in config test file
- [x] Fix: `rejectIfShutdown` now passes
`json.RawMessage(shutdownErrorJSON)` to keep the constant referenced in
production code
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>[duplicate-code] Duplicate Code Analysis
Report</issue_title>
> <issue_description>## Summary
>
> Analysis of the Go codebase identified **3 significant duplication
patterns** spanning the `launcher`, `server`, and `cmd` packages. Two
patterns involve repeated 3-line idioms appearing 3+ times; one involves
~20 lines of near-identical goroutine+timeout logic duplicated across
two closely related functions.
>
> ## Detected Patterns
>
> This analysis found 3 significant duplication patterns:
>
> 1. **Goroutine+timeout connection launch logic in launcher.go** –
Severity: **High** – See sub-issue #1540
> 2. **API key extraction from gateway config in root.go** – Severity:
**Medium** – See sub-issue #1541
> 3. **JSON response writing pattern in server package** – Severity:
**Low** – See sub-issue #1542
>
> ## Overall Impact
>
> - **Total Duplicated Lines**: ~35 lines (excluding minor boilerplate)
> - **Affected Files**: 3 Go files (`internal/launcher/launcher.go`,
`internal/cmd/root.go`, `internal/server/handlers.go` / `health.go` /
`routed.go`)
> - **Maintainability Risk**: Medium — the launcher pattern is the most
risky; a bug fix in one `select`/timeout branch would need to be
replicated manually in the other
> - **Refactoring Priority**: Medium — the launcher pattern should be
addressed first; the others are straightforward one-liner extractions
>
> ## Next Steps
>
> 1. Review individual pattern sub-issues for detailed analysis
> 2. Prioritize `#1540` (launcher goroutine pattern) as it poses the
greatest bug-divergence risk
> 3. `#1541` and `#1542` are small extractions that can be done
opportunistically
>
> ## Analysis Metadata
>
> - **Analyzed Files**: 68 non-test Go files under `internal/` and
`main.go`
> - **Detection Method**: Static code analysis (grep, structural
comparison)
> - **Commit**: 18c5699
> - **Analysis Date**: 2026-03-02</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
> **Custom agent used: agentic-workflows**
> GitHub Agentic Workflows (gh-aw) - Create, debug, and upgrade
AI-powered workflows with intelligent prompt routing
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes#1539
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 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](https://gh.io/copilot-coding-agent-tips) in the docs.
0 commit comments