From ffc02793523941299f42413dca3778d05425df28 Mon Sep 17 00:00:00 2001 From: Abeautifulsnow Date: Wed, 8 Jul 2026 18:44:13 +0800 Subject: [PATCH] fix(ai): make stop button actually cancel the agent stream The stop button called aiCancelStream -> notify_one(), but the four stream_*_with_tools SSE loops only `break`ed and returned Ok(token_usage) when cancelled. run_agent_loop treated that as a normal completed turn and kept going (often into contract-repair or the next turn), so isGenerating stayed true and a single click did not stop the agent. Return Err(AGENT_CANCELLED_ERROR) from the cancel branch so the existing Err-cancel match arm sets loop_exit = Cancelled and stops the loop cleanly, preserving whatever was streamed so far. Additional: - Promote AGENT_CANCELLED_ERROR to a shared `pub const` in ai.rs; agent_loop now references ai::AGENT_CANCELLED_ERROR. - Check cancellation before executing requested tools, so a stop landing between stream-end and tool execution is honored. - Wire cancellation into the text-only fallback (previously `_cancelled`, ignored) via tokio::select! racing ai::complete against cancelled.notified(). Known limitation left: in-flight tool execution (e.g. execute_query) is still not interruptible mid-call. --- crates/dbx-core/src/agent_loop.rs | 30 +++++++++++++++++++++++++----- crates/dbx-core/src/ai.rs | 24 ++++++++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/crates/dbx-core/src/agent_loop.rs b/crates/dbx-core/src/agent_loop.rs index 5f3d1d7d65..f0a1fc4b90 100644 --- a/crates/dbx-core/src/agent_loop.rs +++ b/crates/dbx-core/src/agent_loop.rs @@ -16,7 +16,6 @@ use crate::token_usage::TokenUsage; /// Maximum number of agent loop turns to prevent infinite loops. const MAX_AGENT_TURNS: u32 = 30; -const AGENT_CANCELLED_ERROR: &str = "Agent loop cancelled"; const MAX_TOOL_RESULT_CONTEXT_CHARS: usize = 12_000; const TOOL_RESULT_HEAD_CHARS: usize = 4_000; const TOOL_RESULT_TAIL_CHARS: usize = 4_000; @@ -254,7 +253,7 @@ pub async fn run_agent_loop( } break; } - Err(err) if err == AGENT_CANCELLED_ERROR => { + Err(err) if err == ai::AGENT_CANCELLED_ERROR => { final_text = take_text(&accumulated_text); loop_exit = LoopExit::Cancelled; break; @@ -321,6 +320,14 @@ pub async fn run_agent_loop( } } + // Honor a cancellation that arrived after the stream finished but before we + // run the requested tools; otherwise a long execute_query would keep running. + if cancelled.notified().now_or_never().is_some() { + final_text = accumulated_text; + loop_exit = LoopExit::Cancelled; + break; + } + // Execute each tool call // Emit all ToolCallStart events first for tc in &collected_tool_calls { @@ -631,7 +638,7 @@ async fn stream_with_tools( ) -> Result<(Vec, Option), String> { // Return early if the user cancelled before the LLM call started. if cancelled.notified().now_or_never().is_some() { - return Err(AGENT_CANCELLED_ERROR.to_string()); + return Err(ai::AGENT_CANCELLED_ERROR.to_string()); } ai::stream_with_tools(config, request, session_id, tools, cancelled, on_chunk).await @@ -665,7 +672,7 @@ async fn run_agent_loop_text_only( messages: &[AiMessage], agent_ctx: &AgentLoopContext, on_event: impl Fn(AgentEvent) + Send + Sync + 'static, - _cancelled: &Notify, + cancelled: &Notify, max_tokens: Option, task_contract: Option<&AiTaskContract>, ) -> Result { @@ -673,6 +680,12 @@ async fn run_agent_loop_text_only( // even without tool access. let enriched_prompt = build_schema_prompt(agent_ctx, system_prompt).await; + // Honor a cancellation requested while loading schema context. + if cancelled.notified().now_or_never().is_some() { + on_event(AgentEvent::AgentEnd { input_tokens: None, output_tokens: None }); + return Ok("Agent run was cancelled before producing output.".to_string()); + } + let mut request = AiCompletionRequest { config: config.clone(), system_prompt: enriched_prompt, @@ -683,7 +696,14 @@ async fn run_agent_loop_text_only( for attempt in 0..=MAX_CONTRACT_REPAIR_ATTEMPTS { // Use non-streaming completions so contract repair can suppress incomplete drafts. - let result = ai::complete(&request).await?; + // Race the (non-cancellable) HTTP call against cancellation so Stop still works. + let result = tokio::select! { + result = ai::complete(&request) => result?, + _ = cancelled.notified() => { + on_event(AgentEvent::AgentEnd { input_tokens: None, output_tokens: None }); + return Ok("Agent run was cancelled before producing output.".to_string()); + } + }; match validate_final_answer(task_contract, &result) { FinalAnswerCheck::Satisfied => { on_event(AgentEvent::TextDelta { delta: result.clone() }); diff --git a/crates/dbx-core/src/ai.rs b/crates/dbx-core/src/ai.rs index f2d68be46f..bc3dc6ffdb 100644 --- a/crates/dbx-core/src/ai.rs +++ b/crates/dbx-core/src/ai.rs @@ -34,6 +34,14 @@ pub async fn unregister_stream(session_id: &str) { AI_STREAMS.write().await.remove(session_id); } +/// Error returned by streaming functions when the user cancels mid-stream. +/// +/// `run_agent_loop` matches on this exact string to distinguish a cancellation +/// from a normal completion and stop the loop cleanly. Streaming functions MUST +/// return this (not `Ok`) when `cancelled` fires, otherwise the agent loop +/// treats the truncated turn as a normal completion and keeps going. +pub const AGENT_CANCELLED_ERROR: &str = "Agent loop cancelled"; + // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- @@ -1909,7 +1917,9 @@ async fn stream_claude_with_tools( if finished { break; } } - _ = cancelled.notified() => { break; } + _ = cancelled.notified() => { + return Err(AGENT_CANCELLED_ERROR.to_string()); + } } } @@ -2049,7 +2059,9 @@ async fn stream_openai_with_tools( if finished { break; } } - _ = cancelled.notified() => { break; } + _ = cancelled.notified() => { + return Err(AGENT_CANCELLED_ERROR.to_string()); + } } } @@ -2175,7 +2187,9 @@ async fn stream_responses_with_tools( if finished { break; } } - _ = cancelled.notified() => { break; } + _ = cancelled.notified() => { + return Err(AGENT_CANCELLED_ERROR.to_string()); + } } } @@ -2321,7 +2335,9 @@ async fn stream_gemini_with_tools( } } } - _ = cancelled.notified() => { break; } + _ = cancelled.notified() => { + return Err(AGENT_CANCELLED_ERROR.to_string()); + } } }