diff --git a/crates/ide-agent/src/lib.rs b/crates/ide-agent/src/lib.rs index 521f8a1626..4d48443d5d 100644 --- a/crates/ide-agent/src/lib.rs +++ b/crates/ide-agent/src/lib.rs @@ -5456,6 +5456,7 @@ impl Task { content }; let mut content = with_shell(self.content(&live, prompt).await); + self.state.lock().history.conversation.main.seal(); let mut result: Result = live.peer.request("session/prompt", json!({ "sessionId": live.session, "prompt": content }), None).await; if live.driver == provider::Driver::Copilot diff --git a/crates/ide-agent/src/tests.rs b/crates/ide-agent/src/tests.rs index 9fca7b2552..937ed2fa75 100644 --- a/crates/ide-agent/src/tests.rs +++ b/crates/ide-agent/src/tests.rs @@ -49,6 +49,8 @@ struct FakeAgent { /// Hold the late mode reply until the prompt's permission request is already waiting. hold_late_mode: bool, empty_config: AtomicBool, + /// Text sent as an agent message when a session opens, before any prompt (pi-acp's startup info). + startup_notice: Mutex>, /// Blocks `session/new` until discovery times out (see `acp::discover` test timeout). hold_session_new: AtomicBool, /// Sleeps this long at the start of `session/set_config_option`, after noting the time. @@ -221,6 +223,9 @@ impl Handler for FakeAgent { std::thread::sleep(Duration::from_millis(10)); } } + if let Some(notice) = self.startup_notice.lock().clone() { + self.say("s1", ¬ice); + } if self.empty_config.load(Ordering::Acquire) { return Ok(json!({"sessionId": "s1"})); } @@ -919,6 +924,37 @@ async fn an_ephemeral_task_is_not_saved_and_asks_for_an_unsaved_session() { } } +#[tokio::test(flavor = "multi_thread")] +async fn text_the_agent_sends_before_the_prompt_is_not_part_of_the_reply() { + let tmp = tempfile::tempdir().unwrap(); + let agent = Arc::new(FakeAgent::default()); + *agent.startup_notice.lock() = Some("pi v1.5.2\n---\n## Context\n- AGENTS.md\n".into()); + let mgr = manager(tmp.path(), agent.clone()); + let task = mgr + .create(NewAgentTask { + prompt: text("again"), + provider: "claude".into(), + isolation: Isolation::Local, + ephemeral: true, + ..Default::default() + }) + .await + .unwrap(); + wait_for(&mgr, &task.id, AgentStatus::Ready).await; + let replies: Vec = mgr + .entries(&task.id, None) + .unwrap() + .into_iter() + .filter_map(|e| match e { + AgentEntry::Agent { text } => Some(text), + _ => None, + }) + .collect(); + assert_eq!(replies.last().map(String::as_str), Some("second turn")); + assert!(replies.iter().any(|t| t.starts_with("pi v1.5.2"))); + shut_down(&mgr, &agent).await; +} + #[cfg(unix)] #[tokio::test(flavor = "multi_thread")] async fn claude_commands_share_catalog_discovery_and_persist_results() { diff --git a/crates/ide-agent/src/timeline.rs b/crates/ide-agent/src/timeline.rs index 77fc84e5b5..0e1632b162 100644 --- a/crates/ide-agent/src/timeline.rs +++ b/crates/ide-agent/src/timeline.rs @@ -20,6 +20,8 @@ pub(crate) struct Timeline { cards: HashMap, /// Session messages, by message id. messages: HashMap, + /// The next agent text starts an entry of its own (see [`Timeline::seal`]). + sealed: bool, } /// What a `session/update` changed in the timeline. @@ -138,10 +140,17 @@ impl Timeline { out } + /// A prompt is being sent: text the agent sent before it (a startup notice) is not the + /// start of the reply, so the reply's text gets an entry of its own. + pub fn seal(&mut self) { + self.sealed = true; + } + fn append_text(&mut self, thought: bool, text: &str) -> usize { + let sealed = std::mem::take(&mut self.sealed); match self.entries.last_mut() { - Some(AgentEntry::Agent { text: t }) if !thought => t.push_str(text), - Some(AgentEntry::Thought { text: t, .. }) if thought => t.push_str(text), + Some(AgentEntry::Agent { text: t }) if !thought && !sealed => t.push_str(text), + Some(AgentEntry::Thought { text: t, .. }) if thought && !sealed => t.push_str(text), _ => { let text = text.to_owned(); return self.push(if thought {