Skip to content

.NET: Dotnet: reset $LASTEXITCODE per command in persistent PowerShell sessions - #8259

Merged
westey (westey-m) merged 2 commits into
microsoft:mainfrom
westey-m:dotnet-pwsh-lastexitcode
Sep 10, 2026
Merged

.NET: Dotnet: reset $LASTEXITCODE per command in persistent PowerShell sessions#8259
westey (westey-m) merged 2 commits into
microsoft:mainfrom
westey-m:dotnet-pwsh-lastexitcode

Conversation

@westey-m

Copy link
Copy Markdown
Contributor

Motivation & Context

Dotnet equivalent of #8206

Description & Review Guide

  • What are the major changes?
  • What is the impact of these changes?
  • What do you want reviewers to focus on?

Related Issue

Fixes #

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Copilot AI balanced review requested due to automatic review settings September 10, 2026 16:25
@agent-framework-automation agent-framework-automation Bot added the .NET Usage: [Issues, PRs], Target: .Net label Sep 10, 2026
@github-actions github-actions Bot changed the title Dotnet: reset $LASTEXITCODE per command in persistent PowerShell sessions .NET: Dotnet: reset $LASTEXITCODE per command in persistent PowerShell sessions Sep 10, 2026

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.

🟡 Changes recommended

Consecutive native commands returning the same nonzero code cause the second command to report success.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Updates persistent PowerShell exit-code handling to avoid stale results across commands.

Changes:

  • Tracks prior $LASTEXITCODE and captures $? before flushing.
  • Adds a Windows regression test for cmdlet-only commands.
File summaries
File Description
ShellSession.cs Revises PowerShell exit-code detection.
LocalShellExecutorTests.cs Adds persistent-session coverage.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread dotnet/src/Microsoft.Agents.AI.Tools.Shell/ShellSession.cs Outdated

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): 8cf7359c7cb3
Model: gpt-5.6-sol-fast

Overview

The PR snapshots $LASTEXITCODE, captures $? before flushing, and adds a regression test that preserves stale-code readback while preventing a later cmdlet from inheriting it. Command serialization, PowerShell-only branching, exception handling, and sentinel parsing constrain the change well. However, the value-comparison heuristic cannot distinguish stale state from a new native command returning the same nonzero code, so repeated failures can be reported as success.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 high) across 1 file. Details are attached to the affected lines below.

Affected areas: dotnet/src/Microsoft.Agents.AI.Tools.Shell/ShellSession.cs

Comment thread dotnet/src/Microsoft.Agents.AI.Tools.Shell/ShellSession.cs Outdated
Clear the persistent automatic variable before invoking each command so repeated native failures with the same exit code are reported correctly. Update the Windows regression test to cover repeated failures and reset readback semantics.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@westey-m
westey (westey-m) added this pull request to the merge queue Sep 10, 2026
Merged via the queue into microsoft:main with commit c37de51 Sep 10, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants