fix(agents): reuse existing azure.yaml agent config on init - #9404
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: fed9e97b-e79b-4889-ac76-0d9a428599cd
📋 Prioritization NoteThanks for the contribution! The linked issue isn't in the current milestone yet. |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 21 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Adds reuse of inline agent configuration from an existing project manifest during azd ai agent init.
Changes:
- Detects inline and legacy nested agent definitions.
- Reuses existing configuration without prompting.
- Adds manifest parsing and formatting tests.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
internal/cmd/init.go |
Integrates existing-project reuse detection. |
internal/cmd/init_reuse_project_agent.go |
Implements detection and reuse. |
internal/cmd/init_reuse_project_agent_test.go |
Tests manifest discovery and parsing. |
Jon Gallant (jongio)
left a comment
There was a problem hiding this comment.
Two things worth a second look before this merges.
The new azure.yaml reader re-implements parsing the extension already gets from the azd host, and it reads from --src rather than the project root. Details inline on init_reuse_project_agent.go.
The reuse gate only opts out on --agent-name, but --no-prompt makes reuse unconditional, so a scripted run in a repo that already declares an agent silently ignores every other agent-defining flag instead of adding a second agent. Details inline on init.go.
Two smaller ones inline: a validatePostInit call that can't do anything, and a swallowed YAML parse error.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Suppressed comments (4)
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:1313
- This fallback misses an
azure.ymlproject when the command is run from a subdirectory.azdext.GetProjectDironly walks upward forazure.yaml(cli/azd/pkg/azdext/project.go:16-35), socheckDirbecomes.and the subsequent shallow scan never reaches the parent manifest. Resolve the root using both supported project filenames and add a nested-directoryazure.ymltest.
if errors.Is(projectErr, azdext.ErrProjectNotFound) {
checkDir = "."
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:1311
- This line is not
gofmt-formatted, so the repository's formatting check will fail. Indent it consistently with the surrounding block.
checkDir, projectErr := azdext.GetProjectDir()
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:1357
- No test executes this new
RunEbranch orrunReuseProjectAgentServices; the added tests only validate YAML scanning and display formatting. Add command-level coverage for interactive accept/decline, no-prompt reuse, explicit-option opt-out, environment creation/reuse, and invocation from a nested project directory so the orchestration—not just its parser—is verified.
if err := runReuseProjectAgentServices(
ctx, flags, azdClient, checkDir, displayPath, agentServices,
cli/azd/extensions/azure.ai.agents/internal/cmd/init_reuse_project_agent.go:157
ensureProjectprintsFound existing azd project ... Adding agent to it.for an existing project (init.go:1876-1879), but this reuse path intentionally writes no service. Avoid that helper's mutation-oriented status message here, or refactor it so this path can report that the existing configuration is being reused rather than claiming an agent was added.
if _, err := ensureProject(ctx, flags, azdClient, "."); err != nil {
…arser Addresses review feedback on #9404. Detection went through its own azure.yaml reader rooted at --src, which is the agent source directory rather than the project root, so a project found by walking up from the cwd was missed and init re-prompted anyway. It also carried a private azure.yaml/azure.yml candidate list and a hand-rolled service struct. Ask the host instead: Project().Get() resolves the manifest the same way every other azd command does (walking up from the cwd, honoring azure.yml), and adoptedAgentNameConfig already reads the agent name from both the inline and the deprecated config: shapes. A project the host cannot load still yields no detections so init falls through to its normal prompts, but the cause is now logged instead of silently swallowed. Reuse also opted out only on --agent-name. Since --no-prompt makes reuse unconditional, a scripted run passing --deploy-mode/--runtime/--entry-point or any other agent-defining flag would silently no-op; every such flag now opts out. --src counts only when passed explicitly, because applyPositionalArg folds a positional path into the same field and `azd ai agent init .` must keep reusing. Also drops a validatePostInit call that could never run, since it returns immediately when its codeConfig argument is nil. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1d5b46e6-fc04-48bb-9a2c-156105966b48
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (2)
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:1074
- A positional directory is also an explicit source selection:
applyPositionalArgmapsazd ai agent init ./agents/newtoflags.src, just like--src. BecausesrcExplicitonly checks Cobra's flag state, this invocation is treated as having no agent-defining input; in--no-promptmode it silently reuses the existing service instead of initializing the supplied source directory. Preserve reuse only when the positional path resolves to the active project root (theinit .case), and treat other positional source paths as opt-outs. [azd-code-reviewer]
srcExplicit ||
cli/azd/extensions/azure.ai.agents/internal/cmd/init_reuse_project_agent.go:114
ensureProject's existing-project branch always printsFound existing azd project ... Adding agent to it.(init.go:1882-1885), but this reuse path deliberately writes no service. Every successful reuse therefore emits a false mutation status and may show add-agent-specific infra guidance. Use a read-only project check here, or split the generic project lookup from the add-agent messaging. [azd-code-reviewer]
if _, err := ensureProject(ctx, flags, azdClient, "."); err != nil {
return err
}
Jon Gallant (jongio)
left a comment
There was a problem hiding this comment.
One leftover from the rework, not a blocker.
runReuseProjectAgentServices still calls ensureProject, but that call can't do anything on this path anymore. Detection now runs through the host, so reaching the reuse branch already proves Project().Get() succeeded, which makes the scaffold branch inside ensureProject unreachable and its return value discarded.
Host-based detection already proves Project().Get succeeded before the reuse path runs. Calling ensureProject again could not reach its scaffold branch, discarded the returned project, and printed the false status that an agent was being added even though reuse intentionally writes no service. Remove the second round-trip and document the precondition. Also distinguish a positional project-root path from a positional agent source directory. `init .` at the project root can reuse its configured agent, while `init ./agents/new` must fall through and honor the selected source instead of silently ignoring it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1d5b46e6-fc04-48bb-9a2c-156105966b48
Reuse only project services whose definitions resolve successfully through the production inline, config, ref, or disk loader. Validate service source paths at the project boundary so absolute, traversal, and symlink escapes cannot authorize reuse. Run project-owned reuse before bare agent.yaml reuse so a configured disk-backed service is not added or replaced. Share caller-intent guards while retaining explicit --src support for the bare-file path that consumes it. Assemble next-step state from the environment selected for init without changing the active environment, and qualify emitted azd commands so copied guidance continues targeting that environment. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1d5b46e6-fc04-48bb-9a2c-156105966b48
Summary
azd ai agent initin a project that already has anazure.yamlre-prompted for agent name, protocols, and deploy mode instead of reading them from the manifest.Init already detects and reuses an existing
agent.manifest.yamlor a bareagent.yaml(the latter added for #7268 — "less to ask and just setup azure.yaml"). But the unified manifest work (d8f3abe) moved the agent definition inline onto theazure.yamlservice entry and stopped writing a standaloneagent.yaml:The reuse detection was never extended to look there, so a modern project matches neither existing check, falls through to
promptInitMode→ the from-code path, and re-asks for everythingazure.yamlalready answers.Changes
detectProjectAgentServices— asks the azd host for the project (Project().Get()), validates everyazure.ai.agentservice path inside the project boundary, and reuses only definitions that resolve through the production inline / deprecatedconfig:/$ref/ on-diskagent.yamlloader. Missing or invalid definitions are debug diagnostics, not successful reuse candidates.runReuseProjectAgentServices— completes init without re-prompting: host-based detection already proves the project exists, so it ensures the selected azd environment exists and hands that exact environment to the shared next-step resolverRunEafter manifest detection but before bareagent.yamlreuse, so a configured disk-backed service is treated as a project no-op rather than being added/replaced from its fileInteractive runs get a confirm (defaulting to yes) matching the sibling detection prompts; declining falls through to today's behavior, so adding a second agent to an existing project still works.
Notes
Project().Get()rather than readingazure.yamldirectly means init sees the same manifest every other azd command does: resolved by walking up from the working directory, and honoringazure.ymlas well asazure.yaml. It also reuses core's parsing instead of carrying a second, hand-rolled view of the service schema.project.LoadAgentDefinitionvalidates inline, deprecatedconfig:,$ref, and on-disk definitions. A barehost: azure.ai.agententry with no definition no longer makes--no-promptexit successfully.paths.JoinAllowRootrejects absolute, traversal, and symlink-escaping paths before a service can authorize project reuse.agent.yamlscan. This keeps an existing service'suses:and other fields intact when its definition lives on disk.Project().Get()errors on a malformed manifest (it goes throughproject.Load). Init treats that as nothing detected and falls through to the normal prompts, rather than failing on a file the user hasn't been asked about yet. The cause is logged, so--debugstill surfaces a typo.--no-promptmakes reuse unconditional, so reusing while the caller passed--deploy-mode,--runtime,--entry-point,--agent-name,--protocol,--model,--model-deployment,--project-id,--image, or--srcwould silently discard them. Those runs fall through to the normal flow instead.--envand--infradescribe the environment and the IaC output rather than the agent, so they stay compatible with reuse.--srcopts out of project no-op reuse but remains valid for bareagent.yamlreuse, which consumes it. A positional project-root or configured service-directory path may reuse; an unconfigured source directory opts out so it is not silently ignored.--environmentwithout changing azd's active environment, and every emitted azd command is qualified asazd --environment "<name>" ...; non-azdcd/ edit / README guidance remains unchanged.runInitFromAzureYaml) is deliberately not reused here: it refuses when a project manifest already exists, since merging a sample's services into an existingazure.yamlis tracked separately as [ext-agents]: azd ai agent init -m <azure.yaml> should merge into an existing project's azure.yaml #8884. This change is about respecting the manifest already present, not merging a new one in.Testing
go test ./... -count=1(all extension packages)go build ./...golangci-lint run ./internal/cmd/...— 0 issuesgofmt -s -l ./internal— cleancspell linton the changed files — 0 issuesNew coverage: inline /
config:/ disk definitions, missing definitions rejected, unsafe absolute / traversal / symlink service paths rejected, deterministic ordering, non-agent hosts ignored, project reuse before disk reuse from a service subdirectory, the project-vs-bare agent-defining flag matrix, positional root / configured service / unconfigured source classification, selected-environment state, and environment-qualified next-step commands.Fixes #9154