From e8c13e0ee8e32bfd92888e410f8661b7fac3e989 Mon Sep 17 00:00:00 2001 From: "kiloconnect[bot]" <240665456+kiloconnect[bot]@users.noreply.github.com> Date: Mon, 27 Apr 2026 07:31:57 +0000 Subject: [PATCH 1/2] fix(cli): preserve shell cwd after login startup --- packages/opencode/src/session/prompt.ts | 12 ++++++---- .../test/session/prompt-effect.test.ts | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 0281aaf2816..cb52b9e478a 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -792,6 +792,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the const shellName = ( process.platform === "win32" ? path.win32.basename(sh, ".exe") : path.basename(sh) ).toLowerCase() + const cwd = ctx.directory const invocations: Record = { nu: { args: ["-c", input.command] }, fish: { args: ["-c", input.command] }, @@ -800,12 +801,13 @@ NOTE: At any point in time through this workflow you should feel free to ask the "-l", "-c", ` - __oc_cwd=$PWD [[ -f ~/.zshenv ]] && source ~/.zshenv >/dev/null 2>&1 || true [[ -f "\${ZDOTDIR:-$HOME}/.zshrc" ]] && source "\${ZDOTDIR:-$HOME}/.zshrc" >/dev/null 2>&1 || true - cd "$__oc_cwd" + cd -- "$1" eval ${JSON.stringify(input.command)} `, + "opencode", + cwd, ], }, bash: { @@ -813,12 +815,13 @@ NOTE: At any point in time through this workflow you should feel free to ask the "-l", "-c", ` - __oc_cwd=$PWD shopt -s expand_aliases [[ -f ~/.bashrc ]] && source ~/.bashrc >/dev/null 2>&1 || true - cd "$__oc_cwd" + cd -- "$1" eval ${JSON.stringify(input.command)} `, + "opencode", + cwd, ], }, cmd: { args: ["/c", input.command] }, @@ -828,7 +831,6 @@ NOTE: At any point in time through this workflow you should feel free to ask the } const args = (invocations[shellName] ?? invocations[""]).args - const cwd = ctx.directory const shellEnv = yield* plugin.trigger( "shell.env", { cwd, sessionID: input.sessionID, callID: part.callID }, diff --git a/packages/opencode/test/session/prompt-effect.test.ts b/packages/opencode/test/session/prompt-effect.test.ts index 1cf6abb779f..21b2a0d50a6 100644 --- a/packages/opencode/test/session/prompt-effect.test.ts +++ b/packages/opencode/test/session/prompt-effect.test.ts @@ -1080,6 +1080,30 @@ unix("shell completes a fast command on the preferred shell", () => ), ) +unix("shell commands can change directory after startup", () => + provideTmpdirInstance( + (dir) => + Effect.gen(function* () { + const { prompt, run, chat } = yield* boot() + const parent = path.dirname(dir) + const result = yield* prompt.shell({ + sessionID: chat.id, + agent: "code", + command: "cd .. && pwd", + }) + + expect(result.info.role).toBe("assistant") + const tool = completedTool(result.parts) + if (!tool) return + + expect(tool.state.output).toContain(parent) + expect(tool.state.metadata.output).toContain(parent) + yield* run.assertNotBusy(chat.id) + }), + { git: true, config: cfg }, + ), +) + unix("shell lists files from the project directory", () => provideTmpdirInstance( (dir) => From f631338576830b1de993324ae8ff10d870a69ab7 Mon Sep 17 00:00:00 2001 From: "kiloconnect[bot]" <240665456+kiloconnect[bot]@users.noreply.github.com> Date: Mon, 27 Apr 2026 07:38:01 +0000 Subject: [PATCH 2/2] chore(cli): add kilocode_change markers to ported shell cwd fix --- packages/opencode/src/session/prompt.ts | 6 +++++- packages/opencode/test/session/prompt-effect.test.ts | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index cb52b9e478a..193b289c55f 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -792,11 +792,12 @@ NOTE: At any point in time through this workflow you should feel free to ask the const shellName = ( process.platform === "win32" ? path.win32.basename(sh, ".exe") : path.basename(sh) ).toLowerCase() - const cwd = ctx.directory + const cwd = ctx.directory // kilocode_change - moved up to use in invocations below const invocations: Record = { nu: { args: ["-c", input.command] }, fish: { args: ["-c", input.command] }, zsh: { + // kilocode_change start - port anomalyco/opencode#24215: pass cwd as positional arg instead of $PWD (CI resets $PWD after startup files) args: [ "-l", "-c", @@ -809,8 +810,10 @@ NOTE: At any point in time through this workflow you should feel free to ask the "opencode", cwd, ], + // kilocode_change end }, bash: { + // kilocode_change start - port anomalyco/opencode#24215: pass cwd as positional arg instead of $PWD (CI resets $PWD after startup files) args: [ "-l", "-c", @@ -823,6 +826,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the "opencode", cwd, ], + // kilocode_change end }, cmd: { args: ["/c", input.command] }, powershell: { args: ["-NoProfile", "-Command", input.command] }, diff --git a/packages/opencode/test/session/prompt-effect.test.ts b/packages/opencode/test/session/prompt-effect.test.ts index 21b2a0d50a6..4b3777f32bc 100644 --- a/packages/opencode/test/session/prompt-effect.test.ts +++ b/packages/opencode/test/session/prompt-effect.test.ts @@ -1080,6 +1080,7 @@ unix("shell completes a fast command on the preferred shell", () => ), ) +// kilocode_change start - port anomalyco/opencode#24215 cover shell cwd changes (agent "build" → "code" for our fork) unix("shell commands can change directory after startup", () => provideTmpdirInstance( (dir) => @@ -1103,6 +1104,7 @@ unix("shell commands can change directory after startup", () => { git: true, config: cfg }, ), ) +// kilocode_change end unix("shell lists files from the project directory", () => provideTmpdirInstance(