diff --git a/packages/pi-coding-agent/src/core/agent-session.ts b/packages/pi-coding-agent/src/core/agent-session.ts index 3d1351ddf..c856e9229 100644 --- a/packages/pi-coding-agent/src/core/agent-session.ts +++ b/packages/pi-coding-agent/src/core/agent-session.ts @@ -1356,6 +1356,7 @@ export class AgentSession { this.agent.reset(); // Update cwd to current process directory — auto-mode may have chdir'd // into a worktree since the original session was created. + const previousCwd = this._cwd; this._cwd = process.cwd(); this.sessionManager.newSession({ parentSession: options?.parentSession }); this.agent.sessionId = this.sessionManager.getSessionId(); @@ -1365,6 +1366,17 @@ export class AgentSession { this.sessionManager.appendThinkingLevelChange(this.thinkingLevel); + // Rebuild tools when cwd changed (e.g., auto-mode entered a worktree). + // Tools capture cwd at creation time for path resolution — without + // rebuilding, write/read/edit/bash resolve relative paths against + // the original project root instead of the worktree (#633). + if (this._cwd !== previousCwd) { + this._buildRuntime({ + activeToolNames: this.getActiveToolNames(), + includeAllExtensionTools: true, + }); + } + // Run setup callback if provided (e.g., to append initial messages) if (options?.setup) { await options.setup(this.sessionManager);