Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src-tauri/src/acp/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ pub(crate) async fn handle_event(
};
let conversation_id = state_arc.read().await.conversation_id;
if let Some(cid) = conversation_id {
conversation_service::update_external_id(db_conn, cid, session_id.clone())
.await?;
conversation_service::update_external_id_if_missing(
db_conn,
cid,
session_id.clone(),
)
.await?;
}
Ok(())
}
Expand Down Expand Up @@ -366,6 +370,40 @@ mod tests {
assert_eq!(reloaded.external_id.as_deref(), Some("ext-99"));
}

#[tokio::test]
async fn handle_event_does_not_overwrite_existing_external_id() {
let db = test_helpers::fresh_in_memory_db().await;
let folder_id = test_helpers::seed_folder(&db, "/tmp/test-existing-ext").await;
let conv =
conversation_service::create(&db.conn, folder_id, AgentType::ClaudeCode, None, None)
.await
.unwrap();
conversation_service::update_external_id(&db.conn, conv.id, "ext-existing".into())
.await
.unwrap();

let mgr = ConnectionManager::new();
{
let mut map = mgr.connections.lock().await;
map.insert(
"c1".to_string(),
fake_connection_with_state("c1", Some(conv.id)),
);
}
let env = EventEnvelope {
seq: 1,
connection_id: "c1".to_string(),
payload: AcpEvent::SessionStarted {
session_id: "ext-new".into(),
},
};
handle_event(&db.conn, &mgr, &env).await.unwrap();
let reloaded = conversation_service::get_by_id(&db.conn, conv.id)
.await
.unwrap();
assert_eq!(reloaded.external_id.as_deref(), Some("ext-existing"));
}

#[tokio::test]
async fn handle_event_is_noop_when_no_conversation_bound() {
let db = test_helpers::fresh_in_memory_db().await;
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/src/db/service/conversation_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ pub async fn update_external_id(
Ok(())
}

pub async fn update_external_id_if_missing(
conn: &DatabaseConnection,
conversation_id: i32,
external_id: String,
) -> Result<bool, DbError> {
use sea_orm::sea_query::Expr;
let result = conversation::Entity::update_many()
.col_expr(conversation::Column::ExternalId, Expr::value(external_id))
.col_expr(conversation::Column::UpdatedAt, Expr::value(Utc::now()))
.filter(conversation::Column::Id.eq(conversation_id))
.filter(conversation::Column::DeletedAt.is_null())
.filter(conversation::Column::ExternalId.is_null())
.exec(conn)
.await?;
Ok(result.rows_affected > 0)
}

pub async fn soft_delete(conn: &DatabaseConnection, conversation_id: i32) -> Result<(), DbError> {
let conv = conversation::Entity::find_by_id(conversation_id)
.filter(conversation::Column::DeletedAt.is_null())
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/parsers/gemini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,11 @@ fn group_into_turns(messages: Vec<UnifiedMessage>) -> Vec<MessageTurn> {
mod tests {
use super::resolve_gemini_base_dir_from;
use super::GeminiParser;
use crate::models::ContentBlock;
use crate::parsers::AgentParser;
use chrono::{DateTime, Utc};
use crate::models::{ContentBlock, MessageRole, UnifiedMessage};
use crate::parsers::{
stable_user_anchor_id_from_message, stable_user_anchor_id_from_parts, AgentParser,
};
use std::env;
use std::fs;
use std::path::PathBuf;
Expand Down
8 changes: 7 additions & 1 deletion src/components/conversations/conversation-detail-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const ConversationTabView = memo(function ConversationTabView({
setExternalId,
setLiveMessage,
setPendingCleanup,
setRecoveryConversationId,
setSyncState,
} = useConversationRuntime()

Expand Down Expand Up @@ -275,6 +276,10 @@ const ConversationTabView = memo(function ConversationTabView({
}
return buildNewConversationDraftStorageKey()
}, [dbConversationId])

useEffect(() => {
setRecoveryConversationId(effectiveConversationId, dbConversationId)
}, [dbConversationId, effectiveConversationId, setRecoveryConversationId])
// Use the per-tab workingDir (derived from the tab's own folderId by the
// parent) rather than the active folder's path — otherwise switching tabs
// briefly exposes the previous folder's path to the ACP auto-connect
Expand Down Expand Up @@ -361,7 +366,8 @@ const ConversationTabView = memo(function ConversationTabView({
prevConnStatusRef.current = connStatus
if (!wasPrompting || connStatus === "prompting") return

// Turn completed — promote liveMessage + optimisticTurns to localTurns
// Turn ended (success, cancel, or error) — promote liveMessage +
// optimisticTurns so reload/refetch does not drop the user turn.
completeTurn(effectiveConversationId)

// Cancel previous metadata sync (handles rapid consecutive turns)
Expand Down
Loading