Skip to content

Commit cc9bb5c

Browse files
PierrunoYTclaude
andcommitted
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>
1 parent ca05b73 commit cc9bb5c

4 files changed

Lines changed: 86 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 = osName as unknown as ReturnType<typeof vi.fn>
@@ -101,4 +102,27 @@ describe("getSystemInfoSection", () => {
101102
// sentinel string) breaks this exact join.
102103
expect(result).toContain("default directory for all tool operations. When the user initially gives you a task")
103104
})
105+
106+
describe("shell syntax", () => {
107+
const cmdPath = "C:\\Windows\\System32\\cmd.exe"
108+
109+
it("states the dialect for cmd.exe when execute_command is available", () => {
110+
vi.mocked(getShell).mockReturnValueOnce(cmdPath)
111+
112+
const result = getSystemInfoSection(mockCwd, policyFor())
113+
114+
expect(result).toContain(`Default Shell: ${cmdPath}
115+
Shell Syntax: Commands run through Windows cmd.exe`)
116+
})
117+
118+
it("omits the dialect without execute_command", () => {
119+
vi.mocked(getShell).mockReturnValueOnce(cmdPath)
120+
121+
expect(getSystemInfoSection(mockCwd, policyFor(false))).not.toContain("Shell Syntax")
122+
})
123+
124+
it("omits the dialect for POSIX shells", () => {
125+
expect(getSystemInfoSection(mockCwd, policyFor())).not.toContain("Shell Syntax")
126+
})
127+
})
104128
})

‎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

@@ -33,12 +34,19 @@ export function getSystemInfoSection(cwd: string, policy: EffectiveToolPolicy):
3334
? " 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."
3435
: ""
3536

37+
const shell = getShell()
38+
const shellDialect = executeCommandAvailable ? describeShellDialect(shell) : undefined
39+
const shellDialectLine = shellDialect
40+
? `
41+
Shell Syntax: ${shellDialect}`
42+
: ""
43+
3644
const details = `====
3745
3846
SYSTEM INFORMATION
3947
4048
Operating System: ${osInfo}
41-
Default Shell: ${getShell()}
49+
Default Shell: ${shell}${shellDialectLine}
4250
Home Directory: ${os.homedir().toPosix()}
4351
Current Workspace Directory: ${cwd.toPosix()}
4452
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describeShellDialect } from "../shellDialect"
2+
3+
describe("describeShellDialect", () => {
4+
it.each([
5+
{ shell: "C:\\Windows\\System32\\cmd.exe", expected: "cmd.exe" },
6+
{ shell: "C:\\Windows\\System32\\CMD.EXE", expected: "cmd.exe" },
7+
{ shell: "C:\\Program Files\\PowerShell\\7\\pwsh.exe", expected: "PowerShell" },
8+
{ shell: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", expected: "PowerShell" },
9+
])("names the dialect for $shell", ({ shell, expected }) => {
10+
expect(describeShellDialect(shell)).toContain(expected)
11+
})
12+
13+
it("warns that ';' does not chain commands in cmd.exe", () => {
14+
expect(describeShellDialect("C:\\Windows\\System32\\cmd.exe")).toContain("`;` is not a separator")
15+
})
16+
17+
it.each(["/bin/bash", "/bin/zsh", "C:\\Program Files\\Git\\bin\\bash.exe", "C:\\Windows\\System32\\wsl.exe"])(
18+
"adds nothing for POSIX shell %s",
19+
(shell) => {
20+
expect(describeShellDialect(shell)).toBeUndefined()
21+
},
22+
)
23+
})

‎src/utils/shellDialect.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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`, `%ERRORLEVEL%` for exit codes, " +
17+
"and `dir`, `type`, `findstr`. POSIX tools may be missing or shadowed by other binaries on PATH."
18+
)
19+
}
20+
21+
if (name === "powershell.exe" || name === "pwsh.exe" || name === "powershell" || name === "pwsh") {
22+
return (
23+
"Commands run through PowerShell, not bash. Use PowerShell syntax: `;` to chain commands, " +
24+
"`$env:VAR` for environment variables, `$LASTEXITCODE` for exit codes, and cmdlets such as " +
25+
"`Get-ChildItem` and `Select-String`."
26+
)
27+
}
28+
29+
return undefined
30+
}

0 commit comments

Comments
 (0)