.NET: Python: [Bug]: Persistent PowerShell LocalShellTool returns empty stdout for commands that output objects
维护者通常 1 天内回复
评估
这个 Issue 还没有评估数据。
描述
Description
LocalShellTool in its default mode="persistent" returns an empty stdout for any PowerShell command whose output gets formatted as a table or list. That includes Get-Location / pwd, anything piped through Select-Object, Get-Process, and [pscustomobject]. The command succeeds with exit_code=0, but the agent gets nothing back. Once an object has been written, plain strings that come after it in the same command are lost too. mode="stateless" returns all of this output.
This happens on Windows, where PowerShell is the default shell, and with pwsh on Linux, so it isn't Windows-specific.
Cause: ShellSession._build_script runs the command as & { try { Invoke-Expression $__af_cmd; ... } finally { [Console]::WriteLine('<sentinel>_' + $__af_rc) } } and leaves formatting to the host. The host's Out-Default only flushes table/list formatting when the outer pipeline ends, which is after the script block has returned. By then finally has already written the sentinel straight to [Console]. So the formatted output lands after the sentinel in the stdout buffer, and _run_locked throws it away, since it keeps only the bytes before the sentinel and then clears the buffer. Plain strings survive only because Out-Default writes them immediately.
The .NET ShellSession already handles this. BuildScript in dotnet/src/Microsoft.Agents.AI.Tools.Shell/ShellSession.cs formats the output inside the try, with the comment: "Without this, pwsh defers Out-Default formatting until the script block returns and the sentinel races ahead of the user's output in the byte stream."
Expected: persistent mode returns the same stdout as stateless mode.
Code Sample
import asyncio
import re
import shutil
from agent_framework.tools import LocalShellTool
POWERSHELL = shutil.which("pwsh") or shutil.which("powershell")
SHELLS = {
# the argv LocalShellTool uses by default on Windows
"persistent": [POWERSHELL, "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", "-"],
"stateless": [POWERSHELL],
}
COMMANDS = [
"Get-Location",
"Get-Item $PSHOME | Select-Object Name",
"[pscustomobject]@{ Name = 'an object' }; Write-Output 'a string after it'",
"Write-Output 'just a string'",
]
async def main() -> None:
for mode, shell in SHELLS.items():
async with LocalShellTool(mode=mode, shell=shell) as tool:
for command in COMMANDS:
result = await tool.run(command)
stdout = re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", "", result.stdout).strip() # drop terminal escape codes
print(f"{mode:<10} exit_code={result.exit_code} stdout={stdout!r}")
print(f"{'':<10} ^ {command}")
asyncio.run(main())
Output on main with Linux (Debian 12, PowerShell 7.6.6):
persistent exit_code=0 stdout=''
^ Get-Location
persistent exit_code=0 stdout=''
^ Get-Item $PSHOME | Select-Object Name
persistent exit_code=0 stdout=''
^ [pscustomobject]@{ Name = 'an object' }; Write-Output 'a string after it'
persistent exit_code=0 stdout='just a string'
^ Write-Output 'just a string'
stateless exit_code=0 stdout='Path\n----\n/tmp'
^ Get-Location
stateless exit_code=0 stdout='Name\n----\n7'
^ Get-Item $PSHOME | Select-Object Name
stateless exit_code=0 stdout='Name\n----\nan object\na string after it'
^ [pscustomobject]@{ Name = 'an object' }; Write-Output 'a string after it'
stateless exit_code=0 stdout='just a string'
^ Write-Output 'just a string'
I get the same result on Windows 11 with PowerShell 7.6.6 and with Windows PowerShell 5.1, and the same with the default LocalShellTool() there, without passing shell=.
Error Messages / Stack Traces
None. The empty stdout comes back with exit_code=0.
Package Versions
agent-framework-tools: 1.0.0b261002, agent-framework-core: 1.20.0 (source checkout of main at e9857bd75)
Python Version
Python 3.13
Additional Context
- The persistent PowerShell tests in
python/packages/tools/tests/test_local_shell_tool.pyonly check plain strings and exit codes, so none of them covers output PowerShell formats as a table. The re-anchor test, for example, reads(Get-Location).Path, which returns a string and so doesn't hit this. PlainGet-Locationwould. - Fix: format the output inside the
try, beforefinallywrites the sentinel:Invoke-Expression $__af_cmd | Out-Default;. Unlike a per-objectOut-String, this keeps PowerShell's normal formatting (one table per command). Exit codes for native failures, cmdlet errors,throw, and$LASTEXITCODEreadback stay the same. I'll open a PR with this and a regression test.
- 主要语言
- Python
- 星标
- 13.9k
- 派生
- 2.4k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 443
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/agent-framework 的其他 Issue
-
.NET python triage
难度 1/5 1 小时以内 新手友好度 85/100
microsoft/agent-framework#9092 · 1 条评论 ·
维护者通常 1 天内回复
-
Python: raw-data content mappings lose annotations and attachment metadata可能已有人在做 @moonbox3 于 8 天前认领。 未关闭python triage
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/agent-framework#8632 · 2 条评论 ·
维护者通常 1 天内回复
-
Python: Clarify when to use platform可能已有人在做 @eavanvalkenburg 于 8 天前认领。 未关闭python triage
难度 2/5 1-3 小时 新手友好度 68/100
microsoft/agent-framework#8599 · 1 条评论 ·
维护者通常 1 天内回复
-
.NET Compaction - Update docs to refer to `AIContextProvider` deep dive可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭.NET compaction documentation
难度 1/5 1 小时以内 新手友好度 82/100
microsoft/agent-framework#4629 · 1 条评论 ·
维护者通常 1 天内回复
-
Python: [Bug]: Media type detection documentation examples contain invalid base64可能已有人在做 @eavanvalkenburg 今天认领。 未关闭agents python reproduced
microsoft/agent-framework#9187 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 microsoft/agent-framework 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
SR_SECURITY_DESCRIPTOR.fromString drops the SACL when no DACL is present可能已有人在做 @paul7436 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
equinor/fmu-sumo-uploader#302 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
modelscope/evalscope#1821 ·
维护者通常 1 天内回复
-
Sanity on ansible-core devel fails: ignore-2.23.txt references the removed import-3.9 test可能已有人在做 @yurnov 今天认领。 未关闭needs_triage
难度 1/5 1 小时以内 新手友好度 91/100
ansible-collections/kubernetes.core#1275 ·
维护者通常 1 天内回复