Skip to content

Commit 3ac13b2

Browse files
alliscodeCopilot
and
committed
PR 5604 review fix (group b): add ShellKind.Sh for /bin/sh fallback
Review comment (microsoft#3): when /bin/bash is missing the resolver fell back to /bin/sh but tagged it as ShellKind.Bash, so the launcher passed bash-only flags --noprofile --norc to dash/ash/busybox, which interpret them as positional script names. Fix: * Added ShellKind.Sh for minimal POSIX shells (sh, dash, ash, busybox). * /bin/sh fallback is now tagged Sh. * ClassifyKind maps "SH" / "DASH" / "ASH" / "BUSYBOX" binary names to Sh. * StatelessArgvForCommand emits just `-c <command>` for Sh (no bash-only flags); PersistentArgv emits no flags at all. * LocalShellTool's system-prompt builder describes Sh distinctly and warns the model away from bash-only constructs. Tests: ShellResolverTests covers Sh/Bash classification through the observable argv output (14 new theory cases). Total: 58/58. Co-authored-by: Copilot <[email protected]>
1 parent d7e5707 commit 3ac13b2

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Tools.Shell/LocalShellTool.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ private string BuildDefaultDescription()
497497
ShellKind.PowerShell => "PowerShell (pwsh)",
498498
ShellKind.Cmd => "cmd.exe",
499499
ShellKind.Bash => "bash",
500+
ShellKind.Sh => "POSIX sh (dash/ash)",
500501
_ => "POSIX shell",
501502
};
502503
_ = sb.Append("Shell: ").Append(shellName).Append(" (binary: '").Append(this._shell.Binary).Append("'). ");
@@ -512,9 +513,13 @@ private string BuildDefaultDescription()
512513
_ = sb.Append("`Get-Content` or `cat` (built-in alias works); ");
513514
_ = sb.Append("`Where-Object` / `Select-String` (NOT `grep`). ");
514515
}
515-
else if (this._shell.Kind == ShellKind.Bash)
516+
else if (this._shell.Kind is ShellKind.Bash or ShellKind.Sh)
516517
{
517-
_ = sb.Append("Use POSIX shell syntax (bash). ");
518+
_ = sb.Append("Use POSIX shell syntax. ");
519+
if (this._shell.Kind == ShellKind.Sh)
520+
{
521+
_ = sb.Append("This is a minimal POSIX sh (likely dash/ash) — avoid bash-only features like `[[ ... ]]`, arrays, `<<<` here-strings, or `set -o pipefail`. ");
522+
}
518523
}
519524

520525
if (this._mode == ShellMode.Persistent)

‎dotnet/src/Microsoft.Agents.AI.Tools.Shell/ShellResolver.cs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static ResolvedShell Resolve(string? overrideShell = null)
4848
{
4949
return new ResolvedShell("/bin/bash", ShellKind.Bash);
5050
}
51-
return new ResolvedShell("/bin/sh", ShellKind.Bash);
51+
return new ResolvedShell("/bin/sh", ShellKind.Sh);
5252
}
5353

5454
/// <summary>
@@ -86,6 +86,9 @@ private static ShellKind ClassifyKind(string path)
8686
{
8787
"PWSH" or "POWERSHELL" => ShellKind.PowerShell,
8888
"CMD" => ShellKind.Cmd,
89+
// Non-bash POSIX shells: treat sh/dash/ash/busybox as plain sh
90+
// so we don't pass bash-only flags like --noprofile / --norc.
91+
"SH" or "DASH" or "ASH" or "BUSYBOX" => ShellKind.Sh,
8992
_ => ShellKind.Bash,
9093
};
9194
}
@@ -128,12 +131,14 @@ private static string SystemRoot() =>
128131
/// <summary>Identifies the dialect of the resolved shell.</summary>
129132
internal enum ShellKind
130133
{
131-
/// <summary>POSIX shell (bash, sh, zsh).</summary>
134+
/// <summary>POSIX shell (bash, zsh) that accepts <c>--noprofile</c> / <c>--norc</c>.</summary>
132135
Bash,
133136
/// <summary>PowerShell (pwsh or Windows PowerShell).</summary>
134137
PowerShell,
135138
/// <summary>Windows cmd.exe.</summary>
136139
Cmd,
140+
/// <summary>POSIX <c>sh</c> / dash / ash / busybox — does NOT accept <c>--noprofile</c>.</summary>
141+
Sh,
137142
}
138143

139144
internal readonly record struct ResolvedShell(string Binary, ShellKind Kind, IReadOnlyList<string>? ExtraArgv = null)
@@ -152,6 +157,7 @@ public IReadOnlyList<string> StatelessArgvForCommand(string command)
152157
command,
153158
},
154159
ShellKind.Cmd => new[] { "/d", "/c", command },
160+
ShellKind.Sh => new[] { "-c", command },
155161
_ => new[] { "--noprofile", "--norc", "-c", command },
156162
};
157163
if (extra.Count == 0)
@@ -182,6 +188,7 @@ public IReadOnlyList<string> PersistentArgv()
182188
},
183189
ShellKind.Cmd => throw new NotSupportedException(
184190
"Persistent mode is not supported for cmd.exe — use pwsh, powershell, or a POSIX shell."),
191+
ShellKind.Sh => Array.Empty<string>(),
185192
_ => new[] { "--noprofile", "--norc" },
186193
};
187194
if (extra.Count == 0)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
namespace Microsoft.Agents.AI.Tools.Shell.UnitTests;
4+
5+
/// <summary>
6+
/// Tests for <see cref="ShellResolver.ResolveArgv"/>: bash-only flags like
7+
/// <c>--noprofile</c> / <c>--norc</c> must NOT be passed to dash / ash /
8+
/// busybox, which would treat them as filenames.
9+
/// </summary>
10+
public class ShellResolverTests
11+
{
12+
private static readonly string[] s_shCommandArgv = new[] { "-c", "echo hi" };
13+
private static readonly string[] s_bashCommandArgv = new[] { "--noprofile", "--norc", "-c", "echo hi" };
14+
private static readonly string[] s_bashPersistentArgv = new[] { "--noprofile", "--norc" };
15+
16+
private static ResolvedShell ResolveSingle(string binary) => ShellResolver.ResolveArgv(new[] { binary });
17+
18+
[Theory]
19+
[InlineData("/bin/sh")]
20+
[InlineData("/bin/dash")]
21+
[InlineData("/bin/ash")]
22+
[InlineData("/usr/bin/busybox")]
23+
public void ShVariants_StatelessArgv_OmitBashOnlyFlags(string binary)
24+
{
25+
var argv = ResolveSingle(binary).StatelessArgvForCommand("echo hi");
26+
27+
Assert.Equal(s_shCommandArgv, argv);
28+
Assert.DoesNotContain("--noprofile", argv);
29+
Assert.DoesNotContain("--norc", argv);
30+
}
31+
32+
[Theory]
33+
[InlineData("/bin/sh")]
34+
[InlineData("/bin/dash")]
35+
[InlineData("/bin/ash")]
36+
[InlineData("/usr/bin/busybox")]
37+
public void ShVariants_PersistentArgv_OmitBashOnlyFlags(string binary)
38+
{
39+
var argv = ResolveSingle(binary).PersistentArgv();
40+
41+
Assert.Empty(argv);
42+
}
43+
44+
[Theory]
45+
[InlineData("/bin/bash")]
46+
[InlineData("/usr/local/bin/bash")]
47+
[InlineData("/usr/bin/zsh")]
48+
public void BashVariants_StatelessArgv_IncludeBashFlags(string binary)
49+
{
50+
var argv = ResolveSingle(binary).StatelessArgvForCommand("echo hi");
51+
52+
Assert.Equal(s_bashCommandArgv, argv);
53+
}
54+
55+
[Theory]
56+
[InlineData("/bin/bash")]
57+
[InlineData("/usr/local/bin/bash")]
58+
[InlineData("/usr/bin/zsh")]
59+
public void BashVariants_PersistentArgv_IncludeBashFlags(string binary)
60+
{
61+
var argv = ResolveSingle(binary).PersistentArgv();
62+
63+
Assert.Equal(s_bashPersistentArgv, argv);
64+
}
65+
}

0 commit comments

Comments
 (0)