fix(session): rebuild tools when cwd changes in newSession (#633) (#638)

Tools (write, read, edit, bash) capture cwd at creation time via
createWriteTool(cwd), createReadTool(cwd), etc. When auto-mode
enters a worktree, process.cwd() changes but tools were not
recreated — they continued resolving relative paths against the
original project root.

This caused artifacts to be written to the main project's .gsd/
directory instead of the worktree's .gsd/ directory. The dispatcher
then couldn't find the artifact at the expected worktree path and
retried the unit indefinitely.

Fix: detect cwd change in newSession() and call _buildRuntime()
to recreate tools with the updated cwd. This is a targeted rebuild
that only fires when cwd actually changed (typically once per
auto-mode session when entering/exiting a worktree).

Fixes #633
This commit is contained in:
Tom Boucher 2026-03-16 11:23:19 -04:00 committed by GitHub
parent fd29c02c81
commit 75e82a4236

View file

@ -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);