Commit 8269bae
authored
refactor: extract duplicate code patterns into helpers (#1855)
Addresses duplicate code analysis identifying ~50 lines of structural
duplication across two patterns in `internal/server/unified.go`.
## Changes
### Pattern 1: Backend Tool Call Execution (~40 lines eliminated)
Extracted `executeBackendToolCall` helper consolidating:
- Connection management via `launcher.GetOrLaunchForSession`
- `tools/call` request dispatch
- Backend error checking
- Result unmarshaling
**Before:**
```go
// guardBackendCaller.CallTool and callBackendTool Phase 3 each had:
conn, err := launcher.GetOrLaunchForSession(us.launcher, serverID, sessionID)
if err != nil { ... }
response, err := conn.SendRequestWithServerID(ctx, "tools/call", ...)
if err != nil { ... }
if response.Error != nil { ... }
var result interface{}
json.Unmarshal(response.Result, &result)
```
**After:**
```go
result, err := executeBackendToolCall(ctx, us.launcher, serverID, sessionID, toolName, args)
```
### Pattern 2: Sys Tool Delegation (~10 lines eliminated)
Extracted `callSysServer` helper consolidating:
- Tool name marshaling
- `sysServer.HandleRequest` delegation
**Before:**
```go
// sysInitHandler and sysListHandler each had:
params, _ := json.Marshal(map[string]interface{}{
"name": toolName,
"arguments": map[string]interface{}{},
})
result, err := us.sysServer.HandleRequest("tools/call", json.RawMessage(params))
```
**After:**
```go
result, err := us.callSysServer("sys_init")
```
## Impact
- Eliminates copy-paste error risk when modifying backend call or sys
server logic
- Follows existing helper patterns (`newErrorCallToolResult`,
`parseToolArguments`)
- No functional changes—pure refactoring
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build4242760615/b336/launcher.test
/tmp/go-build4242760615/b336/launcher.test
-test.testlogfile=/tmp/go-build4242760615/b336/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -uns�� ache/go/1.25.7/x64/src/net
/tmp/go-build1249286257/b024/vet.cfg
ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet unset position.go
.12/x64/bin/git ache/go/1.25.7/x64/pkg/tool/linuHEAD -W -I
/tmp/go-build1249286257/b165/
ache/go/1.25.7/x64/pkg/tool/linux_amd64/compile . --gdwarf2 --64
ache/go/1.25.7/x64/pkg/tool/linux_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build2320097585/b332/launcher.test
/tmp/go-build2320097585/b332/launcher.test
-test.testlogfile=/tmp/go-build2320097585/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 64/bin/git ortcfg
/usr/sbin/bash submodules | heanode 9286257/b132/vet-v ndor/bin/git bash
/usr�� runtime-runc/moby ortcfg csi/net-interface-handler
aw-mcpg/internal/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/asm
aw-mcpg/internal-V=full` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build4242760615/b318/config.test
/tmp/go-build4242760615/b318/config.test
-test.testlogfile=/tmp/go-build4242760615/b318/testlog.txt
-test.paniconexit0 -test.timeout=10m0s --de�� c.test --debug-prefix-map
64/bin/git -I /opt/hostedtoolc-unsafeptr=false ut-1969789729.c
VF78F_aidvBPXKXaKR/S4HB-3OSyH3DKHEAD` (dns block)
> - Triggering command: `/tmp/go-build2320097585/b314/config.test
/tmp/go-build2320097585/b314/config.test
-test.testlogfile=/tmp/go-build2320097585/b314/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 17fb9960acf3cdef
-importcfg ine by/b2276789a3c14/usr/bin/chronyc -w -buildmode=exe ine
--ve�� 85e4fa5c5748c5dd -extld=gcc /opt/hostedtoolcache/go/1.25.7/xjson
aw-mcpg/internalbase64 9286257/b151/vet-d ns
/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linu--listen` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build4242760615/b336/launcher.test
/tmp/go-build4242760615/b336/launcher.test
-test.testlogfile=/tmp/go-build4242760615/b336/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -uns�� ache/go/1.25.7/x64/src/net
/tmp/go-build1249286257/b024/vet.cfg
ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet unset position.go
.12/x64/bin/git ache/go/1.25.7/x64/pkg/tool/linuHEAD -W -I
/tmp/go-build1249286257/b165/
ache/go/1.25.7/x64/pkg/tool/linux_amd64/compile . --gdwarf2 --64
ache/go/1.25.7/x64/pkg/tool/linux_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build2320097585/b332/launcher.test
/tmp/go-build2320097585/b332/launcher.test
-test.testlogfile=/tmp/go-build2320097585/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 64/bin/git ortcfg
/usr/sbin/bash submodules | heanode 9286257/b132/vet-v ndor/bin/git bash
/usr�� runtime-runc/moby ortcfg csi/net-interface-handler
aw-mcpg/internal/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/asm
aw-mcpg/internal-V=full` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build4242760615/b336/launcher.test
/tmp/go-build4242760615/b336/launcher.test
-test.testlogfile=/tmp/go-build4242760615/b336/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -uns�� ache/go/1.25.7/x64/src/net
/tmp/go-build1249286257/b024/vet.cfg
ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet unset position.go
.12/x64/bin/git ache/go/1.25.7/x64/pkg/tool/linuHEAD -W -I
/tmp/go-build1249286257/b165/
ache/go/1.25.7/x64/pkg/tool/linux_amd64/compile . --gdwarf2 --64
ache/go/1.25.7/x64/pkg/tool/linux_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build2320097585/b332/launcher.test
/tmp/go-build2320097585/b332/launcher.test
-test.testlogfile=/tmp/go-build2320097585/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 64/bin/git ortcfg
/usr/sbin/bash submodules | heanode 9286257/b132/vet-v ndor/bin/git bash
/usr�� runtime-runc/moby ortcfg csi/net-interface-handler
aw-mcpg/internal/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/asm
aw-mcpg/internal-V=full` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build4242760615/b345/mcp.test
/tmp/go-build4242760615/b345/mcp.test
-test.testlogfile=/tmp/go-build4242760615/b345/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -I ache/go/1.25.7/x64/src/net -I
if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);
if (a[i] < b[i]) exit(3);
else if (a[i] > b[i]) exit(0) --gdwarf-5 --64 -o
ache/go/1.25.7/x64/pkg/tool/linuHEAD -o
/tmp/go-build1249286257/b210/_pkg_.a -trimpath
ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet -p
github.com/githu/tmp/go-build3632611954/b228/vet.cfg -lang=go1.25
ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build2320097585/b341/mcp.test
/tmp/go-build2320097585/b341/mcp.test
-test.testlogfile=/tmp/go-build2320097585/b341/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
/home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/strutil/truncat6662819a7d6931901c1593745131476bbash
y CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt",
"REQUESTS_CA_BUNDLE=/etc/ssl/c--version
by/bd0209c110101/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/link
aw-mcpg/internal-o
ache/go/1.25.7/x/tmp/go-build2320097585/b314/config.test docker insp��
551622d230bb31fc-s by/bd0209c110101-w rtificates.crt",-buildmode=exe
by/a501e7528e5b9bash e330148d0ce51d5e/usr/bin/runc` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>1 file changed
Lines changed: 52 additions & 51 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
469 | 469 | | |
470 | 470 | | |
471 | 471 | | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
472 | 487 | | |
473 | 488 | | |
474 | 489 | | |
| |||
511 | 526 | | |
512 | 527 | | |
513 | 528 | | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
| 529 | + | |
519 | 530 | | |
520 | 531 | | |
521 | 532 | | |
| |||
554 | 565 | | |
555 | 566 | | |
556 | 567 | | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
| 568 | + | |
562 | 569 | | |
563 | 570 | | |
564 | 571 | | |
| |||
783 | 790 | | |
784 | 791 | | |
785 | 792 | | |
786 | | - | |
787 | | - | |
788 | | - | |
789 | | - | |
790 | | - | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
796 | | - | |
797 | | - | |
798 | | - | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
803 | | - | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
804 | 804 | | |
805 | 805 | | |
806 | 806 | | |
807 | 807 | | |
808 | | - | |
| 808 | + | |
809 | 809 | | |
810 | 810 | | |
811 | | - | |
| 811 | + | |
812 | 812 | | |
813 | 813 | | |
814 | 814 | | |
| |||
827 | 827 | | |
828 | 828 | | |
829 | 829 | | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
830 | 851 | | |
831 | 852 | | |
832 | 853 | | |
| |||
1004 | 1025 | | |
1005 | 1026 | | |
1006 | 1027 | | |
1007 | | - | |
1008 | | - | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
1014 | | - | |
1015 | | - | |
1016 | | - | |
| 1028 | + | |
1017 | 1029 | | |
1018 | 1030 | | |
1019 | 1031 | | |
1020 | 1032 | | |
1021 | | - | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
1031 | | - | |
1032 | 1033 | | |
1033 | 1034 | | |
1034 | 1035 | | |
| |||
0 commit comments