chat: strengthen OpenCode cwd prompt — demand absolute paths on every tool call

The prior "treat this as your cwd" wording didn't stop the model from using bare
globs/relative paths, which OpenCode resolves against the server cwd. Reworded to
explicitly require absolute paths under the target dir on every tool call (and `cd`
for bash). Still a soft override; a stronger attempt before considering `opencode run --dir`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-25 12:46:02 +00:00
co-authored by Claude Opus 4.8
parent d8e0dc79ef
commit 6fb48af402
+9 -5
View File
@@ -32,15 +32,19 @@ function splitModel(model: string): { providerID: string; modelID: string } {
return { providerID: model.slice(0, slash), modelID: model.slice(slash + 1) }; return { providerID: model.slice(0, slash), modelID: model.slice(slash + 1) };
} }
// OpenCode can't set a real per-session cwd (every session runs in the fixed server's dir), so we tell // OpenCode can't set a real per-session cwd (every session runs in the fixed server's dir) — and its
// the model its working directory via an appended, non-visible system prompt. Sent on every turn. // tools follow that server cwd, so relative paths/bare globs resolve to the wrong place. We instruct
// the model, via an appended (non-visible) system prompt sent every turn, to target the intended dir
// explicitly with absolute paths on every tool call.
function officerSystemPrompt(cwd: string): string { function officerSystemPrompt(cwd: string): string {
return [ return [
`Session working directory: ${cwd}`, `Working directory for this session: ${cwd}`,
'', '',
`For this session, treat \`${cwd}\` as your current working directory — this overrides any other working directory the environment reports. Resolve relative paths, globs, and file references against it, and run shell commands from inside it (\`cd\` into it, or use absolute paths beneath it). When the user says "here", "this directory", or gives a relative path, they mean a location within \`${cwd}\`.`, `Your tools execute with a system working directory that is NOT \`${cwd}\`, so relative paths and bare globs (\`.\`, \`*\`, \`./x\`) resolve to the wrong place. To actually operate in \`${cwd}\`, target it explicitly on EVERY tool call:`,
`- File/search tools (read, write, edit, ls, glob, grep, …): always pass an ABSOLUTE path under \`${cwd}\` — e.g. \`${cwd}/notes/todo.md\`, or glob \`${cwd}/**/*\`. Never use a relative path or a bare \`.\`/\`*\`.`,
`- Shell/bash: start every command with \`cd ${cwd}\` (or use absolute paths beneath it).`,
'', '',
`Unless the user explicitly directs you elsewhere, keep your work focused on \`${cwd}\`, its files, and its subdirectories.`, `Interpret "here", "this directory", "the current folder", or any relative path the user gives as a location inside \`${cwd}\`. Keep all your work within \`${cwd}\` and its subdirectories unless the user explicitly directs you elsewhere.`,
].join('\n'); ].join('\n');
} }