refactor(guard): extract RunLabelAgentForAgent to eliminate duplicate LabelAgent init pattern#7632
Merged
lpcox merged 3 commits intoJun 16, 2026
Conversation
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
Contributor
There was a problem hiding this comment.
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.RunLabelAgentForAgentwrapper to resolve agent labels viaAgentRegistrybefore delegating toRunLabelAgent. - 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
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentRegistry.GetOrCreate(agentID)followed byRunLabelAgent(...)was duplicated verbatim in bothinternal/proxy/proxy.go(initGuardPolicy) andinternal/server/guard_init.go(ensureGuardInitialized), creating a silent divergence risk for DIFC enforcement semantics.Changes
internal/guard/label_agent.go— NewRunLabelAgentForAgenthelper accepts a*difc.AgentRegistry+agentIDinstead of a pre-resolved*difc.AgentLabels; internally callsGetOrCreatethen delegates toRunLabelAgent:internal/proxy/proxy.goandinternal/server/guard_init.go— Replace the two-stepGetOrCreate+RunLabelAgentpair withRunLabelAgentForAgent. Each caller retains its own session caching,guardInitializedflag, and post-call mode update logic.internal/guard/init_test.go— Two new tests coverRunLabelAgentForAgentsuccess path (registry population verified) and error propagation.