Fix .rules behavior

Richard Feldman created

Change summary

crates/agent/src/agent.rs     |  6 ++++--
crates/agent/src/tests/mod.rs | 14 ++++++--------
2 files changed, 10 insertions(+), 10 deletions(-)

Detailed changes

crates/agent/src/agent.rs 🔗

@@ -368,13 +368,15 @@ impl NativeAgent {
         cx: &mut AsyncApp,
     ) -> Result<()> {
         while needs_refresh.changed().await.is_ok() {
-            let project_context = this
+            let new_project_context_data = this
                 .update(cx, |this, cx| {
                     Self::build_project_context(&this.project, this.prompt_store.as_ref(), cx)
                 })?
                 .await;
             this.update(cx, |this, cx| {
-                this.project_context = cx.new(|_| project_context);
+                this.project_context.update(cx, |project_context, _cx| {
+                    *project_context = new_project_context_data;
+                });
             })?;
         }
 

crates/agent/src/tests/mod.rs 🔗

@@ -2586,15 +2586,14 @@ fn setup_context_server(
 /// being applied to the first message sent in that thread.
 ///
 /// The test simulates:
-/// 1. Creating a thread with a project context entity
-/// 2. A new project context entity is created with updated rules (simulating what
+/// 1. Creating a thread with an initial rule in the project context
+/// 2. Updating the project context entity with new rules (simulating what
 ///    NativeAgent.maintain_project_context does when rules are toggled)
 /// 3. Sending a message through the thread
 /// 4. Verifying that the newly toggled rules appear in the system prompt
 ///
-/// This test will fail until the bug is fixed. The fix requires ensuring that when
-/// rules are toggled, threads see the updated rules rather than continuing to use
-/// a stale project context entity.
+/// The fix ensures that threads see updated rules by updating the project_context
+/// entity in place rather than creating a new entity that threads wouldn't reference.
 #[gpui::test]
 async fn test_rules_toggled_after_thread_creation_are_applied(cx: &mut TestAppContext) {
     let ThreadTest {
@@ -2618,15 +2617,14 @@ async fn test_rules_toggled_after_thread_creation_are_applied(cx: &mut TestAppCo
     let rule_id = UserPromptId::new();
     let new_rule_content = "Always respond in uppercase.";
 
-    let _new_project_context = cx.new(|_cx| {
-        let mut context = ProjectContext::default();
+    project_context.update(cx, |context, _cx| {
+        context.user_rules.clear();
         context.user_rules.push(UserRulesContext {
             uuid: rule_id,
             title: Some("New Rule".to_string()),
             contents: new_rule_content.to_string(),
         });
         context.has_user_rules = true;
-        context
     });
 
     thread