2222#include "../inc/AgentRegistry.h"
2323#include "../inc/AgentPolicy.h"
2424#include "../inc/AgentPaneBackend.h"
25+ #include "../inc/AgentSourceUtils.h"
2526#include "../TerminalSettingsAppAdapterLib/TerminalSettings.h"
2627#include "../inc/CustomModelProviderUtils.h"
2728#include "AgentPaneContent.h"
@@ -1599,11 +1600,9 @@ namespace winrt::TerminalApp::implementation
15991600 }
16001601 if (activeCwd.empty())
16011602 {
1602- wchar_t homePath[MAX_PATH];
1603- if (GetEnvironmentVariableW(L"USERPROFILE", homePath, MAX_PATH) > 0)
1604- {
1605- activeCwd = winrt::hstring{ homePath };
1606- }
1603+ activeCwd = winrt::hstring{
1604+ ::Microsoft::Terminal::AgentSource::ReadEnvironmentVariable(L"USERPROFILE")
1605+ };
16071606 }
16081607 if (!activeCwd.empty())
16091608 {
@@ -3114,9 +3113,10 @@ namespace winrt::TerminalApp::implementation
31143113 }
31153114 }
31163115
3117- // Resolve cwd. Priority matches the legacy spawn:
3118- // a) VirtualWorkingDirectory (CLI-remoted commands like `wt agent`)
3119- // b) Active pane CWD of THIS tab (from shell integration / OSC 9;9)
3116+ // Resolve the source-aware ACP cwd and the Win32 helper launch cwd separately:
3117+ // a) Active pane CWD of THIS tab (pre-seeded from its starting directory,
3118+ // then updated by shell integration / OSC 9;9)
3119+ // b) VirtualWorkingDirectory (CLI-remoted commands like `wt agent`)
31203120 // c) Profile's configured starting directory
31213121 // d) User's home directory
31223122 //
@@ -3127,43 +3127,46 @@ namespace winrt::TerminalApp::implementation
31273127 // the helper would start in the wrong directory (autofix and
31283128 // agent context would attribute to the wrong project). Reading
31293129 // directly from `tab` resolves to whichever pane is active on
3130- // this specific tab. If shell integration hasn't reported a cwd
3131- // yet (common for a just-spawned background tab) we fall through
3132- // to (c)/(d) below.
3133- winrt::hstring startingDirectory = _WindowProperties.VirtualWorkingDirectory();
3134- if (startingDirectory.empty())
3135- {
3136- if (const auto activeControl = tab->GetActiveTerminalControl())
3137- {
3138- startingDirectory = activeControl.WorkingDirectory();
3139- }
3140- }
3141- if (startingDirectory.empty())
3142- {
3143- if (sourceProfile)
3144- {
3145- startingDirectory = sourceProfile.EvaluatedStartingDirectory();
3146- }
3147- }
3148- if (startingDirectory.empty())
3130+ // this specific tab. For a WSL agent, that source cwd may be POSIX and
3131+ // must not become the Windows wta-helper process's starting directory.
3132+ // The pane cwd must also win over the window cwd for agent context: deferred
3133+ // pre-warm runs after startup actions restore that property to the
3134+ // launcher directory, which is System32 for an AUMID activation.
3135+ winrt::hstring paneDirectory;
3136+ if (const auto activeControl = tab->GetActiveTerminalControl())
3137+ {
3138+ paneDirectory = activeControl.WorkingDirectory();
3139+ }
3140+ const auto windowDirectory = _WindowProperties.VirtualWorkingDirectory();
3141+ winrt::hstring profileDirectory;
3142+ if (sourceProfile)
31493143 {
3150- wchar_t homePath[MAX_PATH];
3151- if (GetEnvironmentVariableW(L"USERPROFILE", homePath, MAX_PATH) > 0)
3152- {
3153- startingDirectory = winrt::hstring{ homePath };
3154- }
3144+ profileDirectory = sourceProfile.EvaluatedStartingDirectory();
31553145 }
3156- if (effectiveAgentSource == L"wsl" && !startingDirectory.empty())
3146+ const winrt::hstring homeDirectory{
3147+ ::Microsoft::Terminal::AgentSource::ReadEnvironmentVariable(L"USERPROFILE")
3148+ };
3149+ const auto resolvedWorkingDirectories = ::Microsoft::Terminal::AgentSource::ResolveAgentAndHelperWorkingDirectories(
3150+ effectiveAgentSource == L"wsl",
3151+ std::wstring_view{ paneDirectory },
3152+ std::wstring_view{ windowDirectory },
3153+ std::wstring_view{ profileDirectory },
3154+ std::wstring_view{ homeDirectory },
3155+ [](const std::wstring_view candidate) {
3156+ const std::wstring path{ candidate };
3157+ return Utils::IsValidDirectory(path.c_str());
3158+ });
3159+ if (!resolvedWorkingDirectories.agent.empty())
31573160 {
3158- appendHelperFlagValue(L"--agent-source-cwd", startingDirectory );
3161+ appendHelperFlagValue(L"--agent-source-cwd", resolvedWorkingDirectories.agent );
31593162 }
31603163
31613164 NewTerminalArgs args;
31623165 args.Commandline(winrt::hstring{ helperCmd });
31633166 args.Profile(globals.AiCoordinatorProfile());
3164- if (!startingDirectory .empty())
3167+ if (!resolvedWorkingDirectories.helper .empty())
31653168 {
3166- args.StartingDirectory(startingDirectory );
3169+ args.StartingDirectory(winrt::hstring{ resolvedWorkingDirectories.helper } );
31673170 }
31683171
31693172 auto rawPane = _MakeTerminalPane(args, nullptr, nullptr);
0 commit comments