From 8882ce484e0d531611d64ab49115cb3694d83f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=82CHES?= Date: Sun, 15 Mar 2026 15:45:15 -0600 Subject: [PATCH] fix(session): update cwd on newSession to reflect worktree chdir (#517) When auto-mode creates a worktree and chdir's into it, the Node process cwd changes but AgentSession._cwd stays frozen at the original path. Every newSession() builds a system prompt telling the LLM "Current working directory: /original/path", so the LLM cd's back there and writes files to the wrong location. Update _cwd = process.cwd() at the start of newSession() so the system prompt reflects the actual working directory after chdir. Co-authored-by: Claude Opus 4.6 (1M context) --- packages/pi-coding-agent/src/core/agent-session.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/pi-coding-agent/src/core/agent-session.ts b/packages/pi-coding-agent/src/core/agent-session.ts index 6fc9a9853..2e8fac03a 100644 --- a/packages/pi-coding-agent/src/core/agent-session.ts +++ b/packages/pi-coding-agent/src/core/agent-session.ts @@ -1354,6 +1354,9 @@ export class AgentSession { this._disconnectFromAgent(); await this.abort(); this.agent.reset(); + // Update cwd to current process directory — auto-mode may have chdir'd + // into a worktree since the original session was created. + this._cwd = process.cwd(); this.sessionManager.newSession({ parentSession: options?.parentSession }); this.agent.sessionId = this.sessionManager.getSessionId(); this._steeringMessages = [];