Commit 165a060
authored
Refactor duplicate code patterns in logger and DIFC packages (#1791)
Addresses duplicate code analysis findings: ~60 lines of repeated
mutex-locking and logging patterns across logger and DIFC packages.
## Changes
**DIFC Tag Mutation (#1767)** - 21 lines reduced
- Extracted `modifyTag` helper to consolidate mutex-lock + dual-logging
pattern
- Refactored `AddSecrecyTag`, `AddIntegrityTag`, `DropIntegrityTag`
```go
// Before: duplicated 7-line pattern in each method
func (a *AgentLabels) AddSecrecyTag(tag Tag) {
logAgent.Printf("Agent %s adding secrecy tag: %s", a.AgentID, tag)
a.mu.Lock()
defer a.mu.Unlock()
a.Secrecy.Label.Add(tag)
log.Printf("[DIFC] Agent %s gained secrecy tag: %s", a.AgentID, tag)
}
// After: unified helper
func (a *AgentLabels) AddSecrecyTag(tag Tag) {
a.modifyTag("secrecy", "adding", "gained", tag, func() {
a.Secrecy.Label.Add(tag)
})
}
```
**Global Logger RWMutex Access (#1768)** - 40 lines reduced
- Created generic `withGlobalLogger` helper with `closableLogger`
constraint
- Refactored 8+ call sites across file_logger, markdown_logger,
jsonl_logger, server_file_logger, tools_logger, rpc_logger
```go
// Before: duplicated RWMutex pattern
globalLoggerMu.RLock()
defer globalLoggerMu.RUnlock()
if globalLogger != nil {
globalLogger.DoSomething(args...)
}
// After: type-safe generic helper
withGlobalLogger(&globalLoggerMu, &globalLogger, func(logger *Logger) {
logger.DoSomething(args...)
})
```
**Logger Initialization (#1766)** - No changes required
- Reviewed existing `initLogger` generic - already well-abstracted
- Documented pattern rationale in common.go
## Documentation
Added "Global Logger RWMutex Access Pattern" section to
`internal/logger/common.go` explaining the refactoring approach and
benefits.
> [!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-build2039083650/b332/launcher.test
/tmp/go-build2039083650/b332/launcher.test
-test.testlogfile=/tmp/go-build2039083650/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true se stmain.go
ache/go/1.25.7/x64/pkg/tool/linux_amd64/link . --gdwarf2 --64
ache/go/1.25.7/x64/pkg/tool/linuHEAD -I 7410264/b320/difc.test -I
86_64/git --gdwarf-5 --64 -o
Rlvht1SmvPjlx/EgKV67uNxy1s5Sk4WJTJ/FFffmGA1P5hLrHEAD` (dns block)
> - Triggering command: `/tmp/go-build139309952/b332/launcher.test
/tmp/go-build139309952/b332/launcher.test
-test.testlogfile=/tmp/go-build139309952/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true .cfg
/opt/hostedtoolc-goversion 64/pkg/tool/linux_amd64/vet -bool -buildtags
ache/go/1.25.7/x/tmp/go-build139309952/b319/_pkg_.a
64/pkg/tool/linu-trimpath rev-�� .cfg HEAD ache/go/1.25.7/x-lang=go1.25
--abbrev-ref HEAD tnet/tools/git tf "%s%s", sep, -goversion`
(dns block)
> - Triggering command: `/tmp/go-build311971124/b336/launcher.test
/tmp/go-build311971124/b336/launcher.test
-test.testlogfile=/tmp/go-build311971124/b336/testlog.txt
-test.paniconexit0 -test.timeout=10m0s star��` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build2039083650/b314/config.test
/tmp/go-build2039083650/b314/config.test
-test.testlogfile=/tmp/go-build2039083650/b314/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 7410264/b253/_pkg_.a
/tmp/go-build1837410264/b068/vet.cfg
ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet -dumpbase ntio/asm/base64
-dumpbase-ext ache/go/1.25.7/xHEAD eU_a��
/opt/hostedtoolcache/go/1.25.7/x64/src/net -I rgo/bin/git
7410264/b253/symbase64 --64 -o /opt/hostedtoolcache/go/1.25.7/xHEAD`
(dns block)
> - Triggering command: `/tmp/go-build139309952/b314/config.test
/tmp/go-build139309952/b314/config.test
-test.testlogfile=/tmp/go-build139309952/b314/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true ;
s#^/home/REDACTED/.nvm/##;
\#^[^v]# d;
\#^versions$# d;
tr /opt/hostedtoolcache/go/1.25.7/xHEAD 64/pkg/tool/linux_amd64/vet
pkg/mod/github.cgit pkg/mod/github.crev-parse
ache/Python/3.12--abbrev-ref 64/pkg/tool/linuHEAD /usr�� .cfg
/opt/hostedtoolc-test.timeout=10m0s 64/pkg/tool/linux_amd64/vet
github.com/segmebase64 -trimpath .12/x64/git
64/pkg/tool/linux_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build311971124/b318/config.test
/tmp/go-build311971124/b318/config.test
-test.testlogfile=/tmp/go-build311971124/b318/testlog.txt
-test.paniconexit0 -test.timeout=10m0s .pro��
/opt/hostedtoolc--abbrev-ref /opt/hostedtoolcHEAD
d-dispatcher/off.d/chrony-onoffline /tmp/go-build186dirname git
64/pkg/tool/linux_amd64/compile d-dispatcher/off.d/chrony-onoffline
-nam�� moby -id /usr/bin/runc.original -address /run/containerd/-d
docker-buildx /usr/bin/runc.original` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build2039083650/b332/launcher.test
/tmp/go-build2039083650/b332/launcher.test
-test.testlogfile=/tmp/go-build2039083650/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true se stmain.go
ache/go/1.25.7/x64/pkg/tool/linux_amd64/link . --gdwarf2 --64
ache/go/1.25.7/x64/pkg/tool/linuHEAD -I 7410264/b320/difc.test -I
86_64/git --gdwarf-5 --64 -o
Rlvht1SmvPjlx/EgKV67uNxy1s5Sk4WJTJ/FFffmGA1P5hLrHEAD` (dns block)
> - Triggering command: `/tmp/go-build139309952/b332/launcher.test
/tmp/go-build139309952/b332/launcher.test
-test.testlogfile=/tmp/go-build139309952/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true .cfg
/opt/hostedtoolc-goversion 64/pkg/tool/linux_amd64/vet -bool -buildtags
ache/go/1.25.7/x/tmp/go-build139309952/b319/_pkg_.a
64/pkg/tool/linu-trimpath rev-�� .cfg HEAD ache/go/1.25.7/x-lang=go1.25
--abbrev-ref HEAD tnet/tools/git tf "%s%s", sep, -goversion`
(dns block)
> - Triggering command: `/tmp/go-build311971124/b336/launcher.test
/tmp/go-build311971124/b336/launcher.test
-test.testlogfile=/tmp/go-build311971124/b336/testlog.txt
-test.paniconexit0 -test.timeout=10m0s star��` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build2039083650/b332/launcher.test
/tmp/go-build2039083650/b332/launcher.test
-test.testlogfile=/tmp/go-build2039083650/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true se stmain.go
ache/go/1.25.7/x64/pkg/tool/linux_amd64/link . --gdwarf2 --64
ache/go/1.25.7/x64/pkg/tool/linuHEAD -I 7410264/b320/difc.test -I
86_64/git --gdwarf-5 --64 -o
Rlvht1SmvPjlx/EgKV67uNxy1s5Sk4WJTJ/FFffmGA1P5hLrHEAD` (dns block)
> - Triggering command: `/tmp/go-build139309952/b332/launcher.test
/tmp/go-build139309952/b332/launcher.test
-test.testlogfile=/tmp/go-build139309952/b332/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true .cfg
/opt/hostedtoolc-goversion 64/pkg/tool/linux_amd64/vet -bool -buildtags
ache/go/1.25.7/x/tmp/go-build139309952/b319/_pkg_.a
64/pkg/tool/linu-trimpath rev-�� .cfg HEAD ache/go/1.25.7/x-lang=go1.25
--abbrev-ref HEAD tnet/tools/git tf "%s%s", sep, -goversion`
(dns block)
> - Triggering command: `/tmp/go-build311971124/b336/launcher.test
/tmp/go-build311971124/b336/launcher.test
-test.testlogfile=/tmp/go-build311971124/b336/testlog.txt
-test.paniconexit0 -test.timeout=10m0s star��` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build2039083650/b341/mcp.test
/tmp/go-build2039083650/b341/mcp.test
-test.testlogfile=/tmp/go-build2039083650/b341/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
/opt/hostedtoolcache/go/1.25.7/x64/src/net -I iptables --gdwarf-5 --64
-o as -I /opt/hostedtoolcache/go/1.25.7/x64/src/net -I
7410264/b248/vet.cfg --gdwarf-5 --64 -o */bin.*$") { next }
}
{ printf "%s%s", sep, HEAD` (dns block)
> - Triggering command: `/tmp/go-build139309952/b341/mcp.test
/tmp/go-build139309952/b341/mcp.test
-test.testlogfile=/tmp/go-build139309952/b341/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true -importcfg
4E-sSYy/pwgIjRIG-w ache/go/1.25.7/x-buildmode=exe --abbrev-ref HEAD
nfig/composer/ve--version ache/go/1.25.7/x-extld=gcc` (dns block)
> - Triggering command: `/tmp/go-build311971124/b345/mcp.test
/tmp/go-build311971124/b345/mcp.test
-test.testlogfile=/tmp/go-build311971124/b345/testlog.txt
-test.paniconexit0 -test.timeout=10m0s /usr��
1dedc330cd146d1731f9c903fe295e422bb440808c4a20ae
/var/run/docker/runtime-runc/moby 86_64/git
/run/containerd//opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet
--log-format json 1a2fc60e6817294b-buildtags /usr�� --root
/var/run/docker/-ifaceassert by/7842e098fbefc-nilfunc
/run/containerd/bash 1a2fc60e6817294b/usr/bin/runc json
1a2fc60e6817294b-tests` (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>9 files changed
Lines changed: 156 additions & 90 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
45 | 53 | | |
46 | 54 | | |
47 | | - | |
48 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
49 | 64 | | |
50 | 65 | | |
51 | 66 | | |
52 | 67 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
58 | 71 | | |
59 | 72 | | |
60 | 73 | | |
61 | 74 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
67 | 78 | | |
68 | 79 | | |
69 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
227 | 265 | | |
228 | 266 | | |
229 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
116 | 117 | | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
123 | 121 | | |
124 | 122 | | |
125 | 123 | | |
| |||
| 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 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
26 | 59 | | |
27 | 60 | | |
28 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| 111 | + | |
111 | 112 | | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
141 | 137 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
176 | 178 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
184 | 183 | | |
185 | 184 | | |
186 | 185 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
102 | 100 | | |
103 | 101 | | |
104 | 102 | | |
| |||
124 | 122 | | |
125 | 123 | | |
126 | 124 | | |
127 | | - | |
| 125 | + | |
| 126 | + | |
128 | 127 | | |
129 | 128 | | |
130 | 129 | | |
131 | 130 | | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
139 | 135 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
| 162 | + | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
172 | 169 | | |
173 | 170 | | |
174 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
| 135 | + | |
135 | 136 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 137 | + | |
| 138 | + | |
141 | 139 | | |
142 | 140 | | |
143 | 141 | | |
144 | | - | |
| 142 | + | |
145 | 143 | | |
146 | 144 | | |
147 | 145 | | |
| |||
0 commit comments