Fix SQLite recovery and preserve provider conversation context

Persist exact provider history and archived tool results independently of
terminal scrollback, and retain them when active run checkpoints are cleared.
Keep conversation and session saving independent of automatic reopening.

Deduplicate only committed session snapshots, retry transient SQLite write
contention, and enqueue conversation updates before writer shutdown. Recover
healthy startup data independently and protect unread records by disabling
writes with a persistent warning after an incomplete restore. Retain local
transcript pane conversation links across restarts.

Add regression coverage for SQLite failures, context round trips, legacy
fallbacks, and transcript snapshots.
This commit is contained in:
2026-09-20 16:40:44 -05:00
parent 316f53471b
commit d7fbc09bcf
21 changed files with 714 additions and 185 deletions
+22
View File
@@ -33,6 +33,7 @@ use input_context::{input_context_for_request, parse_context_attachments};
use itertools::Itertools;
use parking_lot::FairMutex;
use pending_response_streams::PendingResponseStreams;
use persistence::model::ProviderConversationContext;
use session_sharing_protocol::common::ParticipantId;
use settings::Setting;
pub use slash_command::*;
@@ -6519,9 +6520,16 @@ impl BlocklistAIController {
Err(error) => return Err(error),
};
snapshot.queued_follow_ups = queued_follow_ups;
let context = ProviderConversationContext {
messages: snapshot.run.transcript()[snapshot.persistence_offset..].to_vec(),
tool_result_archive: snapshot.run.tool_result_archive().to_vec(),
};
let json = serde_json::to_string(&snapshot)
.map_err(|error| format!("failed to serialize active provider run: {error}"))?;
BlocklistAIHistoryModel::handle(ctx).update(ctx, |history_model, ctx| {
if let Some(conversation) = history_model.conversation_mut(&conversation_id) {
conversation.set_provider_context(context);
}
history_model
.persist_active_provider_run_json(conversation_id, Some(json), ctx)
.map_err(|error| format!("failed to persist active provider run: {error:?}"))
@@ -8290,6 +8298,20 @@ impl BlocklistAIController {
let offset = run.persistence_offset.min(transcript.len());
*messages_sent = transcript[offset..].to_vec();
}
// Retain the exact context before terminal projection clears the active checkpoint.
// UI task messages are a lossy representation of tool/reasoning messages.
let transcript = run.coordinator.run().transcript();
let offset = run.persistence_offset.min(transcript.len());
let context = ProviderConversationContext {
messages: transcript[offset..].to_vec(),
tool_result_archive: run.coordinator.run().tool_result_archive().to_vec(),
};
BlocklistAIHistoryModel::handle(ctx).update(ctx, |history, ctx| {
if let Some(conversation) = history.conversation_mut(&conversation_id) {
conversation.set_provider_context(context);
conversation.write_updated_conversation_state(ctx);
}
});
let aggregate_usage = run.coordinator.run().usage().clone();
let events = match run.projector.finish(&outcome, &aggregate_usage) {
Ok(events) => events,
+3
View File
@@ -1703,6 +1703,7 @@ impl BlocklistAIHistoryModel {
let conversation_data = AgentConversationData {
agent_backend: source_conversation.agent_backend().for_fork(),
provider_context: None,
active_provider_run_json: None,
local_agent_groups: Vec::new(),
tool_titles: source_conversation.tool_titles().clone(),
@@ -1870,6 +1871,7 @@ impl BlocklistAIHistoryModel {
// be recomputed based on the retained exchanges in a follow-up.
let conversation_data = AgentConversationData {
agent_backend: conversation.agent_backend().for_fork(),
provider_context: None,
active_provider_run_json: None,
local_agent_groups: Vec::new(),
tool_titles: conversation.tool_titles().clone(),
@@ -2898,6 +2900,7 @@ fn merged_remote_child_placeholder_conversation_data(
cloud_conversation: &AIConversation,
) -> AgentConversationData {
AgentConversationData {
provider_context: None,
// Placeholder authoritative.
agent_backend: placeholder.agent_backend().clone(),
@@ -83,7 +83,14 @@ pub fn convert_persisted_conversation_to_ai_conversation_with_metadata(
}
};
let conversation_data = serde_json::from_str::<AgentConversationData>(&conversation_data).ok();
let conversation_data = match serde_json::from_str::<AgentConversationData>(&conversation_data)
{
Ok(data) => Some(data),
Err(error) => {
log::error!("Failed to restore conversation {conversation_id} metadata: {error}");
return None;
}
};
// Local-DB restore: an empty `agent_tasks` row is the normal shape of a
// child conversation persisted before its first server response, so