Show read-only conversations when resume encounters an active writer (#43253)

## Why

Resuming a conversation that is open in another app fails with an active-writer error. Allow users to inspect its transcript and retry after closing it in the other app.

## What changed

- Fall back to a read-only history snapshot on active-writer conflicts during startup and session selection.
- Replace the composer with an explanatory notice, `R` to retry, exit shortcuts, and the configured transcript shortcut.
- Block input and freeze active command and MCP animations while viewing the snapshot. Preserve initial prompts and existing drafts for a successful retry.
- Keep the subscription when retrying the same thread and preserve read-only state across thread switches and reconnects.

## Testing

Add regression tests for conflict detection, history viewing, draft preservation, same-thread retry without unsubscribing, exit shortcuts, and notice and frozen-tool rendering.

GitOrigin-RevId: fa864cc73143cad9139c22e33b7958569eec20cf
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-09-06 17:27:44 +00:00
1 parent a9896da3fe
commit 4aec23384e
29 files changed
+869 -109

No files matched your search

@@ -217,6 +217,7 @@ impl ChatWidget {
thread_rename_block_message: None,
active_side_conversation: false,
blocks_direct_input: false,
external_writer_view: false,
misalignment_policy_violation: None,
normal_placeholder_text: placeholder,
side_placeholder_text: side_placeholder,
+6 -1
View File
@@ -265,7 +265,12 @@ impl ChatWidget {
mut collaboration_mode: CollaborationModeMask,
) {
if self.blocks_direct_input {
self.add_error_message(PARENT_OWNED_INPUT_MESSAGE.to_string());
self.add_error_message(if self.external_writer_view {
"This thread is open elsewhere. Close it there and retry resume to continue."
.to_string()
} else {
PARENT_OWNED_INPUT_MESSAGE.to_string()
});
return;
}
if collaboration_mode.mode == Some(ModeKind::Plan)
+4 -1
View File
@@ -271,7 +271,7 @@ impl ChatWidget {
}
pub(crate) fn can_launch_external_editor(&self) -> bool {
self.bottom_pane.can_launch_external_editor()
!self.external_writer_view && self.bottom_pane.can_launch_external_editor()
}
pub(crate) fn can_run_ctrl_l_clear_now(&mut self) -> bool {
@@ -512,6 +512,9 @@ impl ChatWidget {
}
pub(crate) fn handle_paste(&mut self, text: String) {
if self.external_writer_view {
return;
}
self.bottom_pane.handle_paste(text);
}
+3
View File
@@ -67,6 +67,9 @@ impl ChatWidget {
}
pub(crate) fn handle_disconnected_key(&mut self, key: KeyEvent) {
if self.external_writer_view {
return;
}
if self.handle_question_key(key) {
return;
}
+123 -5
View File
@@ -3,9 +3,121 @@
use super::transcript::ActiveCellLayoutCache;
use super::transcript::ActiveCellLayoutCacheKey;
use super::*;
use crate::render::RectExt;
use crate::terminal_hyperlinks::HyperlinkParagraph;
use crate::wrapping::RtOptions;
use crate::wrapping::word_wrap_lines;
use ratatui::style::Styled as _;
use ratatui::text::Span;
use ratatui::widgets::Block;
use std::cell::Cell;
struct ExternalWriterNotice {
transcript_hint: Option<crate::key_hint::ShortcutHint>,
}
impl Renderable for ExternalWriterNotice {
fn render(&self, area: Rect, buf: &mut Buffer) {
let content_width = area.width.saturating_sub(/*rhs*/ 4);
let card_lines = self.card_lines(content_width);
let card_height = (card_lines.len() as u16).saturating_add(/*rhs*/ 2);
let card = Rect::new(area.x, area.y, area.width, card_height.min(area.height));
Widget::render(
Block::default().style(crate::style::user_message_style()),
card,
buf,
);
let content = card.inset(Insets::tlbr(
/*top*/ 1, /*left*/ 2, /*bottom*/ 1, /*right*/ 2,
));
Renderable::render(&Paragraph::new(card_lines), content, buf);
let footer_y = card.bottom();
if footer_y < area.bottom() {
let footer = Rect::new(
area.x.saturating_add(/*rhs*/ 2),
footer_y,
area.width.saturating_sub(/*rhs*/ 2),
area.bottom().saturating_sub(footer_y),
);
Renderable::render(
&Paragraph::new(self.footer_lines(footer.width)),
footer,
buf,
);
}
}
fn desired_height(&self, width: u16) -> u16 {
(self.card_lines(width.saturating_sub(/*rhs*/ 4)).len() as u16)
.saturating_add(/*rhs*/ 2)
.saturating_add(self.footer_lines(width.saturating_sub(/*rhs*/ 2)).len() as u16)
}
}
impl ExternalWriterNotice {
fn card_lines(&self, width: u16) -> Vec<Line<'static>> {
let title: Line<'static> = vec![
"🔒".into(),
" ".into(),
"This conversation is open in another app".bold(),
]
.into();
let retry: Line<'static> = vec![
Span::styled("R", crate::style::accent_style()),
" to Retry".into(),
]
.into();
let mut lines = word_wrap_lines(&[title], usize::from(width));
if lines.len() == 1 && lines[0].width() + retry.width() + 2 <= usize::from(width) {
let gap = usize::from(width) - lines[0].width() - retry.width();
lines[0].spans.push(" ".repeat(gap).into());
lines[0].spans.extend(retry.spans);
} else {
lines.push(retry);
}
lines.extend(word_wrap_lines(
&[Line::from(
"Close it there and press R to continue here.".dim(),
)],
RtOptions::new(usize::from(width))
.initial_indent(" ".into())
.subsequent_indent(" ".into()),
));
lines
}
fn footer_lines(&self, width: u16) -> Vec<Line<'static>> {
let mut items = vec![
("r".to_string(), "retry".to_string()),
(
format!(
"{}/{}/{}",
crate::key_hint::plain(KeyCode::Esc).display_label(),
crate::key_hint::ctrl(KeyCode::Char('c')).display_label(),
crate::key_hint::plain(KeyCode::Char('q')).display_label(),
)
.replace(" + ", "+"),
"exit".to_string(),
),
];
if let Some(hint) = self.transcript_hint {
items.push((
hint.display_label().replace(" + ", "+"),
"transcript".to_string(),
));
}
let mut spans = vec![" ".set_style(crate::style::footer_hint_label_style())];
for (idx, (key, label)) in items.into_iter().enumerate() {
if idx > 0 {
spans.push(" ".set_style(crate::style::footer_hint_label_style()));
}
spans.push(key.set_style(crate::style::footer_hint_key_style()));
spans.push(format!(" {label}").set_style(crate::style::footer_hint_label_style()));
}
word_wrap_lines(&[Line::from(spans)], usize::from(width))
}
}
impl ChatWidget {
pub(crate) fn as_renderable(&self) -> RenderableItem<'_> {
if self
@@ -68,13 +180,19 @@ impl ChatWidget {
})),
);
}
flex.push(
/*flex*/ 0,
let bottom = if self.external_writer_view && !self.bottom_pane.has_active_view() {
RenderableItem::Owned(Box::new(ExternalWriterNotice {
transcript_hint: self.bottom_pane.transcript_shortcut_hint(),
}))
} else {
self.bottom_pane
.as_renderable_with_composer_right_reserve(active_cell_right_reserve)
.inset(Insets::tlbr(
/*top*/ 1, /*left*/ 0, /*bottom*/ 0, /*right*/ 0,
)),
};
flex.push(
/*flex*/ 0,
bottom.inset(Insets::tlbr(
/*top*/ 1, /*left*/ 0, /*bottom*/ 0, /*right*/ 0,
)),
);
RenderableItem::Owned(Box::new(flex))
}
@@ -75,6 +75,60 @@ fn contains_text(buffer: &Buffer, text: &str) -> bool {
})
}
#[tokio::test]
async fn external_writer_view_shows_notice_instead_of_composer() {
let (mut widget, _sender, _events, _operations) = make_chatwidget_manual_with_sender().await;
widget.show_external_writer_thread();
let frame = crate::terminal_palette::with_test_default_colors(
crate::terminal_probe::DefaultColors {
fg: (230, 230, 230),
bg: (20, 20, 20),
},
|| render_frame(&widget, /*width*/ 60),
);
let rows: Vec<String> = frame
.content
.chunks(usize::from(frame.area.width))
.map(|row| row.iter().map(ratatui::buffer::Cell::symbol).collect())
.collect();
insta::assert_snapshot!(
rows.iter()
.map(|row| row.trim_end())
.collect::<Vec<_>>()
.join("\n")
);
assert_ne!(frame[(0, 1)].bg, ratatui::style::Color::Reset);
assert_eq!(frame[(0, 1)].bg, frame[(59, 4)].bg);
assert!(
!frame[(3, 5)]
.style()
.add_modifier
.intersects(ratatui::style::Modifier::DIM | ratatui::style::Modifier::BOLD)
);
assert!(
frame[(5, 5)]
.style()
.add_modifier
.contains(ratatui::style::Modifier::DIM)
);
assert!(!widget.bottom_pane.composer_input_enabled());
}
#[tokio::test]
async fn external_writer_notice_uses_current_transcript_shortcut() {
let (mut widget, _sender, _events, _operations) = make_chatwidget_manual_with_sender().await;
let mut keymap = crate::keymap::RuntimeKeymap::defaults();
keymap.app.open_transcript = vec![crate::key_hint::ctrl(KeyCode::Char('k'))];
widget.bottom_pane.set_keymap_bindings(&keymap);
widget.show_external_writer_thread();
assert!(contains_text(
&render_frame(&widget, /*width*/ 80),
"ctrl+k transcript"
));
}
#[test]
fn active_transcript_preserves_clipped_markdown_hyperlinks() {
let cell = history_cell::AgentMarkdownCell::new(
@@ -0,0 +1,10 @@
---
source: tui/src/chatwidget/rendering_tests.rs
expression: "rows.iter().map(|row| row.trim_end()).collect::<Vec<_>>().join(\"\\n\")"
---
🔒 This conversation is open in another app R to Retry
Close it there and press R to continue here.
r retry esc/ctrl+c/q exit ctrl+t transcript
@@ -1,6 +1,51 @@
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn external_writer_snapshot_freezes_active_command_and_mcp_rows() {
let mut rendered = Vec::new();
for active_mcp in [false, true] {
let (mut chat, _events, _operations) =
make_chatwidget_manual(/*model_override*/ None).await;
chat.on_task_started();
if active_mcp {
chat.transcript.active_cell = Some(Box::new(history_cell::new_active_mcp_tool_call(
"mcp-running".to_string(),
McpInvocation {
server: "server".to_string(),
tool: "tool".to_string(),
arguments: None,
},
/*animations_enabled*/ true,
)));
} else {
begin_exec(&mut chat, "call-running", "sleep 5");
}
chat.show_external_writer_thread();
assert!(!chat.is_task_running_for_test());
let cell = chat
.transcript
.active_cell
.as_ref()
.expect("active tool row");
rendered.push(lines_to_single_string(&cell.display_lines(/*width*/ 80)));
if active_mcp {
assert!(cell.transcript_animation_tick().is_none());
} else {
assert!(
!cell
.as_any()
.downcast_ref::<ExecCell>()
.expect("active command row")
.animations_enabled()
);
}
}
insta::assert_snapshot!(rendered.join("\n---\n"));
}
#[tokio::test]
async fn replayed_command_completion_preserves_tracking_without_duplicate_starts() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
@@ -0,0 +1,8 @@
---
source: tui/src/chatwidget/tests/exec_flow.rs
expression: "rendered.join(\"\\n---\\n\")"
---
• Running sleep 5
---
• Calling server.tool()