Skip to content

refactor: use core env in Foundry service targets#9079

Open
huimiu wants to merge 5 commits into
mainfrom
hui/update-foundry-extension-env
Open

refactor: use core env in Foundry service targets#9079
huimiu wants to merge 5 commits into
mainfrom
hui/update-foundry-extension-env

Conversation

@huimiu

@huimiu huimiu commented Jul 10, 2026

Copy link
Copy Markdown
Member

Summary

  • Read service-scoped environment values from ServiceConfig.environment in the Foundry agent, connection, routine, and toolbox targets, with a fallback for older service definitions.
  • Write generated agent and resource variables through the standard service-level env object while preserving raw ${VAR} templates.
  • Update schemas, examples, minimum azd versions, and SDK dependencies for azd 1.27.1.

Follow-up to #8936.

Testing

  • go build ./... && go test ./... in all four changed extension modules.
  • golangci-lint run ./... in all four changed extension modules.

@github-actions github-actions Bot added ext-agents azure.ai.agents extension ext-connections azure.ai.connections extension ext-foundry azure.ai.{agents,connections,inspector,projects,routines,skills,toolboxes}, microsoft.foundry ext-routines azure.ai.routines extension ext-toolboxes azure.ai.toolboxes extension labels Jul 10, 2026
@huimiu huimiu marked this pull request as ready for review July 10, 2026 12:16
Copilot AI review requested due to automatic review settings July 10, 2026 12:16
@github-actions

Copy link
Copy Markdown

🔗 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!

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

Updates Foundry extensions to use azd 1.27.1’s service-level environment forwarding.

Changes:

  • Consume forwarded service environments with legacy fallback.
  • Move generated agent/resource variables into service-level env.
  • Update schemas, tests, dependencies, minimum versions, and changelogs.

Reviewed changes

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

Show a summary per file
File Description
cli/azd/extensions/microsoft.foundry/extension.yaml Requires azd 1.27.1.
cli/azd/extensions/microsoft.foundry/CHANGELOG.md Records the requirement change.
cli/azd/extensions/azure.ai.toolboxes/schemas/azure.ai.toolbox.json Documents service-level variables.
cli/azd/extensions/azure.ai.toolboxes/internal/cmd/service_target.go Uses forwarded environment values.
cli/azd/extensions/azure.ai.toolboxes/internal/cmd/service_target_test.go Tests forwarded-variable expansion.
cli/azd/extensions/azure.ai.toolboxes/go.sum Updates dependency checksums.
cli/azd/extensions/azure.ai.toolboxes/go.mod Uses azd SDK 1.27.1.
cli/azd/extensions/azure.ai.toolboxes/extension.yaml Requires azd 1.27.1.
cli/azd/extensions/azure.ai.toolboxes/CHANGELOG.md Documents the toolbox fix.
cli/azd/extensions/azure.ai.routines/schemas/azure.ai.routine.json Documents routine input variables.
cli/azd/extensions/azure.ai.routines/internal/cmd/service_target.go Uses forwarded environment values.
cli/azd/extensions/azure.ai.routines/internal/cmd/service_target_test.go Tests routine expansion.
cli/azd/extensions/azure.ai.routines/go.sum Updates dependency checksums.
cli/azd/extensions/azure.ai.routines/go.mod Uses azd SDK 1.27.1.
cli/azd/extensions/azure.ai.routines/extension.yaml Requires azd 1.27.1.
cli/azd/extensions/azure.ai.routines/CHANGELOG.md Documents the routine fix.
cli/azd/extensions/azure.ai.connections/schemas/azure.ai.connection.json Documents connection variables.
cli/azd/extensions/azure.ai.connections/internal/cmd/service_target.go Uses forwarded environment values.
cli/azd/extensions/azure.ai.connections/internal/cmd/service_target_test.go Tests connection expansion.
cli/azd/extensions/azure.ai.connections/go.sum Updates dependency checksums.
cli/azd/extensions/azure.ai.connections/go.mod Uses azd SDK 1.27.1.
cli/azd/extensions/azure.ai.connections/extension.yaml Requires azd 1.27.1.
cli/azd/extensions/azure.ai.connections/CHANGELOG.md Documents the connection fix.
cli/azd/extensions/azure.ai.agents/schemas/examples/simple.azure.yaml Demonstrates agent service env.
cli/azd/extensions/azure.ai.agents/schemas/examples/complex.azure.yaml Demonstrates resource-scoped variables.
cli/azd/extensions/azure.ai.agents/schemas/azure.ai.agent.json Removes duplicate inline env schema.
cli/azd/extensions/azure.ai.agents/internal/project/config.go Removes custom environment storage.
cli/azd/extensions/azure.ai.agents/internal/project/config_test.go Updates config round-trip coverage.
cli/azd/extensions/azure.ai.agents/internal/project/agent_definition.go Maps agent variables to core env.
cli/azd/extensions/azure.ai.agents/internal/project/agent_definition_test.go Tests new and legacy mappings.
cli/azd/extensions/azure.ai.agents/internal/cmd/resource_services.go Writes resource variable templates.
cli/azd/extensions/azure.ai.agents/internal/cmd/resource_services_test.go Tests template collection and writes.
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go Writes initialized agent environments.
cli/azd/extensions/azure.ai.agents/internal/cmd/init_test.go Verifies generated service env.
cli/azd/extensions/azure.ai.agents/internal/cmd/init_from_code.go Writes code-init agent environments.
cli/azd/extensions/azure.ai.agents/go.sum Updates dependency checksums.
cli/azd/extensions/azure.ai.agents/go.mod Uses azd SDK 1.27.1.
cli/azd/extensions/azure.ai.agents/extension.yaml Requires azd 1.27.1.
cli/azd/extensions/azure.ai.agents/CHANGELOG.md Documents the agent migration.


sectionValues := make(map[string]any, len(environment))
for key, value := range environment {
sectionValues[key] = value
environment = map[string]string{}
}
maps.Copy(environment, kv)
svc.Environment = environment

@jongio jongio left a comment

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.

One follow-up on the env migration path.

The new legacy-environment test covers the read path but not the optimize apply persist path through AddService, so it won't catch the ${VAR} to $${VAR} escaping that the existing review comment describes. Worth a persist-path test for that migration. Detail inline.

require.Equal(t, AgentDefinitionSourceLegacyConfig, source)
require.Equal(t, map[string]string{
"LEGACY_KEY": "${LEGACY_KEY}",
}, AgentEnvironment(got))

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.

This legacy-env test only exercises the read path (AgentDefinitionFromService then AgentEnvironment). It never runs the persist path that optimize apply uses (UpsertAgentEnvVars then Project().AddService), which is where a migrated ${LEGACY_KEY} is at risk.

When a service migrates from inline environmentVariables, it has no existing core env entry, so preserveUnchangedEnvTemplates in internal/grpcserver/project_service.go skips the key (if !has { continue }) and the mapper persists the value as an expanded literal, turning ${LEGACY_KEY} into $${LEGACY_KEY} in azure.yaml. So this test passes while the actual migration changes semantics.

Consider a test that drives the migration through AddService (or a recording project server) and asserts the persisted template stays ${LEGACY_KEY}.

@huimiu huimiu changed the title fix: use core env in Foundry service targets refactor: use core env in Foundry service targets Jul 14, 2026
@huimiu

huimiu commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

Co-authored-by: huimiu <107838226+huimiu@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 14, 2026 09:54
@huimiu huimiu removed the request for review from Copilot July 14, 2026 09:54
Co-authored-by: huimiu <107838226+huimiu@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 14, 2026 09:59
@huimiu huimiu removed the request for review from Copilot July 14, 2026 09:59

Copilot AI commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Merged origin/main into the branch and resolved the two conflicts in azure.ai.connections/internal/cmd/service_target.go and service_target_test.go by accepting origin/main's no-op Deploy implementation (connections are now provisioned at provision time by the microsoft.foundry provider). The connections extension builds and all tests pass. Commit: fix: resolve merge conflicts in azure.ai.connections service target.

@jongio jongio left a comment

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.

Re-reviewed after the merge from main and conflict resolution. My earlier note on env persist-path coverage is addressed: TestUpsertAgentEnvVars now drives UpsertAgentEnvVars and asserts the inline environmentVariables key is dropped. The service-target refactor reads the env values from ServiceConfig.Environment and falls back to the active azd environment when a service has no env, so existing definitions keep resolving. The template collector correctly skips the Foundry double-brace form and the escaped dollar-dollar literal form.

Copilot AI review requested due to automatic review settings July 14, 2026 10:33

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

Copilot reviewed 28 out of 28 changed files in this pull request and generated 8 comments.

Comment on lines +336 to +339
func (p *toolboxServiceTarget) environmentValues(
ctx context.Context,
serviceConfig *azdext.ServiceConfig,
) (map[string]string, error) {
Comment on lines +135 to +136
maps.Copy(legacyEnvironment, environment)
environmentVariables = environmentVariablesFromMap(legacyEnvironment)
Comment on lines +39 to +41
var envReferencePattern = regexp.MustCompile(
`\$\{([A-Za-z_][A-Za-z0-9_]*)\}`,
)
Comment on lines +5 to +7
### Bugs Fixed

- [[#9079]](https://github.com/Azure/azure-dev/pull/9079) Store hosted-agent runtime variables in the standard service-level `env` object and read the expanded values forwarded by azd core. `azd ai agent init` no longer writes the unsupported inline `environmentVariables` shape; legacy inline and on-disk agent definitions remain readable.
Comment on lines +3 to +7
## 1.0.0-beta.3 (Unreleased)

### Bugs Fixed

- [[#9079]](https://github.com/Azure/azure-dev/pull/9079) Resolve routine input `${VAR}` references from the service-level `env` object forwarded by azd core. Existing services without `env` keep falling back to the active azd environment.
Comment on lines +3 to +7
## 1.0.0-beta.3 (Unreleased)

### Bugs Fixed

- [[#9079]](https://github.com/Azure/azure-dev/pull/9079) Resolve toolbox `${VAR}` references from the service-level `env` object forwarded by azd core. Existing services without `env` keep falling back to the active azd environment.
Comment on lines +3 to +7
## 1.0.0-beta.3 (Unreleased)

### Bugs Fixed

- [[#9079]](https://github.com/Azure/azure-dev/pull/9079) Resolve `${VAR}` references from the service-level `env` object forwarded by azd core. Existing services without `env` keep falling back to the active azd environment.
Comment on lines +3 to +7
## 1.0.0-beta.2 (Unreleased)

### Other Changes

- [[#9079]](https://github.com/Azure/azure-dev/pull/9079) Require azd 1.27.1 so bundled Foundry service targets receive service-level environment values.

@jongio jongio left a comment

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.

Re-reviewed the follow-up commit that preserves legacy inline env vars during migration. toContainerAgent now seeds the map from the inline definition and overlays the service environment, so legacy-only keys survive and service values win on conflicts. TestAgentDefinitionFromService_LegacyEnvironment covers the merge (legacy-only key preserved, new service key added, shared key resolves to the service value). Refreshing my approval since the earlier one predated this push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext-agents azure.ai.agents extension ext-connections azure.ai.connections extension ext-foundry azure.ai.{agents,connections,inspector,projects,routines,skills,toolboxes}, microsoft.foundry ext-routines azure.ai.routines extension ext-toolboxes azure.ai.toolboxes extension

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants