Skip to content

Fix AsHostedAgent project reference missing in publish mode#17680

Closed
maddymontaquila wants to merge 3 commits into
mainfrom
maddymontaquila/fix-ashosted-agent-publish
Closed

Fix AsHostedAgent project reference missing in publish mode#17680
maddymontaquila wants to merge 3 commits into
mainfrom
maddymontaquila/fix-ashosted-agent-publish

Conversation

@maddymontaquila
Copy link
Copy Markdown
Contributor

Description

Fixes a deploy-time failure when using the new Foundry AsHostedAgent(project) API. The agent runs locally but fails on deploy with ValueError: Azure AI project endpoint is required.

Root cause

In publish mode, AsHostedAgent never injected the Foundry project reference into the target compute resource (the resource actually deployed). Run mode did inject it, but publish mode only attached the reference to the wrapper AzureHostedAgentResource, so the deployed agent's environment lacked the project connection (endpoint / connection string). The deployed agent therefore had no way to reach the project.

Fix

  1. Inject the project reference into the target builder in publish mode so the deployed agent gets the project connection environment variables. This is the core fix for the reported error.

  2. Encode the connection name so the emitted env var key is deploy-safe. Hosted agents validate deployed environment variable names against ^[A-Za-z0-9_]+$ at deploy time. A project name containing - — including the auto-generated default {name}-proj — would otherwise emit ConnectionStrings__{name}-proj and fail deployment.

Both run and publish mode now route through a shared AddProjectReference helper that injects the reference with an encoded connection name, keeping run and deploy environment variables symmetric. Run mode still applies WaitFor(project); publish mode skips it (waiting has no meaning there). The public Foundry WithReference(project) overload is left untouched, so there is no behavior change for other callers. Encoding is a no-op for already-valid (dash-free) names.

Testing

  • Aspire.Hosting.Foundry.Tests: 93/93 pass, including the run-mode dependency (WaitAnnotation) test.
  • Aspire.Hosting.Azure.Tests FoundryExtensionsTests: 22/22 pass, including a new publish-mode test asserting the resolved env var key is ConnectionStrings__my_project (not the dashed form) and that all resolved keys satisfy the deploy-time validation regex.

Checklist

  • Builds and tests pass locally.
  • Added test coverage for the encoded, deploy-safe connection name.

maddymontaquila and others added 2 commits May 29, 2026 14:50
The asHostedAgent API was correctly calling WithReference(project) in run mode
but was missing it in publish mode. This caused environment variables that
reference the Foundry project endpoint to fail resolution during deployment.

The fix adds the WithReference(project) call to the original resource builder
in ConfigurePublishMode so that environment variable resolution during the
deployment phase can find the project reference.

Test: Added AsHostedAgent_InPublishMode_WithProject_AddsProjectReference to
verify the project reference is properly wired in publish mode.

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

Hosted agents validate deployed environment variable names against
^[A-Za-z0-9_]+$ at deploy time. Project names containing '-' (including
the auto-generated default '{name}-proj') would emit
'ConnectionStrings__{name}-proj' and fail deployment.

Route both run and publish mode through a shared AddProjectReference helper
that injects the project reference with an encoded connection name, keeping
run and deploy environment variables symmetric. Run mode still applies
WaitFor(project); publish mode skips it. The public Foundry WithReference
overload is left untouched.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 29, 2026 20:12
@maddymontaquila maddymontaquila added this to the 13.4 milestone May 29, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 29, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 17680

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 17680"

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

Fixes a publish/deploy mismatch in the Foundry AsHostedAgent(project) path where the Foundry project reference (and thus its connection environment variables) wasn’t being injected into the deployed target compute resource in publish mode, leading to missing project endpoint/config at deploy time. The change also ensures connection names are encoded so generated environment variable keys pass hosted-agent deploy-time validation.

Changes:

  • Route both run and publish mode through a shared helper that injects the Foundry project reference onto the compute resource, preserving run/publish parity (while only run mode applies WaitFor(project)).
  • Encode the project connection name before emitting ConnectionStrings__{name}-style variables so keys are deploy-safe (e.g., my-projectmy_project).
  • Add/extend unit tests covering publish-mode env var resolution and the encoded connection-name behavior.
Show a summary per file
File Description
tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs Adds a publish-mode regression test verifying the target resource resolves Foundry project connection env vars.
tests/Aspire.Hosting.Azure.Tests/FoundryExtensionsTests.cs Adds a test asserting encoded connection keys pass deploy validation; refactors repeated provisioning simulation into a helper.
src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs Centralizes project reference injection for run/publish, adds encoding for connection name, and injects the reference in publish mode onto the compute resource.
src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj Links in the shared EnvironmentVariableNameEncoder implementation for use by the Foundry hosting package.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 0

Comment thread src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs Outdated
Replace the bespoke AddProjectReference helper (which re-implemented the
IResourceWithWaitSupport cast + WaitFor) with a shared internal
WithProjectReference helper in ProjectBuilderExtension.cs. The public
WithReference(project) overload now delegates to it, so the cast lives in
exactly one place. AsHostedAgent calls the shared helper with an encoded
connection name in both run and publish mode, preserving deploy-safe env var
names and run/publish symmetry while keeping run-mode WaitFor.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants