Commit 276875f
authored
refactor: extract difc.NewComponents to deduplicate DIFC initialization (#4498)
The four-step DIFC initialization sequence (`ParseEnforcementMode` →
`NewAgentRegistryWithDefaults` → `NewCapabilities` →
`NewEvaluatorWithMode`) was duplicated verbatim in both
`server/unified.go` and `proxy/proxy.go`, with silently diverging
default modes (`EnforcementStrict` vs `EnforcementFilter`).
## Changes
- **`internal/difc/evaluator.go`** — Adds `DIFCComponents` struct and
`NewComponents(modeStr string, defaultMode EnforcementMode)
DIFCComponents` constructor that encapsulates the full initialization
sequence
- **`internal/server/unified.go`** — Replaces inline DIFC init with
`difc.NewComponents(cfg.DIFCMode, difc.EnforcementStrict)`
- **`internal/proxy/proxy.go`** — Replaces inline DIFC init with
`difc.NewComponents(cfg.DIFCMode, difc.EnforcementFilter)`, preserving
the proxy-specific warning log for invalid mode strings
```go
// internal/difc/evaluator.go
type DIFCComponents struct {
Mode EnforcementMode
AgentRegistry *AgentRegistry
Capabilities *Capabilities
Evaluator *Evaluator
}
func NewComponents(modeStr string, defaultMode EnforcementMode) DIFCComponents {
mode, err := ParseEnforcementMode(modeStr)
if err != nil {
mode = defaultMode
}
return DIFCComponents{
Mode: mode,
AgentRegistry: NewAgentRegistryWithDefaults(nil, nil),
Capabilities: NewCapabilities(),
Evaluator: NewEvaluatorWithMode(mode),
}
}
```
Future changes to the DIFC initialization sequence (e.g. adding a new
component) now have a single update point.
> [!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-build2613986933/b513/launcher.test
/tmp/go-build2613986933/b513/launcher.test
-test.testlogfile=/tmp/go-build2613986933/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2613986933/b435/vet.cfg _.a -I x_amd64/vet --gdwarf-5
balancer/gracefu-atomic -o x_amd64/vet -W cks..." t.go
x_amd64/compile . --gdwarf2 893947/b314/ x_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build1963674460/b509/launcher.test
/tmp/go-build1963674460/b509/launcher.test
-test.testlogfile=/tmp/go-build1963674460/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true tHjXILjQ_ -buildtags
/opt/hostedtoolcache/go/1.25.9/xjson -errorsas -ifaceassert -nilfunc
StwtP8fWF_W- logs�� tes.crt aw-mcpg/internal/proxy/graphql_r--log-format
7c330d9c11d84b8d-d -errorsas -ifaceassert -nilfunc
ache/go/1.25.9/x-test.timeout=10m0s` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build2613986933/b495/config.test
/tmp/go-build2613986933/b495/config.test
-test.testlogfile=/tmp/go-build2613986933/b495/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2613986933/b368/vet.cfg _.a otection x_amd64/vet --gdwarf-5
credentials -o x_amd64/vet -E w6WVsrdoR -I x_amd64/vet -I .io/auto/sdk
-imultiarch x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build1963674460/b491/config.test
/tmp/go-build1963674460/b491/config.test
-test.testlogfile=/tmp/go-build1963674460/b491/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
-unreachable=fal--log-format /tmp/go-build261json docker-buildx
f19db7ad4b9e741c980b3140ec557422c5d/log.json nwind-tables p/bin/as
docker-buildx -ato�� 40a26d5ba8f85386 -buildtags ine
by/6e9cb3f11e119/usr/lib/open-iscsi/net-interface-handler -ifaceassert
-nilfunc ine` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build2613986933/b513/launcher.test
/tmp/go-build2613986933/b513/launcher.test
-test.testlogfile=/tmp/go-build2613986933/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2613986933/b435/vet.cfg _.a -I x_amd64/vet --gdwarf-5
balancer/gracefu-atomic -o x_amd64/vet -W cks..." t.go
x_amd64/compile . --gdwarf2 893947/b314/ x_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build1963674460/b509/launcher.test
/tmp/go-build1963674460/b509/launcher.test
-test.testlogfile=/tmp/go-build1963674460/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true tHjXILjQ_ -buildtags
/opt/hostedtoolcache/go/1.25.9/xjson -errorsas -ifaceassert -nilfunc
StwtP8fWF_W- logs�� tes.crt aw-mcpg/internal/proxy/graphql_r--log-format
7c330d9c11d84b8d-d -errorsas -ifaceassert -nilfunc
ache/go/1.25.9/x-test.timeout=10m0s` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build2613986933/b513/launcher.test
/tmp/go-build2613986933/b513/launcher.test
-test.testlogfile=/tmp/go-build2613986933/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build2613986933/b435/vet.cfg _.a -I x_amd64/vet --gdwarf-5
balancer/gracefu-atomic -o x_amd64/vet -W cks..." t.go
x_amd64/compile . --gdwarf2 893947/b314/ x_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build1963674460/b509/launcher.test
/tmp/go-build1963674460/b509/launcher.test
-test.testlogfile=/tmp/go-build1963674460/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true tHjXILjQ_ -buildtags
/opt/hostedtoolcache/go/1.25.9/xjson -errorsas -ifaceassert -nilfunc
StwtP8fWF_W- logs�� tes.crt aw-mcpg/internal/proxy/graphql_r--log-format
7c330d9c11d84b8d-d -errorsas -ifaceassert -nilfunc
ache/go/1.25.9/x-test.timeout=10m0s` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build2613986933/b522/mcp.test
/tmp/go-build2613986933/b522/mcp.test
-test.testlogfile=/tmp/go-build2613986933/b522/testlog.txt
-test.paniconexit0 -test.timeout=10m0s 8939�� cfg
elemetry.io/otel-ifaceassert x_amd64/vet --gdwarf-5
telabs/wazero/in/usr/bin/runc -o x_amd64/vet cfg 893947/b432/_pkg_.a
-trimpath x_amd64/vet -p /internal/httpco/usr/bin/runc -lang=go1.17
x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build1963674460/b518/mcp.test
/tmp/go-build1963674460/b518/mcp.test
-test.testlogfile=/tmp/go-build1963674460/b518/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true y -buildtags` (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>3 files changed
Lines changed: 49 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
92 | 123 | | |
93 | 124 | | |
94 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
110 | 108 | | |
111 | | - | |
| 109 | + | |
112 | 110 | | |
113 | 111 | | |
114 | 112 | | |
| |||
120 | 118 | | |
121 | 119 | | |
122 | 120 | | |
123 | | - | |
124 | | - | |
125 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
126 | 124 | | |
127 | 125 | | |
128 | | - | |
| 126 | + | |
129 | 127 | | |
130 | 128 | | |
131 | 129 | | |
| |||
144 | 142 | | |
145 | 143 | | |
146 | 144 | | |
147 | | - | |
| 145 | + | |
148 | 146 | | |
149 | 147 | | |
150 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
150 | 149 | | |
151 | 150 | | |
152 | 151 | | |
| |||
164 | 163 | | |
165 | 164 | | |
166 | 165 | | |
167 | | - | |
168 | | - | |
169 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
170 | 169 | | |
171 | 170 | | |
172 | 171 | | |
| |||
0 commit comments