Skip to content

Improve azd ai agent init for non-interactive environments#6823

Open
spboyer wants to merge 3 commits intoAzure:mainfrom
spboyer:fix/ai-agent-init-non-interactive
Open

Improve azd ai agent init for non-interactive environments#6823
spboyer wants to merge 3 commits intoAzure:mainfrom
spboyer:fix/ai-agent-init-non-interactive

Conversation

@spboyer
Copy link
Member

@spboyer spboyer commented Feb 20, 2026

Problem

azd ai agent init is unusable in non-interactive (non-TTY) environments because two required inputs (environment name and subscription) have no non-interactive defaults. This blocks CI/CD pipelines, IDE-integrated terminals, AI coding assistants, and scripted automation.

Root cause analysis identified 4 sequential blockers when running without a TTY. This PR addresses the first two:

Changes

1. Default environment name to directory basename (#6817)

When --no-prompt is active and no -e flag is provided, the environment name now defaults to the current directory's basename -- consistent with how azd init suggests -dev etc.

Before: no default response for prompt 'Enter a unique environment name:'
After: Automatically creates environment named after the working directory

2. Auto-select subscription in non-interactive mode (#6818)

When --no-prompt is active and no subscription is configured, the command now:

  • Lists available subscriptions via azdClient.Account().ListSubscriptions()
  • Auto-selects if exactly one subscription exists
  • Returns a clear error with remediation hint if multiple subscriptions exist

Before: failed to prompt for subscription: rpc error: ... cannot prompt in non-interactive mode
After:

  • 1 subscription: Auto-selected subscription: My Sub (guid)
  • Multiple: multiple subscriptions found but running in non-interactive mode. Set a default with 'azd config set defaults.subscription <id>'

Testing

All existing extension tests pass. Changes are in the initialization flow which requires end-to-end testing with Azure resources.

Related Issues

Fixes #6817
Fixes #6818

Copy link
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

Improves the azd ai agent init extension initialization flow for non-interactive environments by avoiding prompts for required Azure context when possible.

Changes:

  • Default newly created azd environment name to the current directory basename when --no-prompt is active and -e isn’t provided.
  • In --no-prompt mode, auto-select the Azure subscription when exactly one subscription is available; otherwise return a remediation error.
Comments suppressed due to low confidence (1)

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

  • When listing subscriptions in --no-prompt mode, consider passing the existing tenant from the environment (AZURE_TENANT_ID) as ListSubscriptionsRequest.TenantId when available. Otherwise, users with multiple tenants can hit the "multiple subscriptions" error even if only one subscription exists in the intended tenant.
			subsResp, listErr := azdClient.Account().ListSubscriptions(
				ctx,
				&azdext.ListSubscriptionsRequest{},
			)

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

@spboyer spboyer force-pushed the fix/ai-agent-init-non-interactive branch from 4710e8a to 2365d2d Compare February 23, 2026 18:17
envArgs := []string{"env", "new"}
if flags.env != "" {
envArgs = append(envArgs, flags.env)
} else if flags.NoPrompt {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to just pass --no-prompt to the env new call that we're making, and rely on however core wants to handle that flag, instead of having special logic in the extension? Seems better for consistency

spboyer and others added 3 commits February 25, 2026 19:03
Two fixes for non-TTY environments (CI/CD, IDE terminals, AI
assistants):

1. Default environment name to directory basename when --no-prompt
   is active and no -e flag is provided, consistent with azd init
   behavior. (Fixes Azure#6817)

2. Auto-select subscription when only one is available in
   non-interactive mode using azdClient.Account().ListSubscriptions().
   Shows clear error with remediation hint when multiple
   subscriptions exist. (Fixes Azure#6818)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Handle os.Getwd() failure with clear error suggesting '-e <name>'
- Sanitize directory basename for env name (replace invalid chars)
- Guard against edge cases (root dir, empty/dot names)
- Clarify '-e' hint: specify it needs an env with AZURE_SUBSCRIPTION_ID

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ests

- Query UserConfig for defaults.subscription before falling back to
  ListSubscriptions auto-select logic
- Extract sanitizeEnvName helper function for testability
- Add 12 test cases covering valid, invalid, and edge-case env names

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@spboyer spboyer force-pushed the fix/ai-agent-init-non-interactive branch from 2365d2d to 1cdd108 Compare February 26, 2026 00:05
@spboyer
Copy link
Member Author

spboyer commented Feb 26, 2026

Good points from all three of you. To clarify the intent:

This was originally a quick unblock for AI coding assistants (Copilot CLI, etc.) that need to run azd ai agent init --no-prompt end-to-end without a TTY. The core azd init flow doesn't hit this code path — the extension has its own InitAction that independently prompts for environment name and subscription.

That said, I agree the principle should live in core:

  • Default env name from directory basename in --no-prompt mode
  • Subscription auto-select / defaults.subscription fallback in --no-prompt mode
  • Passing --no-prompt through to sub-commands like env new

Proposal: I'll file a core issue to track making --no-prompt smarter across all azd commands, and we can reference that from here. For this PR, the question is whether to:

  1. Keep as-is — extension-local workaround while core catches up
  2. Simplify — pass --no-prompt to env new (trangevi's suggestion) and only keep the subscription auto-select logic
  3. Close — wait for core support

@vhvb1989 @trangevi @kristenwomack what's your preference? Happy to go any direction.

@vhvb1989
Copy link
Member

Good points from all three of you. To clarify the intent:

This was originally a quick unblock for AI coding assistants (Copilot CLI, etc.) that need to run azd ai agent init --no-prompt end-to-end without a TTY. The core azd init flow doesn't hit this code path — the extension has its own InitAction that independently prompts for environment name and subscription.

That said, I agree the principle should live in core:

  • Default env name from directory basename in --no-prompt mode
  • Subscription auto-select / defaults.subscription fallback in --no-prompt mode
  • Passing --no-prompt through to sub-commands like env new

Proposal: I'll file a core issue to track making --no-prompt smarter across all azd commands, and we can reference that from here. For this PR, the question is whether to:

  1. Keep as-is — extension-local workaround while core catches up
  2. Simplify — pass --no-prompt to env new (trangevi's suggestion) and only keep the subscription auto-select logic
  3. Close — wait for core support

@vhvb1989 @trangevi @kristenwomack what's your preference? Happy to go any direction.

My preference, in order would be 2, 2 and 1 at the end. - I would swap 1 to the top if there is some customer blocked or some team is planning to demo/present this in the nearest feature.

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.

azd ai agent init: Auto-select subscription in non-interactive mode azd ai agent init: Default environment name to directory name

5 participants