Skip to content

Refactor ATS Code Generation and Context Management#13869

Merged
davidfowl merged 10 commits into
mainfrom
davidfowl/poly-apphost-refactor
Jan 14, 2026
Merged

Refactor ATS Code Generation and Context Management#13869
davidfowl merged 10 commits into
mainfrom
davidfowl/poly-apphost-refactor

Conversation

@davidfowl
Copy link
Copy Markdown
Contributor

@davidfowl davidfowl commented Jan 13, 2026

Refactor Polyglot AppHost Infrastructure

Follow up to #13705

Summary

This PR simplifies and consolidates the polyglot app host infrastructure by removing unnecessary abstraction layers and improving the developer experience for TypeScript/Python app hosts.

What Changed

  • Removed Aspire.Hosting.CodeGeneration project - The complex metadata-only reflection system was replaced with direct runtime reflection, significantly reducing code complexity (~5000 lines removed)

  • Improved TypeScript SDK generation - Fixed issues where DTO types were incorrectly treated as handle types, and added cancellation token support

  • Better dev mode experience - The CLI now reads DCP version from the repo when ASPIRE_REPO_ROOT is set, and generated files are properly gitignored

  • Cleaner type system naming - Renamed internal APIs to better reflect the three distinct type categories: Handle Types (passed by reference), DTO Types (serialized as JSON), and Enum Types

Test Plan

  • All existing tests pass
  • TypeScript playground runs successfully

- Removed several classes related to reflection-only parameter and property information (RoParameterInfo, RoPropertyInfo, RoSubstitutedMethod, RoType, WellKnownTypes) to streamline the codebase.
- Introduced AtsContextFactory for creating shared AtsContext from loaded assemblies.
- Added CodeGenerationService to handle JSON-RPC requests for generating SDK code.
- Implemented CodeGeneratorDiscovery to discover and register ICodeGenerator implementations from loaded assemblies.
- Updated JsonRpcServer to include CodeGenerationService for handling code generation requests.
- Enhanced RemoteHostServer to configure services for ATS context and code generation.
- Made several internal classes public to allow for better accessibility in the code generation process.
- Introduced ICodeGenerator interface for generating language-specific SDK code from ATS capabilities.

feat: Remove obsolete project reference from solution file

Implement logging and assembly loading improvements in RemoteHost

- Added a new AssemblyLoader service for on-demand assembly loading with logging.
- Enhanced CapabilityDispatcher to utilize ILogger for better diagnostics.
- Refactored AtsContextFactory to support lazy loading of AtsContext with logging.
- Introduced CodeGeneratorResolver to discover code generators from loaded assemblies.
- Replaced Console.WriteLine with ILogger in various components for consistent logging.
- Created RemoteAppHostService to encapsulate JSON-RPC methods and capabilities.
- Updated JsonRpcServer to use dependency injection for services and improved logging.
- Implemented OrphanDetector as a hosted service to monitor parent process status.
- Removed CodeGeneratorDiscovery in favor of the new CodeGeneratorResolver.
- Improved error handling and logging throughout the codebase.
Copilot AI review requested due to automatic review settings January 13, 2026 05:17
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 13, 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/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 13869

Or

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

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 refactors the ATS (Aspire Type System) code generation architecture by:

  1. Removing the separate Aspire.Hosting.CodeGeneration project and consolidating code generation logic into the RemoteHost
  2. Making several internal ATS classes public to support external code generators (ICodeGenerator, AtsContext, AtsTypeRef, etc.)
  3. Introducing dependency injection for RemoteHost services with proper logging
  4. Changing from in-process to RPC-based code generation where the CLI calls a generateCode JSON-RPC method on the RemoteHost server
  5. Deleting reflection-only type system (RoType, RoMethod, RoParameterInfo, etc.) in favor of runtime reflection via the shared RuntimeAssemblyInfo
  6. Converting to hosted services pattern with BackgroundService and IHostApplicationLifetime

Changes:

  • Removed ~3000 lines of reflection-only code and replaced with runtime reflection via shared ATS scanning infrastructure
  • Introduced 5 new services in RemoteHost: AssemblyLoader, AtsContextFactory, CodeGeneratorResolver, CodeGenerationService, RemoteAppHostService
  • Changed CLI to connect via RPC to generate code instead of generating locally
  • Made AtsTypeScriptCodeGenerator public and added it as a dependency to AppHostServerProject
  • Added proper logging throughout with ILogger instead of Console.WriteLine

Reviewed changes

Copilot reviewed 52 out of 52 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Aspire.Hosting/Ats/*.cs Made internal ATS classes public for external code generator consumption
src/Aspire.Hosting/Aspire.Hosting.csproj Added InternalsVisibleTo for TypeScript code generator
src/Aspire.Hosting.RemoteHost/RemoteHostServer.cs Refactored to use DI with hosted services pattern
src/Aspire.Hosting.RemoteHost/RemoteAppHostService.cs New service encapsulating JSON-RPC methods
src/Aspire.Hosting.RemoteHost/OrphanDetector.cs Converted to BackgroundService with ILogger
src/Aspire.Hosting.RemoteHost/JsonRpcServer.cs Converted to BackgroundService with DI
src/Aspire.Hosting.RemoteHost/CodeGeneration/*.cs New services for code generation via RPC
src/Aspire.Hosting.RemoteHost/AssemblyLoader.cs New service for lazy assembly loading
src/Aspire.Hosting.CodeGeneration/**/*.cs Deleted entire project (~3000 lines)
src/Aspire.Hosting.CodeGeneration.TypeScript/AtsTypeScriptCodeGenerator.cs Made public, added disconnect handler
src/Aspire.Hosting.CodeGeneration.TypeScript/*.csproj Changed to reference Aspire.Hosting instead of CodeGeneration
src/Aspire.Cli/Projects/TypeScriptAppHostProject.cs Refactored to use RPC for code generation
src/Aspire.Cli/Projects/AppHostServerProject.cs Added TypeScript code generator to dependencies
src/Aspire.Cli/Aspire.Cli.csproj Removed direct code generation project references
playground/TypeScriptAppHost/.modules/aspire.ts Generated code with API changes
Aspire.slnx Removed CodeGeneration project from solution

…tionality

- Introduced cancellation token registry to manage cancellation requests.
- Added `registerCancellation` and `unregisterCancellation` functions for handling AbortSignal.
- Enhanced `AspireClient` with a `cancelToken` method to send cancellation requests.
- Updated tests to verify cancellation token functionality and integration with existing capabilities.
- Refactored related classes and methods to accommodate cancellation token parameters.
- Removed the IAtsReflection interfaces and their implementations (RuntimeReflectionWrappers.cs) to streamline the ATS scanning process.
- Updated AtsTypeScriptCodeGeneratorTests to reflect changes in method naming conventions, replacing MethodName with QualifiedMethodName.
- Simplified capability scanning in tests by removing unnecessary wrapping of assemblies with RuntimeAssemblyInfo.
- Enhanced test snapshots to include ClrType for parameters and return types for better type mapping.
- Adjusted AtsCapabilityScannerTests to directly use Type instead of RuntimeTypeInfo for type ID mapping, improving test clarity and performance.
- Updated multiple test files to replace direct HandleRegistry usage with AtsMarshaller for better context management.
- Introduced CreateTestMarshaller and CreateTestMarshallerWithCallbacks methods to streamline marshaller creation in tests.
- Removed AtsIntrinsicsTests.cs as it was deemed unnecessary.
- Enhanced existing tests to ensure compatibility with new marshaller structure and improved error handling.
- Added IsResourceBuilder property to various DTOs in snapshots to reflect updated capabilities.
- Updated TypeScript snapshots to include new properties: IsDistributedApplicationBuilder and IsDistributedApplication.
- Modified AtsContext initialization in multiple test files to replace TypeInfos with HandleTypes for consistency across tests.
@davidfowl davidfowl requested a review from mitchdenny as a code owner January 13, 2026 18:30
…he TypeScriptAppHost dist directory to streamline the project structure and eliminate unused code.
… IInteractionService directly, simplifying dependency injection in tests
@davidfowl davidfowl merged commit d107f5a into main Jan 14, 2026
296 checks passed
@davidfowl davidfowl deleted the davidfowl/poly-apphost-refactor branch January 14, 2026 00:58
@dotnet-policy-service dotnet-policy-service Bot added this to the 13.2 milestone Jan 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants