Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/opencode/src/tool/shell/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,28 @@ Usage notes:
</bad-example>`
}

const NO_RETRY_RULE = `- On the first shell parse error (\`ParserError\`, "is not recognized", "not a valid regular expression", etc.), STOP retrying the same construct. Either (a) translate the command to this shell's native syntax, or (b) for anything multi-line or loop-shaped, write a script file with the Write tool (\`.py\`, \`.ps1\`, or \`.sh\` as appropriate) and execute it. Never attempt the same failing syntax pattern more than once.`

function shellAdaptationSection(name: string) {
const header = "# Shell identity and failure adaptation"
if (PS.has(name)) {
return `${header}
- This host runs ${shellDisplayName(name)}, not a POSIX shell. POSIX-shaped commands are parse errors here: heredocs (\`<<\`), \`for f in ...; do ... done\` one-liners, and POSIX flag forms such as \`rm -f\` (PowerShell reads \`-f\` as an ambiguous parameter name).
${NO_RETRY_RULE}
- PowerShell regex and quoting rules differ from POSIX: use single-quoted verbatim strings for patterns so backslashes are not mangled, and prefer a script file over inline regex when unsure.`
}
if (CMD.has(name)) {
return `${header}
- This host runs ${shellDisplayName(name)}, not a POSIX shell and not PowerShell. POSIX constructs (heredocs \`<<\`, \`for f in ...; do ... done\` one-liners, POSIX flag forms such as \`rm -f\`) and PowerShell cmdlets such as \`Remove-Item\` are both parse errors here.
${NO_RETRY_RULE}
- cmd.exe quoting and escaping rules differ from POSIX: use double quotes, escape \`%\` as \`%%\`, and prefer a script file over an elaborate inline one-liner.`
}
return `${header}
- This host runs ${name} (POSIX). PowerShell and cmd.exe idioms such as \`Remove-Item\`, \`Get-ChildItem\`, or \`if exist\` are not valid here; use POSIX equivalents.
${NO_RETRY_RULE}
- If a POSIX-shaped command ever produces a parse error, treat it as a shell-family mismatch rather than a typo and switch strategy instead of re-sending the same construct.`
}

function profile(name: string, platform: NodeJS.Platform, limits: Limits, defaultTimeoutMs: number) {
const isPowerShell = PS.has(name)
const chain = chainGuidance(name)
Expand Down Expand Up @@ -278,6 +300,7 @@ export function render(name: string, platform: NodeJS.Platform, limits: Limits,
os: platform,
shell: name,
tmp: Global.Path.tmp,
shellAdaptation: shellAdaptationSection(name),
workdirSection: selected.workdirSection,
commandSection: selected.commandSection,
gitCommands: selected.gitCommands,
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/tool/shell/shell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ ${commandSection}
- Before creating a PR, inspect status, diff, remote tracking, recent commits, and the diff from the base branch.
- Review all commits included in the PR, not just the latest commit.
- Use `gh` for GitHub tasks, including PRs, issues, checks, and releases; return the PR URL when done.

${shellAdaptation}
94 changes: 94 additions & 0 deletions packages/opencode/test/tool/shell-prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { describe, expect, test } from "bun:test"
import { ShellPrompt } from "@/tool/shell/prompt"
import EDIT from "@/tool/edit.txt"
import GLOB from "@/tool/glob.txt"
import GREP from "@/tool/grep.txt"
import READ from "@/tool/read.txt"
import WRITE from "@/tool/write.txt"

const limits = { maxLines: 100, maxBytes: 10_000 }
const posix: Array<[string, NodeJS.Platform]> = [
["bash", "linux"],
["zsh", "darwin"],
]
const powershell: Array<[string, NodeJS.Platform]> = [
["pwsh", "win32"],
["powershell", "win32"],
]
const cmd: Array<[string, NodeJS.Platform]> = [["cmd", "win32"]]
const all = [...posix, ...powershell, ...cmd]

/** Collapse template wrapping so assertions do not depend on line breaks. */
const flat = (name: string, platform: NodeJS.Platform) =>
ShellPrompt.render(name, platform, limits, 120_000).description.replace(/\s+/g, " ")

describe("tool.shell prompt shell identity", () => {
for (const [name, platform] of all) {
test(`${name} description carries the no-retry rule`, () => {
const description = flat(name, platform)

expect(description).toContain("# Shell identity and failure adaptation")
expect(description).toContain("STOP retrying the same construct")
expect(description).toContain("Never attempt the same failing syntax pattern more than once.")
// Recovery paths: translate, or write and run a script file.
expect(description).toContain("translate the command to this shell's native syntax")
expect(description).toContain("write a script file with the Write tool")
expect(description).toContain("`ParserError`")
expect(description).toContain("not a valid regular expression")
})
}

for (const [name, platform] of [...powershell, ...cmd]) {
test(`${name} description names the three POSIX failure modes`, () => {
const description = flat(name, platform)

expect(description).toContain("heredocs")
expect(description).toContain("`<<`")
expect(description).toContain("`for f in ...; do ... done` one-liners")
expect(description).toContain("`rm -f`")
expect(description).toContain("not a POSIX shell")
})
}

for (const [name, platform] of powershell) {
test(`${name} description keeps the PowerShell quoting guidance`, () => {
const description = flat(name, platform)

expect(description).toContain("single-quoted verbatim strings for patterns")
expect(description).toContain("ambiguous parameter name")
})
}

test("cmd.exe guidance does not claim the shell is bash or PowerShell", () => {
const description = flat("cmd", "win32")

expect(description).toContain("This host runs cmd.exe")
expect(description).toContain("not a POSIX shell and not PowerShell")
expect(description).not.toContain("Your shell may be bash (POSIX) or PowerShell")
})

for (const [name, platform] of posix) {
test(`${name} guidance is POSIX-framed and not PowerShell-framed`, () => {
const description = flat(name, platform)

expect(description).toContain(`This host runs ${name} (POSIX)`)
expect(description).toContain("If a POSIX-shaped command ever produces a parse error")
expect(description).not.toContain("heredocs")
})
}

test("rendered descriptions contain no leftover template placeholders", () => {
for (const [name, platform] of all) {
expect(ShellPrompt.render(name, platform, limits, 120_000).description).not.toContain("${")
}
})
})

describe("tool.shell prompt scoping", () => {
test("the section stays out of other tools' descriptions", () => {
for (const other of [WRITE, EDIT, GREP, GLOB, READ]) {
expect(other).not.toContain("Shell identity and failure adaptation")
expect(other).not.toContain("Never attempt the same failing syntax pattern")
}
})
})
Loading