Commit 6f83ea4
authored
refactor: reduce duplicate code in loggers, validation, and session handling (#2790)
Three structural duplication patterns in `internal/logger`,
`internal/config`, and `internal/server` accumulated ~500 lines of
repetitive boilerplate with divergence risk.
## Logger initialization closures → named functions
Each `Init*Logger` function used anonymous inline closures for setup and
error handling. Extracted to named package-level functions per logger
type:
```go
// Before: anonymous closures inline in InitFileLogger
logger, err := initLogger(logDir, fileName, os.O_APPEND,
func(file *os.File, logDir, fileName string) (*FileLogger, error) { ... },
func(err error, logDir, fileName string) (*FileLogger, error) { ... },
)
// After: named functions
logger, err := initLogger(logDir, fileName, os.O_APPEND, setupFileLogger, handleFileLoggerError)
```
Applied to `FileLogger`, `MarkdownLogger`, `JSONLLogger`, and
`ToolsLogger`.
## Validation log helpers
30+ near-identical `logValidation.Printf(...)` calls in `validation.go`
standardized via three helpers:
```go
func logValidateServerStart(name, serverType string)
func logValidateServerPassed(name string)
func logValidateServerFailed(name, reason string)
```
## `withLock` helper on logger types
Replaced repeated `mu.Lock() / defer mu.Unlock()` preambles with a
per-type `withLock(fn func() error) error` method on `FileLogger`,
`JSONLLogger`, `MarkdownLogger`, and `ToolsLogger`. Updated `Close()`
and write methods accordingly.
## `session.go` double-checked locking → `syncutil.GetOrCreate`
`requireSession` previously hand-rolled read→check→write→double-check
locking. Replaced with the existing `syncutil.GetOrCreate` helper (same
pattern already used in `server_file_logger.go`):
```go
isNew := false
if _, err := syncutil.GetOrCreate(&us.sessionMu, us.sessions, sessionID, func() (*Session, error) {
s := NewSession(sessionID, "")
isNew = true
return s, nil
}); err != nil {
return err
}
```
Updated `common.go` documentation to reflect the new `withLock` and
named-function patterns.
> [!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-build758752077/b330/launcher.test
/tmp/go-build758752077/b330/launcher.test
-test.testlogfile=/tmp/go-build758752077/b330/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go --local 86_64/as
user.email` (dns block)
> - Triggering command: `/tmp/go-build972084089/b334/launcher.test
/tmp/go-build972084089/b334/launcher.test
-test.testlogfile=/tmp/go-build972084089/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2374296311/b001/vet.cfg ache/go/1.25.8/x64/src/net
64/src/encoding/gob/dec_helpers.go cfg -I . -imultiarch
ache/go/1.25.8/x-buildtags -uns�� 752077/b258/_pkg-errorsas
/tmp/go-build758-ifaceassert ache/go/1.25.8/x-nilfunc _amd64.s
telabs/wazero/in-atomic -D_FORTIFY_SOURC-bool
ache/go/1.25.8/x-buildtags` (dns block)
> - Triggering command: `/tmp/go-build2612040108/b334/launcher.test
/tmp/go-build2612040108/b334/launcher.test
-test.testlogfile=/tmp/go-build2612040108/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s /usr�� ry=1
ga/m4mMaA4EfUuX_-tests bash
/opt/hostedtoolc/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet
cfg 64/pkg/tool/linu/tmp/go-build532890590/b158/vet.cfg bash --no��
--noprofile 64/pkg/tool/linux_amd64/vet .13/x64/bash se cfg x_amd64/vet
ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet` (dns block)
> - `https://api.github.com/graphql`
> - Triggering command: `/usr/bin/gh gh issue close 2755 --repo
github/gh-aw-mcpg --comment Resolved in PR #2790: refactored logger
initialization closures to named functions, added validation log
helpers, added withLock helpers to logger types, and refactored
requireSession to use syncutil.GetOrCreate.
by/734792accd854/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/compile
065 -lang=go1.25 065/init.pid 644 9221a53b1917004b-p
runtime-runc/mobgithub.com/github/gh-aw-mcpg/internal/server
/systemd-sysctl io.containerd.rugit -c=4
-certificates.crcopilot/duplicate-code-analysis-report 31d/log.json`
(http block)
> - Triggering command: `/usr/bin/gh gh issue close 2756 --repo
github/gh-aw-mcpg --comment Resolved in PR #2790: refactored logger
initialization closures to named functions, added validation log
helpers, added withLock helpers to logger types, and refactored
requireSession to use syncutil.GetOrCreate. -errorsas 065 -nilfunc`
(http block)
> - Triggering command: `/usr/bin/gh gh issue close 2757 --repo
github/gh-aw-mcpg --comment Resolved in PR #2790: refactored logger
initialization closures to named functions, added validation log
helpers, added withLock helpers to logger types, and refactored
requireSession to use syncutil.GetOrCreate.
io.containerd.ru/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/link
a8d469f49c8ee67b-o
084089/b330/vet./tmp/go-build1108576084/b001/server.test
/usr/bin/runc.or-importcfg init�� 64/pkg/tool/linu-s y ctl
ntime.v2.task/mogit -buildtags
64/pkg/tool/linucopilot/duplicate-code-analysis-report ctl` (http block)
> - `https://api.github.com/repos/github/gh-aw-mcpg/issues/2754`
> - Triggering command: `/usr/bin/gh gh api -X PATCH
repos//issues/2754 -f state=closed
"CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt",
"REQUESTS_CA_Bcopilot/duplicate-code-analysis-report
ntime.v2.task/modocker main -lang=go1.25 table.d/chrony-onoffline /usr��
d -n 10 /var/run/docker/runtime-runc/moby iginal /run/containerd/git
--log-format 908/log.json iginal` (http block)
> - `https://api.github.com/repos/github/gh-aw-mcpg/issues/2755`
> - Triggering command: `/usr/bin/gh gh api -X PATCH
repos//issues/2755 -f state=closed /bin/java
io.containerd.ru/usr/libexec/docker/cli-plugins/docker-buildx
-ifaceassert -nilfunc /bin/java /usr�� d -n 10
io.containerd.runtime.v2.task/moby/ec75429440bf8--log-format
erd-shim-runc-v2 io.containerd.rugit 849bc50063d4bbbaadd json
erd-shim-runc-v2-v` (http block)
> - Triggering command: `/usr/bin/curl curl -s -X PATCH REDACTED -H
Authorization: token ****** -H Content-Type: application/json -d
{"state":"closed"}
aw-mcpg/test/integration/start_gateway_with_pipe.sh --ro��
115b774795f51db055fc7f996e03d7a54f3702b53ed9bbf2 runtime-runc/moby
re-commit-msg io.containerd.rubash functions for l--norc
--systemd-cgroup--noprofile re-commit-msg` (http block)
> - Triggering command: `/usr/bin/curl curl -v -X PATCH REDACTED -H
Authorization: token ****** -H Content-Type: application/json -d
{"state":"closed"} csi/net-interfac-v 954f��
l/config/validation.go
io.containerd.runtime.v2.task/moby/901063bdb288e--log-format bin/bash
io.containerd.rubash a361f2191e0cbe7c--norc json docker` (http block)
> - `https://api.github.com/repos/github/gh-aw-mcpg/issues/2756`
> - Triggering command: `/usr/bin/gh gh api -X PATCH
repos//issues/2756 -f state=closed /bin/java
da27db1da389d8d6/usr/libexec/docker/cli-plugins/docker-buildx
-ifaceassert df1eeab6e3b61bce271/log.json /bin/java /usr��
/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib y bash
ntime.v2.task/mogit --log-format df1eeab6e3b61bce. bash` (http block)
> - `https://api.github.com/repos/github/gh-aw-mcpg/issues/2757`
> - Triggering command: `/usr/bin/gh gh api -X PATCH
repos//issues/2757 -f state=closed
d-dispatcher/off.d/chrony-onoffline ntime.v2.task/modocker -buildtags
d0d99cb50954aa1dd34960fd0baf0e9b. d-dispatcher/off.d/chrony-onoffline
--ro�� d -n 10 y deql ntime.v2.task/mogit df1eeab6e3b61bceadd
c9934293d0945041. deql` (http block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build758752077/b315/config.test
/tmp/go-build758752077/b315/config.test
-test.testlogfile=/tmp/go-build758752077/b315/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go1.25.8 -c=4
-nolocalimports -importcfg /tmp/go-build758752077/b192/importcfg -pack
/opt/hostedtoolcache/go/1.25.8/x64/src/net/http/httptest/httptest.go
rtcf�� eee754.go W2BDEJWaf
ache/go/1.25.8/x-ffile-prefix-map=/opt/hostedtoolcache/go/1.25.8/x64=/_/GOROOT
pull.rebase` (dns block)
> - Triggering command: `/tmp/go-build837462839/b315/config.test
/tmp/go-build837462839/b315/config.test
-test.testlogfile=/tmp/go-build837462839/b315/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
ache/go/1.25.8/x64/src/runtime/c-p Jxl7/weSoYHfkM6pMRgR2Jxl7
x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build972084089/b319/config.test
/tmp/go-build972084089/b319/config.test
-test.testlogfile=/tmp/go-build972084089/b319/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build1849658217/b202/vet.cfg 9lJ78QKSc
/tmp/go-build758752077/b019/vet.cfg
ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build758752077/b330/launcher.test
/tmp/go-build758752077/b330/launcher.test
-test.testlogfile=/tmp/go-build758752077/b330/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go --local 86_64/as
user.email` (dns block)
> - Triggering command: `/tmp/go-build972084089/b334/launcher.test
/tmp/go-build972084089/b334/launcher.test
-test.testlogfile=/tmp/go-build972084089/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2374296311/b001/vet.cfg ache/go/1.25.8/x64/src/net
64/src/encoding/gob/dec_helpers.go cfg -I . -imultiarch
ache/go/1.25.8/x-buildtags -uns�� 752077/b258/_pkg-errorsas
/tmp/go-build758-ifaceassert ache/go/1.25.8/x-nilfunc _amd64.s
telabs/wazero/in-atomic -D_FORTIFY_SOURC-bool
ache/go/1.25.8/x-buildtags` (dns block)
> - Triggering command: `/tmp/go-build2612040108/b334/launcher.test
/tmp/go-build2612040108/b334/launcher.test
-test.testlogfile=/tmp/go-build2612040108/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s /usr�� ry=1
ga/m4mMaA4EfUuX_-tests bash
/opt/hostedtoolc/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet
cfg 64/pkg/tool/linu/tmp/go-build532890590/b158/vet.cfg bash --no��
--noprofile 64/pkg/tool/linux_amd64/vet .13/x64/bash se cfg x_amd64/vet
ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build758752077/b330/launcher.test
/tmp/go-build758752077/b330/launcher.test
-test.testlogfile=/tmp/go-build758752077/b330/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go --local 86_64/as
user.email` (dns block)
> - Triggering command: `/tmp/go-build972084089/b334/launcher.test
/tmp/go-build972084089/b334/launcher.test
-test.testlogfile=/tmp/go-build972084089/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2374296311/b001/vet.cfg ache/go/1.25.8/x64/src/net
64/src/encoding/gob/dec_helpers.go cfg -I . -imultiarch
ache/go/1.25.8/x-buildtags -uns�� 752077/b258/_pkg-errorsas
/tmp/go-build758-ifaceassert ache/go/1.25.8/x-nilfunc _amd64.s
telabs/wazero/in-atomic -D_FORTIFY_SOURC-bool
ache/go/1.25.8/x-buildtags` (dns block)
> - Triggering command: `/tmp/go-build2612040108/b334/launcher.test
/tmp/go-build2612040108/b334/launcher.test
-test.testlogfile=/tmp/go-build2612040108/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s /usr�� ry=1
ga/m4mMaA4EfUuX_-tests bash
/opt/hostedtoolc/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet
cfg 64/pkg/tool/linu/tmp/go-build532890590/b158/vet.cfg bash --no��
--noprofile 64/pkg/tool/linux_amd64/vet .13/x64/bash se cfg x_amd64/vet
ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build758752077/b339/mcp.test
/tmp/go-build758752077/b339/mcp.test
-test.testlogfile=/tmp/go-build758752077/b339/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go --local
ndor/bin/as committer.name` (dns block)
> - Triggering command: `/tmp/go-build972084089/b343/mcp.test
/tmp/go-build972084089/b343/mcp.test
-test.testlogfile=/tmp/go-build972084089/b343/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -uns�� -unreachable=false
/tmp/go-build758752077/b059/vet.cfg
ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet -dumpbase _cgo_.c
-dumpbase-ext ache/go/1.25.8/x-importcfg -I /opt/hostedtoolc-s -I bash
--gdwarf-5 --64 -o /opt/hostedtoolc-extld=gcc` (dns block)
> - Triggering command: `/tmp/go-build2612040108/b343/mcp.test
/tmp/go-build2612040108/b343/mcp.test
-test.testlogfile=/tmp/go-build2612040108/b343/testlog.txt
-test.paniconexit0 -test.timeout=10m0s --no�� --noprofile
pA/uZZaPODy050ia-buildtags docker-buildx
/opt/hostedtoolc/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet
cfg 64/pkg/tool/linu/tmp/go-build532890590/b139/vet.cfg docker-buildx
-ato�� /logger/common.go /logger/file_logger.go
64/pkg/tool/linux_amd64/compile -errorsas -ifaceassert -nilfunc
64/pkg/tool/linux_amd64/compile` (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>
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Send tasks to Copilot coding agent from
[Slack](https://gh.io/cca-slack-docs) and
[Teams](https://gh.io/cca-teams-docs) to turn conversations into code.
Copilot posts an update in your thread when it's finished.7 files changed
Lines changed: 228 additions & 204 deletions
File tree
- internal
- config
- logger
- server
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
23 | 38 | | |
24 | 39 | | |
25 | 40 | | |
| |||
105 | 120 | | |
106 | 121 | | |
107 | 122 | | |
108 | | - | |
| 123 | + | |
109 | 124 | | |
110 | 125 | | |
111 | 126 | | |
| |||
134 | 149 | | |
135 | 150 | | |
136 | 151 | | |
137 | | - | |
| 152 | + | |
138 | 153 | | |
139 | 154 | | |
140 | 155 | | |
| |||
150 | 165 | | |
151 | 166 | | |
152 | 167 | | |
153 | | - | |
| 168 | + | |
154 | 169 | | |
155 | 170 | | |
156 | 171 | | |
157 | | - | |
| 172 | + | |
158 | 173 | | |
159 | 174 | | |
160 | 175 | | |
161 | 176 | | |
162 | | - | |
| 177 | + | |
163 | 178 | | |
164 | 179 | | |
165 | 180 | | |
| |||
403 | 418 | | |
404 | 419 | | |
405 | 420 | | |
406 | | - | |
| 421 | + | |
407 | 422 | | |
408 | 423 | | |
409 | 424 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
28 | 22 | | |
29 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
30 | 27 | | |
31 | 28 | | |
32 | | - | |
33 | | - | |
34 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
35 | 32 | | |
36 | 33 | | |
37 | 34 | | |
| |||
40 | 37 | | |
41 | 38 | | |
42 | 39 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
46 | 43 | | |
47 | 44 | | |
48 | 45 | | |
49 | 46 | | |
50 | 47 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
59 | 54 | | |
60 | 55 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 56 | + | |
| 57 | + | |
66 | 58 | | |
67 | 59 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 60 | + | |
| 61 | + | |
75 | 62 | | |
76 | 63 | | |
77 | 64 | | |
| |||
81 | 68 | | |
82 | 69 | | |
83 | 70 | | |
84 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
85 | 74 | | |
86 | 75 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 76 | + | |
92 | 77 | | |
93 | 78 | | |
94 | 79 | | |
95 | 80 | | |
96 | 81 | | |
97 | 82 | | |
98 | 83 | | |
99 | | - | |
100 | | - | |
| 84 | + | |
| 85 | + | |
101 | 86 | | |
102 | 87 | | |
103 | 88 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
26 | 51 | | |
27 | 52 | | |
28 | 53 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 54 | + | |
56 | 55 | | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | | - | |
61 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
66 | 72 | | |
67 | 73 | | |
68 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
40 | 56 | | |
41 | 57 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
72 | 73 | | |
73 | 74 | | |
| 75 | + | |
| 76 | + | |
74 | 77 | | |
75 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
76 | 83 | | |
77 | 84 | | |
78 | 85 | | |
| |||
0 commit comments