Retry safety-buffered turns on a forked thread (#33207)

## Why

Retrying a safety-buffered turn by rolling back the active thread rewrites the
source conversation and requires the TUI to repair its local transcript state.

## What changed

- Interrupt the buffered turn, then fork after the preceding completed turn and
  submit the prompt on the new thread with the faster model and low reasoning
  effort.
- Start a new thread when the buffered request is the conversation's first turn.
- Preserve committed steering input in the retry, leave the source thread
  intact, and restore the prompt alongside any existing draft if branching
  fails.
- Do not offer the retry action for side conversations or stale and in-progress
  turns.

## Testing

Added coverage for retries with prior context, first-turn retries, committed
steering input, branch failures, existing composer drafts and attachments, and
side conversations.

GitOrigin-RevId: adb49da8236e9212ef0082e033e6f715986b5cd9
This commit is contained in:
Felipe Coury
2026-07-15 02:03:46 +00:00
committed by copyberry
parent 77a3f4e80f
commit 9cddda7556
21 changed files with 897 additions and 586 deletions
+1 -67
View File
@@ -1,30 +1,14 @@
//! Transcript and active-cell bookkeeping for `ChatWidget`.
use super::HistoryCell;
use super::MAX_AGENT_COPY_HISTORY;
#[derive(Debug)]
pub(super) struct AgentTurnMarkdown {
pub(super) user_turn_count: usize,
pub(super) markdown: String,
}
#[derive(Default)]
pub(super) struct TranscriptState {
pub(super) active_cell: Option<Box<dyn HistoryCell>>,
/// Monotonic-ish counter used to invalidate transcript overlay caching.
pub(super) active_cell_revision: u64,
/// Raw markdown of the most recently completed agent response that
/// survived any local thread rollback.
/// Raw markdown of the most recently completed agent response.
pub(super) last_agent_markdown: Option<String>,
/// Copyable agent responses keyed by the number of visible user turns at
/// the time the response completed.
pub(super) agent_turn_markdowns: Vec<AgentTurnMarkdown>,
/// Number of user turns currently reflected in the visible transcript.
pub(super) visible_user_turn_count: usize,
/// True when rollback discarded the requested copy source because it was
/// older than the retained copy history.
pub(super) copy_history_evicted_by_rollback: bool,
/// Raw markdown of the most recently completed proposed plan.
pub(super) latest_proposed_plan_markdown: Option<String>,
/// Whether this turn already produced a copyable response.
@@ -61,48 +45,12 @@ impl TranscriptState {
}
pub(super) fn record_agent_markdown(&mut self, markdown: String) {
match self.agent_turn_markdowns.last_mut() {
Some(entry) if entry.user_turn_count == self.visible_user_turn_count => {
entry.markdown = markdown.clone();
}
_ => {
self.agent_turn_markdowns.push(AgentTurnMarkdown {
user_turn_count: self.visible_user_turn_count,
markdown: markdown.clone(),
});
if self.agent_turn_markdowns.len() > MAX_AGENT_COPY_HISTORY {
self.agent_turn_markdowns.remove(0);
}
}
}
self.last_agent_markdown = Some(markdown);
self.copy_history_evicted_by_rollback = false;
self.saw_copy_source_this_turn = true;
}
pub(super) fn record_visible_user_turn(&mut self) {
self.visible_user_turn_count = self.visible_user_turn_count.saturating_add(1);
}
pub(super) fn reset_copy_history(&mut self) {
self.last_agent_markdown = None;
self.agent_turn_markdowns.clear();
self.visible_user_turn_count = 0;
self.copy_history_evicted_by_rollback = false;
self.saw_copy_source_this_turn = false;
}
pub(super) fn truncate_copy_history_to_user_turn_count(&mut self, user_turn_count: usize) {
self.visible_user_turn_count = user_turn_count;
let had_copy_history = !self.agent_turn_markdowns.is_empty();
self.agent_turn_markdowns
.retain(|entry| entry.user_turn_count <= user_turn_count);
self.last_agent_markdown = self
.agent_turn_markdowns
.last()
.map(|entry| entry.markdown.clone());
self.copy_history_evicted_by_rollback =
had_copy_history && self.last_agent_markdown.is_none();
self.saw_copy_source_this_turn = false;
}
@@ -134,18 +82,4 @@ mod tests {
assert_eq!(state.active_cell_revision, 0);
}
#[test]
fn copy_history_tracks_latest_visible_turn() {
let mut state = TranscriptState::default();
state.record_visible_user_turn();
state.record_agent_markdown("first".to_string());
state.record_visible_user_turn();
state.record_agent_markdown("second".to_string());
state.truncate_copy_history_to_user_turn_count(/*user_turn_count*/ 1);
assert_eq!(state.last_agent_markdown.as_deref(), Some("first"));
assert!(!state.copy_history_evicted_by_rollback);
}
}