Skip to content

fix: inherit active pane profile in agent-created terminals to match … - #366

Merged
Kai Tao (vanzue) merged 6 commits into
microsoft:mainfrom
adityabagchi24:main
Jul 1, 2026
Merged

Kai Tao (vanzue) merged 6 commits into
microsoft:mainfrom
adityabagchi24:main

Conversation

@adityabagchi24

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

When the AI agent launches a new terminal, it now matches the active session's profile (e.g. WSL/Ubuntu) instead of falling back to the user's default profile (e.g. PowerShell).

References and Relevant Issues

Closes #351

Detailed Description of the Pull Request / Additional comments

The agent's create_terminal command was creating new tabs without specifying a profile, so Windows Terminal would fall back to the default profile. This meant an agent running in an Ubuntu session would spawn new terminals in PowerShell.

The fix pipes the active pane's profile name through the entire chain: Rust ShellManager → CliChannel → wtcli → COM CreateTab. Now when the agent creates a terminal, the new tab uses the same profile as the currently active pane.

Validation Steps Performed

Traced the full data flow end-to-end: the profile name is fetched from the active pane, passed through to the COM CreateTab call, and resolved to the correct profile by Windows Terminal's settings engine.

PR Checklist

…WSL/Ubuntu session instead of default profile
Copilot AI review requested due to automatic review settings June 28, 2026 13:06
@adityabagchi24

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

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

This PR fixes agent-created terminals defaulting to the user’s default profile by propagating the effective active pane’s profile name through the WTA Rust → wtcli → COM CreateTab pipeline, so a new tab created while working in (for example) a WSL/Ubuntu profile launches as the same profile.

Changes:

  • WTA ShellManager queries get_active_pane and forwards its profile value into the create_tab request payload.
  • WTA CliChannel forwards profile to wtcli new-tab via a new -p/--profile argument.
  • wtcli new-tab adds -p/--profile and passes it through to the COM server CreateTab call.

Reviewed changes

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

File Description
tools/wta/src/shell/wt_channel/cli_channel.rs Forwards optional profile parameter to wtcli new-tab.
tools/wta/src/shell/shell_manager.rs Reads active pane profile and includes it in create_tab parameters.
src/tools/wtcli/main.cpp Adds --profile to new-tab and passes it to CreateTab.

@vanzue

Copy link
Copy Markdown
Contributor

Nice work, thanks for picking this up! One gap before it fully closes #351:

This covers the ACP create_terminal path, but in practice the integrated agents open new tabs through the coordinator path instead — open / open_and_send in coordinator.rs call wt_create_tab (shell_manager.rs:475), which has no profile parameter and so still falls back to the default profile (PowerShell). I verified this live: prompting copilot/claude to open a tab always hits coordinator: open_and_send create_tab, never create_terminal.

Could you also thread the active pane's profile through wt_create_tab / wt_split_pane and pass it from the open / open_and_send handlers? The -p/--profile plumbing you already added to wtcli new-tab can be reused there. That should make the fix complete. Happy to help if useful!

@adityabagchi24

Copy link
Copy Markdown
Contributor Author

Kai Tao (@vanzue) Good catch — you're right, the agents use the coordinator path, not the ACP one. I've now fixed both open and open_and_send to automatically pick up the active pane's profile (e.g. "Ubuntu") and pass it through when creating new tabs or panels. The -p/--profile flag I already added to wtcli new-tab is now also wired into split-pane, and all the Rust plumbing connects them together. So whether the agent opens a tab or splits a panel, the new terminal will match the current shell session instead of falling back to the default profile. Please check now.

The profile field is destructured by reference (&Option<String>), so
the Some(p) if !p.is_empty() => Some(p) arm produced Option<&String>
while the fallback arm produced owned Option<String>, failing to
compile. Clone in the first arm so both arms are Option<String>.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings July 1, 2026 02:44

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 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread tools/wta/src/coordinator.rs Outdated
Comment on lines +421 to +429
// Resolve profile: prefer explicit from LLM, fall back to active pane's profile
let profile = match profile {
Some(p) if !p.is_empty() => Some(p.clone()),
_ => shell_mgr
.wt_get_active_pane()
.await
.ok()
.and_then(|v| v.get("profile").and_then(|v| v.as_str()).map(|s| s.to_owned())),
};
Comment thread tools/wta/src/coordinator.rs Outdated
Comment on lines +529 to +537
// Resolve profile: prefer explicit from LLM, fall back to active pane's profile
let profile = match profile {
Some(p) if !p.is_empty() => Some(p.clone()),
_ => shell_mgr
.wt_get_active_pane()
.await
.ok()
.and_then(|v| v.get("profile").and_then(|v| v.as_str()).map(|s| s.to_owned())),
};
Kai Tao (vanzue) and others added 2 commits July 1, 2026 10:52
Extract the profile-resolution logic from execute_choice into a pure
resolve_agent_profile() helper and unit-test it, replacing three
duplicated inline copies (two in coordinator.rs, one in
ShellManager::create_terminal_wt). The helper now also filters an empty
active-pane profile to None so all three paths behave consistently and
downstream lets Windows Terminal pick the default.

Tests:
- resolve_agent_profile: explicit-over-active, fallback, empty-explicit,
  empty/missing/non-string active-pane profile, both-unavailable.
- serde: Open/OpenAndSend parse an explicit profile and default to None
  when the field is absent (pre-microsoft#366 schema compatibility).

Co-authored-by: Copilot <[email protected]>
When the active pane is a WSL profile, its reported working directory is a
POSIX path (e.g. /home/user). The delegate flow (?<prompt> / Alt+Shift+/)
launches a Windows agent CLI (Copilot/Claude/Gemini) and passed that POSIX
cwd straight through, which is not a valid working directory for a Windows
process — the CLI failed to launch.

Add a pure, unit-tested sanitize_windows_agent_cwd() helper: a bare POSIX
absolute path falls back to %USERPROFILE% (or is dropped when unavailable),
while Windows drive-letter paths, UNC paths (//wsl.localhost/..., \\\\...),
and relative paths pass through unchanged. Apply it in delegate_with_context
before creating the tab.

Running an agent natively inside WSL (so it honors the Linux cwd and uses
the WSL toolchain) remains a separate future feature; this is only a guard
so the Windows agent CLI doesn't crash on an unusable cwd.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings July 1, 2026 05:14

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 6 out of 6 changed files in this pull request and generated no new comments.

Comment thread tools/wta/src/coordinator.rs Fixed
@github-actions

This comment has been minimized.

@vanzue

Kai Tao (vanzue) commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed end-to-end — approach is sound. Pushed 3 commits:

  1. Compile fix (blocker): the profile match arms had mismatched types (Option<&String> vs Option<String>) → E0308, crate didn't build. cargo build compiles the wta crate that F5/bcz skips, so CI would've caught it. Fixed by cloning.
  2. Tests + refactor: extracted resolve_agent_profile() (dedupes 3 copies), added unit tests incl. empty/missing-profile fallback and serde parsing of the new field. Also made empty active-pane profile resolve to None, matching the C++ !profileH.empty() guard.
  3. WSL cwd guard: testing the delegate path (?<prompt> / Alt+Shift+/) from a WSL pane, the Windows agent CLI failed to launch — the pane's POSIX cwd (/home/user) isn't a valid Windows working dir. Added a unit-tested sanitize_windows_agent_cwd() (POSIX → %USERPROFILE%; Windows/UNC/relative pass through).

Verified: profile inheritance works for shell opens (#351 goal); no agent-pane leak (GetProtocolActivePane uses the source shell pane); harmless for exe-launching tabs (commandline replaces the profile shell).

Known limitation: for a Windows agent CLI launched from a WSL pane, inheriting the WSL profile/cwd can't truly "run inside WSL" — the proper fix is running the CLI natively via wsl -e, a separate future change.

Thanks a lot for the contribution, Aditya Bagchi (@adityabagchi24)! Really appreciate you tackling #351, and looking forward to more contributions from you. 🙏

check-spelling's line_forbidden.patterns rejects ', otherwise' (wants
'; otherwise' or '. Otherwise'). Adjust the sanitize_windows_agent_cwd
doc comment accordingly.

Co-authored-by: Copilot <[email protected]>
@vanzue
Kai Tao (vanzue) merged commit cabe09a into microsoft:main Jul 1, 2026
6 checks passed
@adityabagchi24

Aditya Bagchi (adityabagchi24) commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Kai Tao (@vanzue) Thank you so much for the thorough review and for taking the time to improve the implementation. I really appreciate you fixing the compile issue, refactoring the duplicated logic, adding comprehensive tests, and addressing the WSL working directory edge case. The explanations were very helpful, especially regarding the current limitation with WSL and the rationale for leaving the native wsl -e approach as a future enhancement.
I'm glad the overall approach aligned with the goal of #351 . Thank you again for your guidance throughout the review process and for merging the contribution. I truly appreciate it, and I'm looking forward to contributing more to the project in the future🙏

Kai Tao (vanzue) added a commit that referenced this pull request Jul 3, 2026
Second review pass over the release checklist against the v0.1.2 release notes:

- Added a [new] coverage marker to the legend and tagged all 12 genuinely-new v0.1.2 test cases with it (the two reworded existing cases — F5 refresh, ACP-sourced historical — are corrections, not new cases, so they stay untagged).
- Added 5 more previously-missing cases surfaced by a closer read:
  - FRE execution-policy detection is correct / no false-block on probe timeout (#336/#338/#309)
  - wta-master death is a consistent degraded state requiring /restart, no split-brain (#329)
  - Session titles are clean (no bare '# AGENTS.md instructions' heading) (#355)
  - Focus brings the target window to the foreground (#353)
  - Agent-created terminals inherit the active pane profile (#366, closes #351)

Co-authored-by: Copilot <[email protected]>
Kai Tao (vanzue) added a commit that referenced this pull request Jul 3, 2026
* docs(release-check-list): sync with v0.1.2 features

Reviewed the release checklist against the v0.1.2 release notes and fixed two outdated statements plus added seven missing test cases for new features.

Outdated statements corrected:
- Session view refresh: now F5 on-demand re-scan (incl. WSL distros started after launch), not gated on hooks (#344).
- Historical state: history is now sourced from ACP session/list, not on-disk file parsing (#365).

Missing test cases added:
- Alt+V clipboard image paste into agent chat (#354)
- GitHub Enterprise Copilot sign-in (#362)
- Bash/WSL shell integration incl. set -u safety (#340)
- Shells self-report identity via OSC 9001;ShellType + nested-shell autofix targeting (#345)
- Environment-aware answers/fixes: investigate PATH before answering/autofixing (#306)
- WSL distro sessions visible and resumable in the correct distro (#323)
- Agent panes not persisted into saved window layout (#360/#275)

Co-authored-by: Copilot <[email protected]>

* docs(release-check-list): tag new v0.1.2 cases with [new] + add 5 more

Second review pass over the release checklist against the v0.1.2 release notes:

- Added a [new] coverage marker to the legend and tagged all 12 genuinely-new v0.1.2 test cases with it (the two reworded existing cases — F5 refresh, ACP-sourced historical — are corrections, not new cases, so they stay untagged).
- Added 5 more previously-missing cases surfaced by a closer read:
  - FRE execution-policy detection is correct / no false-block on probe timeout (#336/#338/#309)
  - wta-master death is a consistent degraded state requiring /restart, no split-brain (#329)
  - Session titles are clean (no bare '# AGENTS.md instructions' heading) (#355)
  - Focus brings the target window to the foreground (#353)
  - Agent-created terminals inherit the active pane profile (#366, closes #351)

Co-authored-by: Copilot <[email protected]>

* test(e2e): add master-death degraded-state integration test (#329); drop rolled-back WSL session checklist item

Adds Feature.AgentMasterDeath.Tests.ps1: kills this app's wta-master out from under a live helper and asserts the full #329 contract end-to-end — the pane leaves Connected, the / popup is filtered to only /restart, NO master is silently respawned while degraded (anti-split-brain), and /restart brings up exactly one fresh master and reconnects. Verified passing live against the deployed dev package.

Also removes the '[new] WSL distro sessions are visible and resumable' checklist item (#323) since WSL in-distro session support is being rolled back for v0.1.2, and drops the WSL example from the F5 refresh item to match.

Co-authored-by: Copilot <[email protected]>

* Address Copilot review: tighten master-count assertion and clarify Alt+V behavior

- Feature.AgentMasterDeath.Tests.ps1: assert exactly one wta-master while connected (-Be 1, was -BeGreaterThan 0) so the connected-state gate itself catches a split-brain regression. Re-verified passing live.

- release-check-list.md: correct the Alt+V item — Alt+V queues the image until the next prompt, and an unsupported agent / empty clipboard surfaces a clear system message rather than being a silent no-op.

The [WSL-<distro>] raw-angle-bracket comment is already resolved: that checklist item was removed with the WSL session rollback.

Co-authored-by: Copilot <[email protected]>

* Address Copilot review round 2: block /stop in degraded popup test, release-agnostic [new] marker, spelling fix

- Feature.AgentMasterDeath.Tests.ps1: add /stop to the blocked-slash-command list so a regression that offers /stop while transport-lost is caught (it's a real CommandKind::Stop). Re-verified passing live.

- release-check-list.md: reword the [new] marker in release-agnostic terms and say when to clear it (was hard-coded to v0.1.2); fix forbidden spelling non-existent -> nonexistent (check-spelling CI).

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
Yuandi (DDKinger) added a commit that referenced this pull request Jul 9, 2026
…ng shut

Delegating (?<prompt>) to a non-existent / misconfigured agent opened a tab
that flashed shut before the user could read the error. The previous
workaround opened a *separate* canned-message `cmd /k` tab; this restores the
original behavior of keeping the real error visible in the tab that ran the
command, and removes the second tab.

Root cause: a protocol `create_tab` that carries a commandline but no profile
resolves (via CascadiaSettings::GetProfileForArgs) to the "Defaults" profile,
whose pane is auto-closed on *any* process exit -- even a non-zero one. So the
doomed `cmd /c <agent>` exited (code 1) and WT closed the pane before the
"'<agent>' is not recognized" error was readable. Verified live: a no-profile
`wtcli new-tab -c "cmd /c exit 1"` flashed shut, while the same command with an
explicit profile stayed open.

Fix (two parts):

- C++ (TerminalPage.Protocol.cpp): CreateProtocolTab now pins a real profile
  when the caller specifies none -- the profile of the pane the user is
  currently working in (so a delegate/agent tab opened from e.g. a WSL/Ubuntu
  session matches it, same intent as PR #366), falling back to the user's
  global default profile when there is no focused terminal. Its closeOnExit
  (automatic/graceful) keeps a non-zero exit visible, exactly like a normally-
  opened tab. This is never left empty -- that's what re-introduces the auto-
  closing "Defaults" profile. This fixes every no-profile protocol tab, not
  just the delegate. An explicitly-requested profile is left untouched; if the
  resolved GUID somehow can't be matched, GetProfileForArgs falls back to the
  same "Defaults" profile as before (no regression).

- WTA (delegate_with_context): a non-launchable delegate agent is now launched
  as a bare `cmd /c <agent>` (no baked prompt) so it fails cleanly with a
  non-zero exit. This keeps the doomed launch out of the fragile prompt-baking
  path, where a stray `"`/`&` in the pane context could unbalance cmd's quoting
  and make cmd exit 0 (which the profile would then close). Replaces the
  separate `cmd /k` error tab.

Co-authored-by: Copilot <[email protected]>
Yuandi (DDKinger) added a commit that referenced this pull request Jul 9, 2026
…ng shut (#390)

* fix: keep a misconfigured delegate agent's tab open instead of flashing shut

Delegating (?<prompt>) to a non-existent / misconfigured agent opened a tab
that flashed shut before the user could read the error. The previous
workaround opened a *separate* canned-message `cmd /k` tab; this restores the
original behavior of keeping the real error visible in the tab that ran the
command, and removes the second tab.

Root cause: a protocol `create_tab` that carries a commandline but no profile
resolves (via CascadiaSettings::GetProfileForArgs) to the "Defaults" profile,
whose pane is auto-closed on *any* process exit -- even a non-zero one. So the
doomed `cmd /c <agent>` exited (code 1) and WT closed the pane before the
"'<agent>' is not recognized" error was readable. Verified live: a no-profile
`wtcli new-tab -c "cmd /c exit 1"` flashed shut, while the same command with an
explicit profile stayed open.

Fix (two parts):

- C++ (TerminalPage.Protocol.cpp): CreateProtocolTab now pins a real profile
  when the caller specifies none -- the profile of the pane the user is
  currently working in (so a delegate/agent tab opened from e.g. a WSL/Ubuntu
  session matches it, same intent as PR #366), falling back to the user's
  global default profile when there is no focused terminal. Its closeOnExit
  (automatic/graceful) keeps a non-zero exit visible, exactly like a normally-
  opened tab. This is never left empty -- that's what re-introduces the auto-
  closing "Defaults" profile. This fixes every no-profile protocol tab, not
  just the delegate. An explicitly-requested profile is left untouched; if the
  resolved GUID somehow can't be matched, GetProfileForArgs falls back to the
  same "Defaults" profile as before (no regression).

- WTA (delegate_with_context): a non-launchable delegate agent is now launched
  as a bare `cmd /c <agent>` (no baked prompt) so it fails cleanly with a
  non-zero exit. This keeps the doomed launch out of the fragile prompt-baking
  path, where a stray `"`/`&` in the pane context could unbalance cmd's quoting
  and make cmd exit 0 (which the profile would then close). Replaces the
  separate `cmd /k` error tab.

Co-authored-by: Copilot <[email protected]>

* address PR review: scope the profile pin, redact delegate log, fix spelling

- TerminalPage.Protocol.cpp: only pin a profile for the case that actually
  hits the bug -- a commandline with no explicit profile AND no ProfileIndex.
  A caller that omits the commandline already lands on the user's real default
  profile (not the auto-closing "Defaults"), and a profile-by-index request
  must be honored, so neither is overridden now.
- main.rs: the "not launchable" warn logs only the executable (first token),
  not the full delegate commandline, which could embed a custom agent's
  tokens/credentials. The full commandline stays trace-only.
- coordinator.rs: generalize the delegate_command_launchable doc -- the bare
  launch is only shell-wrapped when needs_shell_launch is true (a direct .exe
  miss fails to start instead), so the comment no longer hard-codes `cmd /c`.
- main.rs: spelling -- "non-existent" -> "nonexistent" (check-spelling
  forbidden pattern).

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
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.

Agent-launched new terminal should match the active WSL/Ubuntu session, not the default profile

4 participants