Skip to content

Commit d9262bc

Browse files
authored
Merge pull request #508 from OpenCoven/fix/502-terminal-stop-replay-20261117
fix(tui): finish event reconciliation with terminal replay guard (#502)
2 parents af88908 + ffc08e9 commit d9262bc

6 files changed

Lines changed: 1738 additions & 31 deletions

File tree

‎docs/tui-v2/dependency-review.md‎

Lines changed: 334 additions & 0 deletions
Large diffs are not rendered by default.

‎src-rust/crates/cli/src/main.rs‎

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,23 @@ async fn forward_scoped_events(
26022602
}
26032603
}
26042604

2605+
/// Replace the conversation with `replacement` and start a new session
2606+
/// generation, so nothing still queued from the replaced conversation's
2607+
/// worker is admitted to it (#408).
2608+
fn replace_conversation(
2609+
scope: &mut claurst_tui::application::SessionScope,
2610+
replacement: Vec<claurst_core::types::Message>,
2611+
messages: &mut Vec<claurst_core::types::Message>,
2612+
app: &mut claurst_tui::App,
2613+
session: &mut claurst_core::history::ConversationSession,
2614+
) {
2615+
scope.advance();
2616+
app.replace_messages(replacement.clone());
2617+
session.messages = replacement.clone();
2618+
*messages = replacement;
2619+
session.updated_at = chrono::Utc::now();
2620+
}
2621+
26052622
struct InteractiveQuery {
26062623
client: Arc<claurst_api::AnthropicClient>,
26072624
tools: Arc<Vec<Box<dyn claurst_tools::Tool>>>,
@@ -2743,6 +2760,7 @@ fn select_idle_compaction(app: &claurst_tui::App, query_running: bool) -> Option
27432760
|| app.rate_limit_recovery.is_pending()
27442761
|| app.file_injection_dialog.visible
27452762
|| app.file_injection_force
2763+
|| app.rewind_flow.visible
27462764
|| app.notifications.current_is_error()
27472765
{
27482766
return None;
@@ -3950,20 +3968,24 @@ async fn run_interactive(
39503968
match cli_result {
39513969
Some(CommandResult::Exit) => break 'main,
39523970
Some(CommandResult::ClearConversation) => {
3953-
session_scope.advance();
3954-
messages.clear();
3955-
app.replace_messages(Vec::new());
3956-
session.messages.clear();
3957-
session.updated_at = chrono::Utc::now();
3971+
replace_conversation(
3972+
&mut session_scope,
3973+
Vec::new(),
3974+
&mut messages,
3975+
&mut app,
3976+
&mut session,
3977+
);
39583978
app.status_message = Some("Conversation cleared.".to_string());
39593979
}
39603980
Some(CommandResult::SetMessages(new_msgs)) => {
3961-
session_scope.advance();
39623981
let removed = messages.len().saturating_sub(new_msgs.len());
3963-
messages = new_msgs.clone();
3964-
app.replace_messages(new_msgs);
3965-
session.messages = messages.clone();
3966-
session.updated_at = chrono::Utc::now();
3982+
replace_conversation(
3983+
&mut session_scope,
3984+
new_msgs,
3985+
&mut messages,
3986+
&mut app,
3987+
&mut session,
3988+
);
39673989
app.status_message = Some(format!(
39683990
"Rewound {} message{}.",
39693991
removed,
@@ -5224,12 +5246,16 @@ async fn run_interactive(
52245246
// Both bridge transports share the same literal-prompt launch path.
52255247
// Primary events observed above precede supplementary messages read here;
52265248
// each transport retains FIFO ordering. Never reinterpret remote slash text.
5227-
// A pending file-injection decision also holds remote prompts in their queue.
5228-
let file_injection_pending = app.file_injection_dialog.visible || app.file_injection_force;
5249+
// A pending file-injection decision also holds remote prompts in their
5250+
// queue, and so does an open rewind: a turn started under it would run
5251+
// from, and be saved with, the conversation the rewind removes (#408).
5252+
let remote_prompts_held = app.file_injection_dialog.visible
5253+
|| app.file_injection_force
5254+
|| app.rewind_flow.visible;
52295255
if let Some(content) = take_remote_prompt(
52305256
&mut pending_remote_prompts,
52315257
&mut remote_prompt_rx,
5232-
app.is_streaming || file_injection_pending,
5258+
app.is_streaming || remote_prompts_held,
52335259
current_query.is_some(),
52345260
) {
52355261
if rollback_unattempted_turn(
@@ -5596,7 +5622,8 @@ async fn run_interactive(
55965622
// existing conversation — the failed turn's prompt is never lost and
55975623
// never duplicated.
55985624
app.rate_limit_recovery.tick();
5599-
if current_query.is_none() && !app.is_streaming {
5625+
// An open rewind holds the retry, as it holds remote prompts (#408).
5626+
if current_query.is_none() && !app.is_streaming && !app.rewind_flow.visible {
56005627
if let Some(directive) = app.rate_limit_recovery.take_retry_directive() {
56015628
if !messages.is_empty() && goal_turn.as_ref().is_some_and(|owner| !owner.abandoned)
56025629
{
@@ -5606,6 +5633,10 @@ async fn run_interactive(
56065633
});
56075634
app.is_streaming = true;
56085635
app.streaming_text.clear();
5636+
// The retry pushes no prompt: it reruns a turn that never
5637+
// completed, so that turn's timer is still running, and
5638+
// the retry's completion spends it (#408).
5639+
app.turn_start.get_or_insert_with(std::time::Instant::now);
56095640

56105641
let ct = CancellationToken::new();
56115642
cancel = Some(ct.clone());
@@ -8332,6 +8363,9 @@ mod tests {
83328363
#[cfg(test)]
83338364
mod goal_dispatch_tests;
83348365

8366+
#[cfg(test)]
8367+
mod reconcile_tests;
8368+
83358369
#[cfg(test)]
83368370
mod session_save_tests;
83378371

0 commit comments

Comments
 (0)