fix(agents): show an imported session's history in its task (#5644)

An imported Claude Code or Codex session starts as a task with the session id and no entries. Its first turn loaded the session, but every update replayed during session/load was dropped as history the task already had, and the timeline ignores user_message_chunk, so only messages sent after the import appeared. A task restored without entries now adopts the next load's replay as its history (user messages included), placed before any message that started the turn; later loads drop the replay as before. Importing also loads the session in the background, released when idle, so the history shows right away.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-24 22:23:45 +08:00
1 джерело 6cca609d5c
коміт 72b4d5c6de
2 files changed
+141 -3

No files matched your search

+88 -3
Переглянути файл
@@ -549,8 +549,11 @@ struct Task {
/// Unanswered permission requests: where to send the chosen option id.
permissions: Mutex<HashMap<u32, std::sync::mpsc::Sender<Option<String>>>>,
next_permission: AtomicU32,
/// Set while `session/load` replays history we already have.
/// Set while `session/load` replays the session's history.
replaying: AtomicBool,
/// The task has a session but none of its history (an imported session): the next load's
/// replay becomes the task's history instead of being dropped.
adopting_replay: AtomicBool,
/// The saved session failed to load and the user was told: the next start opens a new one.
start_fresh: AtomicBool,
/// The current turn is being cancelled to send the first queued message right away.
@@ -929,7 +932,7 @@ impl AgentManager {
out
}
/// Imports an external session as a task: its first turn loads and replays it (#312).
/// Imports an external session as a task, loading it in the background to show its history (#312).
pub fn import_session(&self, provider: &str, session_id: &str) -> IdeResult<AgentTask> {
if !matches!(provider, "claude" | "codex") {
return Err(IdeError::invalid(format!("cannot import {provider} sessions")));
@@ -994,6 +997,13 @@ impl AgentManager {
self.tasks.write().insert(task.id.clone(), handle.clone());
handle.emit_task();
handle.persist();
// Loading the session replays its history into the task, so it shows before a first message.
self.shared.runtime.spawn(async move {
match handle.ensure_session().await {
Ok(_) => handle.release_when_idle(),
Err(e) => tracing::info!("agent task {}: loading the imported session failed: {e}", handle.id),
}
});
Ok(task)
}
@@ -2212,6 +2222,7 @@ impl Task {
fn restore(file: TaskFile, shared: Arc<Shared>) -> Arc<Self> {
let mut task = file.task;
config::add_client_options(&task.provider, &mut task.config);
let adopt = file.entries.is_empty() && file.session_id.is_some();
let conversation = Conversation::restore(file.entries, file.subagents);
let mut wanted = file.wanted;
let pending_model = file.pending_switch.or_else(|| {
@@ -2258,6 +2269,7 @@ impl Task {
permissions: Mutex::new(HashMap::new()),
next_permission: AtomicU32::new(1),
replaying: AtomicBool::new(false),
adopting_replay: AtomicBool::new(adopt),
start_fresh: AtomicBool::new(false),
send_now: AtomicBool::new(false),
cancel_pending: AtomicBool::new(false),
@@ -2569,6 +2581,19 @@ impl Task {
self.persist();
}
/// Removes the task's own entries and returns them.
fn take_entries(self: &Arc<Self>) -> Vec<AgentEntry> {
let entries = {
let mut s = self.state.lock();
let entries = s.conversation.main.entries.clone();
s.conversation.truncate(0);
entries
};
self.streamed.lock().clear();
self.shared.emit(AgentEvent::Truncated { task: self.id.clone(), length: 0 });
entries
}
fn push_entry(self: &Arc<Self>, entry: AgentEntry) -> usize {
let i = self.state.lock().conversation.main.push(entry);
self.mark_dirty([(None, i)]);
@@ -3161,12 +3186,27 @@ impl Task {
});
}
if let (Some(id), true, false) = (saved, can_load, start_fresh) {
// The replay goes before what this turn already showed (the user's message).
let shown = self.adopts_replay().then(|| self.take_entries());
self.replaying.store(true, Ordering::Release);
let mut params = session_params(&cwd_s, &mcp_servers, skills_delivery.as_ref(), &roots);
params["sessionId"] = json!(&id);
let loaded = self.request_with_auth(&peer, &init, &mut tried_auth, "session/load", params).await;
tokio::time::sleep(REPLAY_GRACE).await;
self.replaying.store(false, Ordering::Release);
if let Some(shown) = shown {
if loaded.is_ok() {
self.adopting_replay.store(false, Ordering::Release);
let changed = self.state.lock().conversation.end_turn(None);
self.mark_dirty(changed);
} else {
// A failed load may still have replayed part of it.
self.take_entries();
}
for entry in shown {
self.push_entry(entry);
}
}
match loaded {
Ok(v) => session = Some((id, v)),
// Not silently: a new session would lose the conversation and replace the saved id.
@@ -3484,7 +3524,7 @@ impl Task {
let kind = update.get("sessionUpdate").and_then(Value::as_str).unwrap_or_default();
// A subagent's title, settings and usage are its own, not the task's.
if self.state.lock().conversation.owner(session).is_some() {
if !SESSION_STATE.contains(&kind) && !self.replaying.load(Ordering::Acquire) {
if !SESSION_STATE.contains(&kind) && (!self.replaying.load(Ordering::Acquire) || self.adopts_replay()) {
self.apply(session, kind, update);
}
return;
@@ -3600,6 +3640,13 @@ impl Task {
return;
}
if self.replaying.load(Ordering::Acquire) {
if self.adopts_replay() {
if kind == "user_message_chunk" {
self.adopt_user_chunk(update);
} else {
self.apply(session, kind, update);
}
}
return;
}
// The first sign that the agent took the prompt: a stop sent before it is repeated.
@@ -3612,6 +3659,44 @@ impl Task {
self.apply(session, kind, update);
}
fn adopts_replay(&self) -> bool {
self.adopting_replay.load(Ordering::Acquire)
}
/// A user message of the adopted history: live turns show the user's own, so only a replay's count.
fn adopt_user_chunk(self: &Arc<Self>, update: &Value) {
let content = update.get("content");
let image = content.filter(|c| c.get("type").and_then(Value::as_str) == Some("image")).map(|c| AgentImage {
mime_type: c.get("mimeType").and_then(Value::as_str).unwrap_or_default().to_owned(),
data: c.get("data").and_then(Value::as_str).unwrap_or_default().to_owned(),
name: None,
});
let text = timeline::content_text(content).unwrap_or_default();
if text.is_empty() && image.is_none() {
return;
}
let changed = {
let mut s = self.state.lock();
let entries = &mut s.conversation.main.entries;
// The chunks of one message arrive in a row.
if let Some(AgentEntry::User { text: shown, images, .. }) = entries.last_mut() {
shown.push_str(&text);
images.extend(image);
entries.len() - 1
} else {
s.conversation.main.push(AgentEntry::User {
text,
attachments: Vec::new(),
images: image.into_iter().collect(),
sent_ms: None,
done_ms: None,
steered: false,
})
}
};
self.mark_dirty([(None, changed)]);
}
fn apply(self: &Arc<Self>, session: Option<&str>, kind: &str, update: &Value) {
// Output again: the retried request went through.
self.set_retry(None);
+53
Переглянути файл
@@ -222,6 +222,10 @@ impl Handler for FakeAgent {
}
"session/load" if self.load_error.is_some() => Err(self.load_error.clone().unwrap()),
"session/load" => {
self.update(
&session,
json!({"sessionUpdate": "user_message_chunk", "content": {"type": "text", "text": "earlier question"}}),
);
self.say(&session, "replayed history");
Ok(json!({}))
}
@@ -2542,6 +2546,55 @@ async fn renamed_titles_survive_updates_forks_and_restart() {
restored.shutdown();
}
#[tokio::test(flavor = "multi_thread")]
async fn a_session_without_history_takes_it_from_the_replay_before_the_first_message() {
let tmp = tempfile::tempdir().unwrap();
let path = {
let mgr = manager(tmp.path(), Arc::new(FakeAgent::default()));
let _down = ShutDown(&mgr);
let task = mgr
.create(NewAgentTask { provider: "claude".into(), prompt: text("again"), ..Default::default() })
.await
.unwrap();
wait_for(&mgr, &task.id, AgentStatus::Ready).await;
task_file(tmp.path(), &task.id)
};
let id = path.file_stem().unwrap().to_string_lossy().into_owned();
// As an imported session is saved: its session id, none of its history.
let mut file: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
file["entries"] = json!([]);
std::fs::write(&path, serde_json::to_vec(&file).unwrap()).unwrap();
{
let mgr = manager(tmp.path(), Arc::new(FakeAgent::default()));
let _down = ShutDown(&mgr);
mgr.prompt(&id, text("again"), false).await.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
wait_for(&mgr, &id, AgentStatus::Ready).await;
let entries = mgr.entries(&id, None).unwrap();
assert!(
matches!(&entries[0], AgentEntry::User { text, sent_ms: None, .. } if text == "earlier question"),
"{entries:?}"
);
assert_eq!(entries[1], AgentEntry::Agent { text: "replayed history".into() }, "{entries:?}");
assert!(
matches!(&entries[2], AgentEntry::User { text, sent_ms: Some(_), .. } if text == "again"),
"{entries:?}"
);
assert_eq!(entries.last(), Some(&AgentEntry::Agent { text: "second turn".into() }), "{entries:?}");
}
assert_eq!(saved_task(&path).unwrap().1, 4);
// From then on the task has its history: a later reload's replay is dropped again.
let mgr = manager(tmp.path(), Arc::new(FakeAgent::default()));
let _down = ShutDown(&mgr);
mgr.prompt(&id, text("again"), false).await.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
wait_for(&mgr, &id, AgentStatus::Ready).await;
let entries = mgr.entries(&id, None).unwrap();
assert_eq!(entries.len(), 6, "{entries:?}");
}
#[tokio::test(flavor = "multi_thread")]
async fn startup_warnings_survive_replay_and_restart_without_failing_the_task() {
let tmp = tempfile::tempdir().unwrap();