From 6fb48af4020056b9e5ca12a4ef3136b5e6808a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Padez?= Date: Sat, 25 Jul 2026 12:46:02 +0000 Subject: [PATCH] =?UTF-8?q?chat:=20strengthen=20OpenCode=20cwd=20prompt=20?= =?UTF-8?q?=E2=80=94=20demand=20absolute=20paths=20on=20every=20tool=20cal?= =?UTF-8?q?l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/servers/channels/send-opencode.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/servers/channels/send-opencode.ts b/src/servers/channels/send-opencode.ts index b36b17a3..18bb6994 100644 --- a/src/servers/channels/send-opencode.ts +++ b/src/servers/channels/send-opencode.ts @@ -32,15 +32,19 @@ function splitModel(model: string): { providerID: string; modelID: string } { 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 -// the model its working directory via an appended, non-visible system prompt. Sent on every turn. +// OpenCode can't set a real per-session cwd (every session runs in the fixed server's dir) — and its +// 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 { 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'); }