Repository navigation
Description
Describe the bug
On Windows machines where PowerShell runs in ConstrainedLanguage mode (enforced by AppLocker or WDAC, common in managed enterprise environments), every single shell command run by the agent prints a spurious error block.
Copilot CLI appends an exit-status epilogue to each command it runs:
} finally { $__copilotSuccess = $?; if (-not $__copilotSuccess -and $LASTEXITCODE -is [int] -and $LASTEXITCODE -ne 0) { $host.SetShouldExit($LASTEXITCODE) } elseif (-not $__copilotSuccess) { $host.SetShouldExit(1) } else { $host.SetShouldExit(0) } }$host.SetShouldExit() is a .NET method call on a non-core type, which ConstrainedLanguage forbids. The wrapper itself therefore fails and emits:
Cannot invoke the method. Method invocation is supported only on core types in this language mode.
At line:2 char:225
+ ... Success) { $host.SetShouldExit(1) } else { $host.SetShouldExit(0) } }
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
Why this matters
- Affects every command:
Get-Item,python,curl— all of them. - Pollutes the model context. The block is ~150 tokens.
- Risks incorrect failure detection. The text goes to stderr.
Suggested fix
Replace the $host.SetShouldExit(...) calls with the exit keyword, which is fully permitted in ConstrainedLanguage:
} finally { $__copilotSuccess = $?; if (-not $__copilotSuccess -and $LASTEXITCODE -is [int] -and $LASTEXITCODE -ne 0) { exit $LASTEXITCODE } elseif (-not $__copilotSuccess) { exit 1 } else { exit 0 } }Alternatively, guard the call with a language-mode check and fall back to exit.
Affected version
1.0.81-0 (also reproduced on 1.0.82)
Steps to reproduce the behavior
- On a Windows machine where AppLocker/WDAC forces PowerShell into ConstrainedLanguage mode.
Verify with$ExecutionContext.SessionState.LanguageMode, which returnsConstrainedLanguage. - Run any trivial shell command via the agent, e.g.
Get-Item $env:TEMPorpython --version. - Observe the
MethodInvocationNotSupportedInConstrainedLanguageerror block shown above.
Expected behavior
The exit-status wrapper should use constructs permitted in ConstrainedLanguage (e.g. the exit keyword), so no spurious output is appended to successful commands.
Additional context
- OS: Windows 11, domain-joined, managed by corporate GPO
- CPU architecture: x86_64
- Shell: Windows PowerShell 5.1 (
powershell.exe) — also reproduced on PowerShell 7.6.5 (pwsh.exe), so this is not version-specific - Language mode: ConstrainedLanguage, enforced by AppLocker (Exe rule collection,
EnforcementMode=1, pushed via domain GPO) - Host: Copilot CLI running as the VS Code agent host (
@github/copilot-win32-x64)
Verification performed
- Confirmed the offending template string is present in
prebuilds/win32-x64/runtime.node. - Confirmed it is still present in 1.0.82, so updating does not resolve it.
- Confirmed the wrapped commands themselves succeed and the process exit code is still correct (
0on success). The failure is limited to the epilogue, making this primarily output noise plus a stderr false-positive risk. - A user-side
trapcannot suppress it, because the error is raised from the wrapper's ownfinallyblock.
Activity
MilkyWay008 commentedon Sep 1, 2026
The
Cannot invoke the method. Method invocation is supported only on core typeson every command is the wrapper's$host.SetShouldExit()epilogue, ConstrainedLanguage blocks method calls on core types so the wrapped command itself runs fine and only the exit path throws. Yourexit-keyword patch is the right fix. Until it lands, pointing the agent's shell at cmd.exe or bash skips the PowerShell wrapper entirely, that's the practical workaround on AppLocker/WDAC-managed boxes. oh also, btw, I keep a portable Hermes OTG on a USB stick as my rescue agent for these locked-down Windows setups, https://github.com/MilkyWay008/Hermes-OTG if you want to poke at it.Lerri-Cofannos commentedon Sep 3, 2026
I tried the suggested workaround of routing Copilot's shell tool through cmd.exe via VS Code's terminal.integrated.automationProfile.windows. After a window reload and testing in a fresh session, the CLI still spawned powershell.exe and still hit the same SetShouldExit/ConstrainedLanguage error. Shell selection is hardcoded in the CLI's native runtime and doesn't go through VS Code's integrated terminal settings at all, so this setting has no effect on it.
Also checked ~/.copilot/settings.json, the config-dir reference, and the CLI's command-line flags — none expose a way to change which shell the command-execution tool spawns. No environment variable (SHELL, COMSPEC) influences it either.
So there's currently no client-side workaround for machines where ConstrainedLanguage mode is enforced by policy (AppLocker/WDAC) and can't be disabled by the user.
mr-gurke commentedon Sep 24, 2026
Additional reproduction and isolation from the JetBrains integration:
Environment:
LanguageMode: ConstrainedLanguage, enforced by AppLocker/WDACThere are two separate failures.
1. Virtualenv activation prevents the command from running
With PyCharm's Activate virtualenv terminal option enabled, the plugin generates an init script containing:
powershell . 'C:...\ .venv\Scripts\Activate.ps1'
The generated init script runs in
ConstrainedLanguage, while the virtualenv script is loaded from an AppLocker-allowedlocation and is therefore defined in
FullLanguage. PowerShell rejects the dot-sourcing operation with:The wrapper then tries to clean up the error using:
This produces a second:
In this state, the user command never executes, despite the message claiming that the wrapper is continuing with it.
2. Disabling virtualenv activation allows commands to run
After disabling Activate virtualenv and restarting PyCharm, no shell-init scripts are generated. Commands then execute
successfully:
However, every invocation still ends with:
which produces the same ConstrainedLanguage method-invocation error. The remaining issue is therefore limited to the
exit-status epilogue: the command succeeds, but the wrapper reports failure and emits noisy stderr.
This identifies two fixes needed:
The successful tests demonstrate that PowerShell itself and ordinary command execution are not the problem. The failure
is caused by the Copilot-generated wrapper and its handling of ConstrainedLanguage mode.
bdoeven commentedon Nov 25, 2026
+1
ItsTimeToSnuff commentedon Dec 4, 2026
Filed a follow-up report with
1.0.105session telemetry (events.jsonl) in #5679 to clarify that this is not just cosmetic stderr noise with exit code0:$host.SetShouldExit(0)actually forcesexitCode: 1(shell_error_category: "command_nonzero_exit"), marking 100% of succeeding commands as failed (×) in IDE hosts (copilot-intellij, VS Code).ShellOptions.initScripts(1.0.105+),$Error.RemoveAt(0)crashes PowerShell before the user command even starts, causing 5+ wasted LLM retry turns (hundreds of thousands of tokens) per prompt until the agent falls back to a non-PowerShell MCP tool.Included ConstrainedLanguage-safe replacements for both wrapper paths in #5679.