Part of duplicate code analysis: #4458
Summary
The four-step DIFC component initialization sequence (ParseEnforcementMode → NewAgentRegistryWithDefaults → NewCapabilities → NewEvaluatorWithMode) is repeated in both the server and proxy packages, making it a refactoring opportunity.
Duplication Details
Pattern: DIFC component init sequence
- Severity: Medium
- Occurrences: 2 (constructor sites)
- Locations:
internal/server/unified.go lines ~143–168 (NewUnified)
internal/proxy/proxy.go lines ~103–124 (New)
internal/server/unified.go:
difcMode, err := difc.ParseEnforcementMode(cfg.DIFCMode)
if err != nil {
difcMode = difc.EnforcementStrict // default for server
}
// ... struct literal includes:
agentRegistry: difc.NewAgentRegistryWithDefaults(nil, nil),
capabilities: difc.NewCapabilities(),
evaluator: difc.NewEvaluatorWithMode(difcMode),
internal/proxy/proxy.go:
difcMode, err := difc.ParseEnforcementMode(cfg.DIFCMode)
if err != nil {
difcMode = difc.EnforcementFilter // default for proxy
}
// ... struct literal includes:
evaluator: difc.NewEvaluatorWithMode(difcMode),
agentRegistry: difc.NewAgentRegistryWithDefaults(nil, nil),
capabilities: difc.NewCapabilities(),
The two variants differ only in the fallback enforcement mode (EnforcementStrict vs EnforcementFilter) and an extra warning log in the proxy.
Impact Analysis
- Maintainability: If the DIFC initialization logic changes (e.g., new components added), both sites must be updated
- Bug Risk: The two sites already use different default modes silently — this could cause unexpected behavioral differences
- Code Bloat: ~8–10 lines duplicated
Refactoring Recommendations
- Add a
difc.NewComponents(modeStr string, defaultMode EnforcementMode) DIFCComponents constructor
type DIFCComponents struct {
Mode EnforcementMode
AgentRegistry *AgentRegistry
Capabilities *Capabilities
Evaluator *Evaluator
}
func NewComponents(modeStr string, defaultMode EnforcementMode) DIFCComponents { ... }
- Both server and proxy pass their chosen
defaultMode (EnforcementStrict / EnforcementFilter)
- Estimated effort: 1–2 hours
Implementation Checklist
Parent Issue
See parent analysis report: #4458
Related to #4458
Generated by Duplicate Code Detector · ● 3M · ◷
Part of duplicate code analysis: #4458
Summary
The four-step DIFC component initialization sequence (
ParseEnforcementMode→NewAgentRegistryWithDefaults→NewCapabilities→NewEvaluatorWithMode) is repeated in both theserverandproxypackages, making it a refactoring opportunity.Duplication Details
Pattern: DIFC component init sequence
internal/server/unified.golines ~143–168 (NewUnified)internal/proxy/proxy.golines ~103–124 (New)internal/server/unified.go:internal/proxy/proxy.go:The two variants differ only in the fallback enforcement mode (
EnforcementStrictvsEnforcementFilter) and an extra warning log in the proxy.Impact Analysis
Refactoring Recommendations
difc.NewComponents(modeStr string, defaultMode EnforcementMode) DIFCComponentsconstructordefaultMode(EnforcementStrict/EnforcementFilter)Implementation Checklist
DIFCComponentsstruct andNewComponentsconstructor tointernal/difcinternal/server/unified.go:NewUnifiedto usedifc.NewComponentsinternal/proxy/proxy.go:Newto usedifc.NewComponentsmake test-allto verify no regressionsParent Issue
See parent analysis report: #4458
Related to #4458