Skip to content

refactor(guard): extract RunLabelAgentForAgent to eliminate duplicate LabelAgent init pattern#7632

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-guard-labelagent-initialization
Jun 16, 2026
Merged

refactor(guard): extract RunLabelAgentForAgent to eliminate duplicate LabelAgent init pattern#7632
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-guard-labelagent-initialization

Conversation

Copilot AI commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

AgentRegistry.GetOrCreate(agentID) followed by RunLabelAgent(...) was duplicated verbatim in both internal/proxy/proxy.go (initGuardPolicy) and internal/server/guard_init.go (ensureGuardInitialized), creating a silent divergence risk for DIFC enforcement semantics.

Changes

  • internal/guard/label_agent.go — New RunLabelAgentForAgent helper accepts a *difc.AgentRegistry + agentID instead of a pre-resolved *difc.AgentLabels; internally calls GetOrCreate then delegates to RunLabelAgent:

    func RunLabelAgentForAgent(
        ctx context.Context,
        g Guard,
        payload interface{},
        backend BackendCaller,
        caps *difc.Capabilities,
        registry *difc.AgentRegistry,
        agentID string,
        defaultMode difc.EnforcementMode,
    ) (difc.EnforcementMode, *LabelAgentResult, error) {
        agentLabels := registry.GetOrCreate(agentID)
        return RunLabelAgent(ctx, g, payload, backend, caps, agentLabels, defaultMode)
    }
  • internal/proxy/proxy.go and internal/server/guard_init.go — Replace the two-step GetOrCreate + RunLabelAgent pair with RunLabelAgentForAgent. Each caller retains its own session caching, guardInitialized flag, and post-call mode update logic.

  • internal/guard/init_test.go — Two new tests cover RunLabelAgentForAgent success path (registry population verified) and error propagation.

GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:13 View session
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:14
Copilot AI changed the title [WIP] Fix duplicate code in guard label agent initialization sequence refactor(guard): extract RunLabelAgentForAgent to eliminate duplicate LabelAgent init pattern Jun 16, 2026
GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:19 View session
Copilot finished work on behalf of lpcox June 16, 2026 17:19
Copilot AI requested a review from lpcox June 16, 2026 17:19
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:21
@lpcox lpcox marked this pull request as ready for review June 16, 2026 17:21
Copilot AI review requested due to automatic review settings June 16, 2026 17:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors DIFC guard initialization by extracting a shared RunLabelAgentForAgent helper, eliminating duplicated AgentRegistry.GetOrCreate(agentID) + RunLabelAgent(...) sequences across the proxy and unified server paths to reduce divergence risk in label initialization semantics.

Changes:

  • Added guard.RunLabelAgentForAgent wrapper to resolve agent labels via AgentRegistry before delegating to RunLabelAgent.
  • Updated proxy and unified server guard initialization code paths to use the new helper.
  • Added unit tests covering the helper’s success path (registry population) and error propagation.
Show a summary per file
File Description
internal/guard/label_agent.go Adds RunLabelAgentForAgent wrapper that performs registry.GetOrCreate(agentID) then calls RunLabelAgent.
internal/proxy/proxy.go Uses RunLabelAgentForAgent during proxy guard policy initialization to avoid duplicated label lookup logic.
internal/server/guard_init.go Uses RunLabelAgentForAgent during session guard initialization to keep behavior aligned with proxy init path.
internal/guard/init_test.go Adds tests for RunLabelAgentForAgent success and error propagation behavior.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 4/4 changed files
  • Comments generated: 1

Comment thread internal/guard/init_test.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:26 View session
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:27
@lpcox lpcox merged commit 4ecf858 into main Jun 16, 2026
27 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-guard-labelagent-initialization branch June 16, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants