From c932aa9f5b8f9b09146b82a3ba1f6580a0a2a937 Mon Sep 17 00:00:00 2001 From: Ben Brandt Date: Wed, 18 Mar 2026 23:43:44 +0100 Subject: [PATCH] agent: Fix project context on first thread (#51868) We seemed to have a regression where the worktrees weren't showing up in the system prompt on first load. Release Notes: - N/A --- crates/agent/src/agent.rs | 55 ++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/crates/agent/src/agent.rs b/crates/agent/src/agent.rs index 140c872ecacc9d572881d2745bbbfc2810b1d309..5a3b1710d18f16805d8ca10ff7cbd5708de88316 100644 --- a/crates/agent/src/agent.rs +++ b/crates/agent/src/agent.rs @@ -493,8 +493,12 @@ impl NativeAgent { })?? .await; this.update(cx, |this, cx| { - if let Some(state) = this.projects.get_mut(&project_id) { - state.project_context = cx.new(|_| project_context); + if let Some(state) = this.projects.get(&project_id) { + state + .project_context + .update(cx, |current_project_context, _cx| { + *current_project_context = project_context; + }); } })?; } @@ -2131,10 +2135,15 @@ mod internal_tests { .unwrap(); cx.run_until_parked(); + let thread = agent.read_with(cx, |agent, _cx| { + agent.sessions.values().next().unwrap().thread.clone() + }); + agent.read_with(cx, |agent, cx| { let project_id = project.entity_id(); let state = agent.projects.get(&project_id).unwrap(); - assert_eq!(state.project_context.read(cx).worktrees, vec![]) + assert_eq!(state.project_context.read(cx).worktrees, vec![]); + assert_eq!(thread.read(cx).project_context().read(cx).worktrees, vec![]); }); let worktree = project @@ -2145,14 +2154,16 @@ mod internal_tests { agent.read_with(cx, |agent, cx| { let project_id = project.entity_id(); let state = agent.projects.get(&project_id).unwrap(); + let expected_worktrees = vec![WorktreeContext { + root_name: "a".into(), + abs_path: Path::new("/a").into(), + rules_file: None, + }]; + assert_eq!(state.project_context.read(cx).worktrees, expected_worktrees); assert_eq!( - state.project_context.read(cx).worktrees, - vec![WorktreeContext { - root_name: "a".into(), - abs_path: Path::new("/a").into(), - rules_file: None - }] - ) + thread.read(cx).project_context().read(cx).worktrees, + expected_worktrees + ); }); // Creating `/a/.rules` updates the project context. @@ -2165,18 +2176,20 @@ mod internal_tests { .read(cx) .entry_for_path(rel_path(".rules")) .unwrap(); + let expected_worktrees = vec![WorktreeContext { + root_name: "a".into(), + abs_path: Path::new("/a").into(), + rules_file: Some(RulesFileContext { + path_in_worktree: rel_path(".rules").into(), + text: "".into(), + project_entry_id: rules_entry.id.to_usize(), + }), + }]; + assert_eq!(state.project_context.read(cx).worktrees, expected_worktrees); assert_eq!( - state.project_context.read(cx).worktrees, - vec![WorktreeContext { - root_name: "a".into(), - abs_path: Path::new("/a").into(), - rules_file: Some(RulesFileContext { - path_in_worktree: rel_path(".rules").into(), - text: "".into(), - project_entry_id: rules_entry.id.to_usize() - }) - }] - ) + thread.read(cx).project_context().read(cx).worktrees, + expected_worktrees + ); }); }