Skip to content

Commit 697b214

Browse files
lukemarsdenclaude
andcommitted
fix: use consistent cwd for ACP session storage
Use ZED_WORK_DIR env var (or $HOME/work fallback) as the root directory for ACP agents. This ensures session storage is always in the same location, fixing session restoration on Zed restart. Previously, root_dir was derived from worktrees which could vary based on ordering or timing. Now it's always consistent. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ae86510 commit 697b214

1 file changed

Lines changed: 8 additions & 20 deletions

File tree

crates/agent_ui/src/acp/thread_view.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -680,24 +680,12 @@ impl AcpThreadView {
680680
"External agents are not yet supported in shared projects.".into(),
681681
));
682682
}
683-
let mut worktrees = project.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
684-
// Pick the first non-single-file worktree for the root directory if there are any,
685-
// and otherwise the parent of a single-file worktree, falling back to $HOME if there are no visible worktrees.
686-
worktrees.sort_by(|l, r| {
687-
l.read(cx)
688-
.is_single_file()
689-
.cmp(&r.read(cx).is_single_file())
690-
});
691-
let root_dir = worktrees
692-
.into_iter()
693-
.filter_map(|worktree| {
694-
if worktree.read(cx).is_single_file() {
695-
Some(worktree.read(cx).abs_path().parent()?.into())
696-
} else {
697-
Some(worktree.read(cx).abs_path())
698-
}
699-
})
700-
.next();
683+
// Use ZED_WORK_DIR if set, otherwise fall back to $HOME/work.
684+
// This ensures ACP session storage is always at a consistent location.
685+
let root_dir: Option<std::sync::Arc<std::path::Path>> = std::env::var("ZED_WORK_DIR")
686+
.ok()
687+
.or_else(|| std::env::var("HOME").ok().map(|home| format!("{}/work", home)))
688+
.map(|dir| std::sync::Arc::from(std::path::Path::new(&dir).to_path_buf()));
701689
let (status_tx, mut status_rx) = watch::channel("Loading…".into());
702690
let (new_version_available_tx, mut new_version_available_rx) = watch::channel(None);
703691
let delegate = AgentServerDelegate::new(
@@ -749,8 +737,8 @@ impl AcpThreadView {
749737
.or_else(|| connection.get_last_session_id(&root_dir));
750738

751739
if let Some(session_id) = session_id {
752-
log::info!("🔄 [ACP SESSION] Loading session for agent: {:?}", session_id);
753-
eprintln!("🔄 [ACP SESSION] Loading session for agent: {:?}", session_id);
740+
log::info!("🔄 [ACP SESSION] Loading session {:?} with cwd: {:?}", session_id, root_dir);
741+
eprintln!("🔄 [ACP SESSION] Loading session {:?} with cwd: {:?}", session_id, root_dir);
754742
cx.update(|_, cx| {
755743
connection
756744
.clone()

0 commit comments

Comments
 (0)