Skip to content

Commit 32a56f3

Browse files
PierrunoYTclaude
andauthored
[Feat] Tell the model which shell syntax execute_command uses (cmd.exe / PowerShell) (#2115)
* feat(prompt): state the command syntax for cmd.exe and PowerShell The system prompt only named the shell path, so rules and habits written for bash silently ran with cmd semantics on Windows. Add a Shell Syntax line for cmd.exe and PowerShell (when execute_command is available) covering command chaining, variables and exit codes. Refs #2083 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(prompt): correct PowerShell status guidance and assert dialect syntax Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(prompt): qualify cmd.exe exit-code guidance and cover bare shell names Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * test(prompt): assert cmd.exe ERRORLEVEL expansion and same-line success guidance --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 75a93a9 commit 32a56f3

4 files changed

Lines changed: 116 additions & 1 deletion

File tree

‎src/core/prompts/sections/__tests__/system-info.spec.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ vi.mock("../../../../utils/shell", () => ({
1010
}))
1111

1212
import { getSystemInfoSection } from "../system-info"
13+
import { getShell } from "../../../../utils/shell"
1314
import osName from "os-name"
1415

1516
const mockOsName = vi.mocked(osName)
@@ -137,4 +138,27 @@ describe("getSystemInfoSection", () => {
137138
// sentinel string) breaks this exact join.
138139
expect(result).toContain("default directory for all tool operations. When the user initially gives you a task")
139140
})
141+
142+
describe("shell syntax", () => {
143+
const cmdPath = "C:\\Windows\\System32\\cmd.exe"
144+
145+
it("states the dialect for cmd.exe when execute_command is available", () => {
146+
vi.mocked(getShell).mockReturnValueOnce(cmdPath)
147+
148+
const result = getSystemInfoSection(mockCwd, policyFor())
149+
150+
expect(result).toContain(`Default Shell: ${cmdPath}
151+
Shell Syntax: Commands run through Windows cmd.exe`)
152+
})
153+
154+
it("omits the dialect without execute_command", () => {
155+
vi.mocked(getShell).mockReturnValueOnce(cmdPath)
156+
157+
expect(getSystemInfoSection(mockCwd, policyFor(false))).not.toContain("Shell Syntax")
158+
})
159+
160+
it("omits the dialect for POSIX shells", () => {
161+
expect(getSystemInfoSection(mockCwd, policyFor())).not.toContain("Shell Syntax")
162+
})
163+
})
140164
})

‎src/core/prompts/sections/system-info.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import os from "os"
22
import osName from "os-name"
33

44
import { getShell } from "../../../utils/shell"
5+
import { describeShellDialect } from "../../../utils/shellDialect"
56

67
import type { EffectiveToolPolicy } from "../tools/effective-tool-policy"
78

@@ -46,12 +47,19 @@ export function getSystemInfoSection(cwd: string, policy: EffectiveToolPolicy):
4647
? " New terminals will be created in the current workspace directory, however if you change directories in a terminal it will then have a different working directory; changing directories in a terminal does not modify the workspace directory, because you do not have access to change the workspace directory."
4748
: ""
4849

50+
const shell = getShell()
51+
const shellDialect = executeCommandAvailable ? describeShellDialect(shell) : undefined
52+
const shellDialectLine = shellDialect
53+
? `
54+
Shell Syntax: ${shellDialect}`
55+
: ""
56+
4957
const details = `====
5058
5159
SYSTEM INFORMATION
5260
5361
Operating System: ${osInfo}
54-
Default Shell: ${getShell()}
62+
Default Shell: ${shell}${shellDialectLine}
5563
Home Directory: ${os.homedir().toPosix()}
5664
Current Workspace Directory: ${cwd.toPosix()}
5765
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describeShellDialect } from "../shellDialect"
2+
3+
describe("describeShellDialect", () => {
4+
const cmdSyntax = ["`&`", "`&&`", "`%VAR%`", "`set X=Y`", "`%ERRORLEVEL%`", "`!ERRORLEVEL!`"]
5+
const powershellSyntax = ["`;`", "`$env:VAR`", "`$?`", "`$LASTEXITCODE`"]
6+
7+
it.each([
8+
{ shell: "C:\\Windows\\System32\\cmd.exe", expected: "cmd.exe", syntax: cmdSyntax },
9+
{ shell: "C:\\Windows\\System32\\CMD.EXE", expected: "cmd.exe", syntax: cmdSyntax },
10+
{ shell: "cmd", expected: "cmd.exe", syntax: cmdSyntax },
11+
{ shell: "C:\\Program Files\\PowerShell\\7\\pwsh.exe", expected: "PowerShell", syntax: powershellSyntax },
12+
{
13+
shell: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
14+
expected: "PowerShell",
15+
syntax: powershellSyntax,
16+
},
17+
{ shell: "powershell", expected: "PowerShell", syntax: powershellSyntax },
18+
{ shell: "pwsh", expected: "PowerShell", syntax: powershellSyntax },
19+
])("names the dialect and its syntax for $shell", ({ shell, expected, syntax }) => {
20+
const description = describeShellDialect(shell)
21+
22+
expect(description).toContain(expected)
23+
for (const token of syntax) {
24+
expect(description).toContain(token)
25+
}
26+
})
27+
28+
it("tells PowerShell users that a failed cmdlet does not change $LASTEXITCODE", () => {
29+
expect(describeShellDialect("pwsh.exe")).toContain("does not change when a cmdlet fails")
30+
})
31+
32+
it("warns that ';' does not chain commands in cmd.exe", () => {
33+
expect(describeShellDialect("C:\\Windows\\System32\\cmd.exe")).toContain("`;` is not a separator")
34+
})
35+
36+
it("tells cmd.exe users that %ERRORLEVEL% expands before a chained line runs", () => {
37+
const description = describeShellDialect("C:\\Windows\\System32\\cmd.exe")
38+
39+
expect(description).toContain("`%ERRORLEVEL%` expands before a chained line runs")
40+
expect(description).toContain("use `&&` or `||` for same-line success checks")
41+
expect(description).toContain("enable delayed expansion and use `!ERRORLEVEL!`")
42+
})
43+
44+
it.each(["/bin/bash", "/bin/zsh", "C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Windows\\System32\\wsl.exe"])(
45+
"adds nothing for POSIX shell %s",
46+
(shell) => {
47+
expect(describeShellDialect(shell)).toBeUndefined()
48+
},
49+
)
50+
})

‎src/utils/shellDialect.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Describes the command syntax of Windows-native shells for the system prompt.
3+
*
4+
* Rules and habits written for bash silently do the wrong thing under these shells
5+
* (e.g. `;` is not a command separator in cmd.exe), so the model is told which
6+
* dialect it is writing for. Returns undefined for POSIX shells, where the shell
7+
* path already names the dialect.
8+
*/
9+
export function describeShellDialect(shellPath: string): string | undefined {
10+
// path.basename follows the host platform; split on both separators so Windows paths work anywhere.
11+
const name = (shellPath.split(/[\\/]/).pop() ?? "").toLowerCase()
12+
13+
if (name === "cmd.exe" || name === "cmd") {
14+
return (
15+
"Commands run through Windows cmd.exe, not bash. Use cmd syntax: `&` or `&&` to chain commands " +
16+
"(`;` is not a separator), `%VAR%` for variables, `set X=Y`, and `%ERRORLEVEL%` for exit codes " +
17+
"(`%ERRORLEVEL%` expands before a chained line runs, so use `&&` or `||` for same-line success checks; " +
18+
"enable delayed expansion and use `!ERRORLEVEL!` when the numeric result is needed after a command), " +
19+
"and `dir`, `type`, `findstr`. POSIX tools may be missing or shadowed by other binaries on PATH."
20+
)
21+
}
22+
23+
if (name === "powershell.exe" || name === "pwsh.exe" || name === "powershell" || name === "pwsh") {
24+
return (
25+
"Commands run through PowerShell, not bash. Use PowerShell syntax: `;` to chain commands, " +
26+
"`$env:VAR` for environment variables, `$?` for whether the last command succeeded " +
27+
"(`$LASTEXITCODE` only holds the exit code of native programs and does not change when a cmdlet fails), " +
28+
"and cmdlets such as `Get-ChildItem` and `Select-String`."
29+
)
30+
}
31+
32+
return undefined
33+
}

0 commit comments

Comments
 (0)