Skip to content

Commit 53d8fef

Browse files
PierrunoYTclaude
andcommitted
fix(dcg): correct PowerShell status guidance and assert interpolated shell name
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent e1d0e5d commit 53d8fef

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎src/core/tools/__tests__/executeCommandTool.spec.ts‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { unescapeHtmlEntities } from "../../../utils/text-normalization"
1313
import { Terminal } from "../../../integrations/terminal/Terminal"
1414
import { TerminalRegistry } from "../../../integrations/terminal/TerminalRegistry"
1515
import type { RooTerminalCallbacks, RooTerminalProcess } from "../../../integrations/terminal/types"
16+
import { t } from "../../../i18n"
1617

1718
// Mock dependencies
1819
vitest.mock("execa", () => ({
@@ -61,6 +62,15 @@ vitest.mock("../../task/Task", async (importOriginal) => {
6162
})
6263
vitest.mock("../../prompts/responses")
6364

65+
// Tests run with empty translation resources, so `t` returns the key without its namespace.
66+
// Keep that default, and let individual tests replace it with an interpolating fake.
67+
const stripNamespace = (key: string) => key.replace(/^\w+:/, "")
68+
69+
vitest.mock("../../../i18n", async (importOriginal) => {
70+
const actual = await importOriginal<typeof import("../../../i18n")>()
71+
return { ...actual, t: vitest.fn((key: string) => key.replace(/^\w+:/, "")) }
72+
})
73+
6474
const mockRunDcg = vitest.fn()
6575
const mockEnsureDcgInstalled = vitest.fn()
6676

@@ -93,6 +103,7 @@ describe("executeCommandTool", () => {
93103
beforeEach(() => {
94104
// Reset mocks
95105
vitest.clearAllMocks()
106+
vitest.mocked(t).mockImplementation(stripNamespace)
96107
vitest.useRealTimers()
97108

98109
// Spy on executeCommandInTerminal and mock its return value
@@ -320,14 +331,27 @@ describe("executeCommandTool", () => {
320331
})
321332

322333
it.each([
323-
["C:\\Windows\\System32\\cmd.exe", true],
324-
["C:\\Program Files\\PowerShell\\7\\pwsh.exe", true],
325-
["/bin/bash", false],
326-
[undefined, false],
327-
])("appends the POSIX-assumption note only for non-POSIX shell %s", (shellPath, expectNote) => {
334+
["C:\\Windows\\System32\\cmd.exe", "cmd.exe"],
335+
["C:\\Program Files\\PowerShell\\7\\pwsh.exe", "pwsh.exe"],
336+
["/bin/bash", undefined],
337+
[undefined, undefined],
338+
])("appends the POSIX-assumption note only for non-POSIX shell %s", (shellPath, expectedShell) => {
339+
// Interpolate like the real translation so a dropped `shell` option fails the test.
340+
vitest
341+
.mocked(t)
342+
.mockImplementation((key: string, options?: Record<string, unknown>) =>
343+
options?.shell ? `${stripNamespace(key)}[shell=${String(options.shell)}]` : stripNamespace(key),
344+
)
345+
328346
const message = executeCommandModule.formatDcgBlockedMessage(undefined, undefined, shellPath)
329347

330-
expect(message.includes("executeCommand.destructiveCommandGuard.assumedPosixNote")).toBe(expectNote)
348+
if (expectedShell) {
349+
expect(message).toContain(
350+
`executeCommand.destructiveCommandGuard.assumedPosixNote[shell=${expectedShell}]`,
351+
)
352+
} else {
353+
expect(message).not.toContain("assumedPosixNote")
354+
}
331355
})
332356

333357
it("shows a DCG block message as an error before requesting explicit approval", async () => {

‎src/utils/shellDialect.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export function describeShellDialect(shellPath: string): string | undefined {
2525
if (name === "powershell.exe" || name === "pwsh.exe" || name === "powershell" || name === "pwsh") {
2626
return (
2727
"Commands run through PowerShell, not bash. Use PowerShell syntax: `;` to chain commands, " +
28-
"`$env:VAR` for environment variables, `$LASTEXITCODE` for exit codes, and cmdlets such as " +
29-
"`Get-ChildItem` and `Select-String`."
28+
"`$env:VAR` for environment variables, `$?` for whether the last command succeeded " +
29+
"(`$LASTEXITCODE` only holds the exit code of native programs and does not change when a cmdlet fails), " +
30+
"and cmdlets such as `Get-ChildItem` and `Select-String`."
3031
)
3132
}
3233

0 commit comments

Comments
 (0)