fix(agents): keep text an agent sends before the prompt out of the reply

pi-acp sends its startup info (version, context files, extensions, update
notice) as an agent message right after session/new. The timeline appended
the reply to that entry, so Generate Commit Message with Agent put the
startup info into the commit message. The timeline is now sealed when the
prompt is sent: the reply's text starts an entry of its own.
Цей коміт міститься в:
l0ng-ai committed 2026-12-05 01:08:25 +08:00
1 джерело 3278eab0c8
коміт dfddb65149
3 files changed
+48 -2

No files matched your search

+1
Переглянути файл
@@ -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<Value, RpcError> =
live.peer.request("session/prompt", json!({ "sessionId": live.session, "prompt": content }), None).await;
if live.driver == provider::Driver::Copilot
+36
Переглянути файл
@@ -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<Option<String>>,
/// 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", &notice);
}
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<String> = 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() {
+11 -2
Переглянути файл
@@ -20,6 +20,8 @@ pub(crate) struct Timeline {
cards: HashMap<String, usize>,
/// Session messages, by message id.
messages: HashMap<String, usize>,
/// 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 {