Skip to content

Add AI agent init naming safeguards#8189

Open
glharper wants to merge 4 commits into
mainfrom
glharper/ai-agent-init-unique-agent-name
Open

Add AI agent init naming safeguards#8189
glharper wants to merge 4 commits into
mainfrom
glharper/ai-agent-init-unique-agent-name

Conversation

@glharper
Copy link
Copy Markdown
Member

Summary

  • add --agent-name to azd ai agent init for setting the Foundry agent identity
  • write selected agent names into generated agent.yaml for manifest and from-code init flows
  • warn when init or deploy will version an existing Foundry agent instead of creating a separate agent
  • add tests for agent-name validation, manifest updates, existing-agent checks, and deploy warnings

Addresses #8107

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/init.go Outdated
Comment thread cli/azd/extensions/azure.ai.agents/internal/cmd/init.go Outdated
@glharper glharper marked this pull request as ready for review May 15, 2026 13:05
Copilot AI review requested due to automatic review settings May 15, 2026 13:05
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 15, 2026

🔗 Linked Issue Required

Thanks for the contribution! Please link a GitHub issue to this PR by adding Fixes #123 to the description or using the sidebar.
No issue yet? Feel free to create one!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 enhances the azure.ai.agents extension’s init/deploy UX to reduce accidental “versioning” of an existing Foundry agent by name, by allowing users to choose an explicit agent name during azd ai agent init and warning when a name already exists.

Changes:

  • Added --agent-name to azd ai agent init, validating and writing the selected agent name into the generated agent.yaml.
  • Added “existing agent name” detection and warnings during init (when the Foundry endpoint is available) and during deploy/update flows.
  • Added unit tests covering agent existence detection, warning text generation, and init agent-name validation/helpers.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cli/azd/extensions/azure.ai.agents/internal/project/service_target_agent.go Adds agent existence check helper + standardized warning for deploy paths when an agent name already exists.
cli/azd/extensions/azure.ai.agents/internal/project/service_target_agent_test.go Adds tests for agent existence detection and warning message content.
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go Adds --agent-name, name validation, manifest template name updates, and init-time existing-agent conflict handling.
cli/azd/extensions/azure.ai.agents/internal/cmd/init_test.go Adds tests for --agent-name flag registration, name validation, and helper behavior.
cli/azd/extensions/azure.ai.agents/internal/cmd/init_from_code.go Reuses shared agent-name resolution + adds existing-agent conflict check before writing definition name.
cli/azd/extensions/azure.ai.agents/internal/cmd/init_from_code_test.go Strengthens sanitizeAgentName test by asserting the result is a valid agent name.
Comments suppressed due to low confidence (1)

cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:369

  • resolveExistingAgentNameConflict silently skips the existing-agent check when Environment().GetValue returns an error. This can cause the new naming safeguard to be bypassed without any user feedback (and without logging), even though the Foundry endpoint might be available. Consider either returning an error, or at least emitting a warning/debug log when the env lookup fails so users understand the check could not be performed.
	endpointResp, err := azdClient.Environment().GetValue(ctx, &azdext.GetEnvRequest{
		EnvName: environment.Name,
		Key:     "AZURE_AI_PROJECT_ENDPOINT",
	})
	if err != nil || endpointResp == nil || endpointResp.Value == "" {
		return agentName, nil
	}

Comment on lines +330 to +336
func existingAgentWarning(agentName string) string {
return output.WithWarningFormat(
"An agent named '%s' already exists in this Foundry project. "+
"Deploying with this name will create a new version of the existing agent, not a separate agent.\n",
agentName,
)
}
Comment on lines +784 to +790
func existingAgentVersionWarning(agentName string) string {
return output.WithWarningFormat(
"An agent named '%s' already exists in this Foundry project. "+
"Deploying with this name will create a new version of the existing agent, not a separate agent.\n",
agentName,
)
}
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@glharper
Copy link
Copy Markdown
Member Author

@copilot apply changes based on the comments in this thread

…al/pkg/agents

Agent-Logs-Url: https://github.com/Azure/azure-dev/sessions/973eae4f-d50f-4cb3-b9b3-71725592ea3b

Co-authored-by: glharper <64209257+glharper@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

@copilot apply changes based on the comments in this thread

Done in c40b49e. Extracted the shared AgentChecker interface, AgentExists function, and ExistingAgentWarning string formatter into internal/pkg/agents/existence.go. Both cmd/init.go and project/service_target_agent.go now delegate to the shared helpers, eliminating the duplicated warning text and existence-check logic.

@glharper
Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

…t-unique-agent-name

# Conflicts:
#	cli/azd/extensions/azure.ai.agents/internal/project/service_target_agent_test.go

Co-authored-by: glharper <64209257+glharper@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

@copilot resolve the merge conflicts in this pull request

Resolved in b0d8bfa. The only conflict was in service_target_agent_test.go where our agents import and the incoming exterrors import both needed to be kept. All tests pass after the merge.

Copy link
Copy Markdown
Member

@jongio jongio left a comment

Choose a reason for hiding this comment

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

Reviewed 7 files (+605/-14). Clean approach to the naming safeguards. The existence.go extraction is well-placed, and the init flow integration is solid. A few minor observations below.

"google.golang.org/grpc/status"
)

type fakeAgentGetter struct {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

low fakeAgentGetter here and fakeAgentExistenceClient in service_target_agent_test.go are structurally identical - both test agents.AgentExists with the same logic. Since AgentExists lives in the agents package, its unit tests could live in existence_test.go there, and each consumer test file would focus on its own integration.

EnvName: environment.Name,
Key: "AZURE_AI_PROJECT_ENDPOINT",
})
if err != nil || endpointResp == nil || endpointResp.Value == "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

low When GetValue fails or returns empty, this silently returns the original name with no signal. A log.Printf here would help debugging when the naming check doesn't fire as expected.

}
}

func nextAgentNameSuggestion(agentName string) string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit This always suggests -2 as a suffix. If my-agent-2 also exists, the next suggestion becomes my-agent-2-2. Not a big deal since users can type any name, but an increment-aware suffix would be slightly smoother UX.

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.

4 participants