From 8492b15d72028ac0dbf1db6f85ee7ba972ce2017 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Wed, 12 Nov 2025 10:48:06 -0500 Subject: [PATCH] Fix .rules behavior --- crates/agent/src/agent.rs | 6 ++++-- crates/agent/src/tests/mod.rs | 14 ++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/agent/src/agent.rs b/crates/agent/src/agent.rs index a85c01bb225ab58a372a8b09fb07e7bc155a7aeb..3b7f2d4f72afa48ec26ce1d3d5353f14dff80cee 100644 --- a/crates/agent/src/agent.rs +++ b/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; + }); })?; } diff --git a/crates/agent/src/tests/mod.rs b/crates/agent/src/tests/mod.rs index 6066a77895c9517aed9d8b401a28f11794a11f28..b9d9159550d07dabf1659db53587f86af801b4cb 100644 --- a/crates/agent/src/tests/mod.rs +++ b/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