Commit 6470017
authored
refactor(difc): reduce boilerplate duplication in agent.go and labels.go (#2591)
Three structural duplication patterns in `internal/difc/` required
updating multiple identical code blocks for any locking strategy, log
format, or label type changes.
## Changes
- **`modifyTags` helper** — bulk label mutations (`AddSecrecyTags`,
`AddIntegrityTags`, `DropIntegrityTags`) were each inlining the same
lock/operate/log pattern instead of using the existing `modifyTag`
precedent:
```go
func (a *AgentLabels) modifyTags(labelType, action, pastTense string,
tags []Tag, fn func()) {
logAgent.Printf("Agent %s %s %d %s tags", a.AgentID, action, len(tags),
labelType)
a.mu.Lock()
defer a.mu.Unlock()
fn()
if len(tags) > 0 {
log.Printf("[DIFC] Agent %s %s %s tags: %v", a.AgentID, pastTense,
labelType, tags)
}
}
// callers become one-liners:
func (a *AgentLabels) AddSecrecyTags(tags []Tag) {
a.modifyTags("secrecy", "adding", "gained", tags, func() {
a.Secrecy.Label.AddAll(tags) })
}
```
- **`getTagsLocked` helper** — `GetSecrecyTags` and `GetIntegrityTags`
each duplicated the same `RLock` + `GetTags()` body; extracted to a
single nil-guarded helper.
- **`cloneLabelOrNew` helper** — `SecrecyLabel.Clone` and
`IntegrityLabel.Clone` shared identical nil-guard logic; extracted to a
standalone function so both `Clone` methods become single-line
delegations.
> [!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-build220454796/b334/launcher.test
/tmp/go-build220454796/b334/launcher.test
-test.testlogfile=/tmp/go-build220454796/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s rtcf�� ache/go/1.25.8/x-s
64/src/crypto/hm-w x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build988514342/b330/launcher.test
/tmp/go-build988514342/b330/launcher.test
-test.testlogfile=/tmp/go-build988514342/b330/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
1afb76c6d5a1b24a/run/containerd/io.containerd.runtime.v2.task/moby/c066a1e0c75e18fce6c91b9a92a66sed
y e-handler by/2dfd37f3cd6c3git lang.org/x/text/config` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build220454796/b319/config.test
/tmp/go-build220454796/b319/config.test
-test.testlogfile=/tmp/go-build220454796/b319/testlog.txt
-test.paniconexit0 -test.timeout=10m0s conf�� 64/src/runtime/cgo go
x_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build988514342/b315/config.test
/tmp/go-build988514342/b315/config.test
-test.testlogfile=/tmp/go-build988514342/b315/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
f6977767ec5dcecf/run/containerd/io.containerd.runtime.v2.task/moby/c066a1e0c75e18fce6c91b9a92a66/bin/sh
runtime-runc/mob--log-format csi/net-interfacjson by/37580e7ab5c50git`
(dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build220454796/b334/launcher.test
/tmp/go-build220454796/b334/launcher.test
-test.testlogfile=/tmp/go-build220454796/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s rtcf�� ache/go/1.25.8/x-s
64/src/crypto/hm-w x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build988514342/b330/launcher.test
/tmp/go-build988514342/b330/launcher.test
-test.testlogfile=/tmp/go-build988514342/b330/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
1afb76c6d5a1b24a/run/containerd/io.containerd.runtime.v2.task/moby/c066a1e0c75e18fce6c91b9a92a66sed
y e-handler by/2dfd37f3cd6c3git lang.org/x/text/config` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build220454796/b334/launcher.test
/tmp/go-build220454796/b334/launcher.test
-test.testlogfile=/tmp/go-build220454796/b334/testlog.txt
-test.paniconexit0 -test.timeout=10m0s rtcf�� ache/go/1.25.8/x-s
64/src/crypto/hm-w x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build988514342/b330/launcher.test
/tmp/go-build988514342/b330/launcher.test
-test.testlogfile=/tmp/go-build988514342/b330/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
1afb76c6d5a1b24a/run/containerd/io.containerd.runtime.v2.task/moby/c066a1e0c75e18fce6c91b9a92a66sed
y e-handler by/2dfd37f3cd6c3git lang.org/x/text/config` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build220454796/b343/mcp.test
/tmp/go-build220454796/b343/mcp.test
-test.testlogfile=/tmp/go-build220454796/b343/testlog.txt
-test.paniconexit0 -test.timeout=10m0s o_.o�� 64/src/net
late/parse/lex.go x_amd64/vet -p
github.com/tetra-test.testlogfile=/tmp/go-build220454796/b334/testlog.txt
-lang=go1.24 x_amd64/vet 5991�� _.a 599188/b151/ x_amd64/vet --gdwarf-5
ternal/engine/wa-m -o dmA41Y_/RID864WwwFNZuxdU3EAU` (dns block)
> - Triggering command: `/tmp/go-build988514342/b339/mcp.test
/tmp/go-build988514342/b339/mcp.test
-test.testlogfile=/tmp/go-build988514342/b339/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
/tmp/go-build220454796/b370/_pkg_.a
by/ee0f09b892b0c84149e47062e494bfc467b029bcbe96e63e2b5ad75092fcc8cd
ker/cli-plugins/docker-buildx
by/ee0f09b892b0c/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet
2b5ad75092fcc8cd-atomic 76224512e8a2c58c-bool ker/cli-plugins/-buildtags
n-me�� tes.crt -goversion /home/REDACTED/wor-nilfunc -c=4
-nolocalimports dea2d1d50bb3e29a-stringintconv 4w8uw1gybk6` (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 -->
---
📱 Kick off Copilot coding agent tasks wherever you are with [GitHub
Mobile](https://gh.io/cca-mobile-docs), available on iOS and Android.2 files changed
Lines changed: 51 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
60 | 79 | | |
61 | 80 | | |
62 | 81 | | |
| |||
80 | 99 | | |
81 | 100 | | |
82 | 101 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
90 | 105 | | |
91 | 106 | | |
92 | 107 | | |
93 | 108 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
101 | 112 | | |
102 | 113 | | |
103 | 114 | | |
104 | 115 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
112 | 119 | | |
113 | 120 | | |
114 | 121 | | |
| |||
184 | 191 | | |
185 | 192 | | |
186 | 193 | | |
187 | | - | |
188 | | - | |
| 194 | + | |
| 195 | + | |
189 | 196 | | |
190 | 197 | | |
191 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
192 | 207 | | |
193 | 208 | | |
194 | 209 | | |
195 | 210 | | |
196 | | - | |
197 | | - | |
198 | | - | |
| 211 | + | |
199 | 212 | | |
200 | 213 | | |
201 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
| |||
279 | 288 | | |
280 | 289 | | |
281 | 290 | | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
| 291 | + | |
286 | 292 | | |
287 | 293 | | |
288 | 294 | | |
| |||
326 | 332 | | |
327 | 333 | | |
328 | 334 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
| 335 | + | |
333 | 336 | | |
334 | 337 | | |
335 | 338 | | |
| |||
0 commit comments