Skip to content

Commit 8149527

Browse files
committed
fix(tui): avoid repeating final answers after compaction
1 parent 0fef9c4 commit 8149527

9 files changed

Lines changed: 422 additions & 5 deletions

File tree

‎crates/jcode-tui/src/tui/app.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ mod remote;
102102
mod remote_header_hint;
103103
mod remote_notifications;
104104
mod replay;
105+
mod response_output;
105106
pub(crate) mod run_shell;
106107
mod runtime_memory;
107108
mod shortcut_hints;
@@ -885,6 +886,7 @@ enum ColdCacheWarningTrigger {
885886
#[derive(Clone, Debug, Default)]
886887
struct StreamingProgress {
887888
streaming_text: String,
889+
response_output: response_output::ResponseOutput,
888890
// Live token usage (per turn)
889891
streaming_input_tokens: u64,
890892
streaming_output_tokens: u64,
@@ -2006,6 +2008,7 @@ impl App {
20062008
&mut self,
20072009
signature: KvCacheRequestSignature,
20082010
) {
2011+
self.streaming.response_output.reset();
20092012
let turn_number = self
20102013
.display_messages
20112014
.iter()

‎crates/jcode-tui/src/tui/app/input.rs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,13 @@ impl App {
15591559
/// guardrail circuit breaker first, then tries auto-poke and overnight
15601560
/// poke scheduling. Returns true when a follow-up was queued.
15611561
pub(super) fn schedule_turn_end_followups(&mut self) -> bool {
1562+
self.schedule_turn_end_followups_after_response(false)
1563+
}
1564+
1565+
pub(super) fn schedule_turn_end_followups_after_response(
1566+
&mut self,
1567+
final_response_delivered: bool,
1568+
) -> bool {
15621569
if self.guardrail_stops_exhausted_at_turn_end() {
15631570
self.stop_auto_continuation_after_guardrail();
15641571
return false;
@@ -1574,11 +1581,19 @@ impl App {
15741581
if self.has_pending_user_followup() {
15751582
return false;
15761583
}
1577-
self.schedule_auto_poke_followup_if_needed()
1584+
self.schedule_auto_poke_followup_after_response(final_response_delivered)
15781585
|| self.schedule_overnight_poke_followup_if_needed()
15791586
}
15801587

1588+
#[cfg(test)]
15811589
pub(super) fn schedule_auto_poke_followup_if_needed(&mut self) -> bool {
1590+
self.schedule_auto_poke_followup_after_response(false)
1591+
}
1592+
1593+
fn schedule_auto_poke_followup_after_response(
1594+
&mut self,
1595+
final_response_delivered: bool,
1596+
) -> bool {
15821597
// Completion is visible even with /poke off, and never needs another
15831598
// model turn merely to show the plan's remaining weaknesses.
15841599
self.refresh_completed_todo_report(true);
@@ -1784,6 +1799,15 @@ impl App {
17841799
if !self.todo_final_response_requested {
17851800
self.todo_final_response_requested = true;
17861801
self.final_response_todo_fingerprint = todo_fingerprint;
1802+
// The ordinary answer already wrapped up this work. Keep the
1803+
// completed fingerprint latched, but do not buy another turn
1804+
// merely to repeat it. Quality gates above still run normally.
1805+
if final_response_delivered {
1806+
crate::logging::info(
1807+
"AUTO_POKE_DECISION action=idle reason=final_response_already_delivered",
1808+
);
1809+
return false;
1810+
}
17871811
self.queued_messages
17881812
.push(crate::todo::TODO_FINAL_RESPONSE_CONTINUATION_MESSAGE.to_string());
17891813
self.pending_queued_dispatch = true;
@@ -1903,6 +1927,7 @@ impl App {
19031927
}
19041928

19051929
pub(super) fn schedule_queued_dispatch_after_interrupt(&mut self) {
1930+
self.streaming.response_output.reset();
19061931
if self.has_queued_followups() {
19071932
self.pending_queued_dispatch = true;
19081933
}
@@ -3776,6 +3801,7 @@ impl App {
37763801
/// replayed from the top, so everything from the aborted attempt must
37773802
/// disappear or the replay would render duplicated output.
37783803
pub(super) fn rollback_streaming_attempt(&mut self) {
3804+
self.streaming.response_output.reset();
37793805
self.stream_buffer.clear();
37803806
self.clear_streaming_render_state();
37813807
self.streaming_tool_calls.clear();

‎crates/jcode-tui/src/tui/app/local.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,9 @@ pub(super) fn finish_turn(app: &mut App) {
654654
app.thinking_prefix_emitted = false;
655655
app.thinking_buffer.clear();
656656
app.note_runtime_memory_event_force("turn_completed", "local_turn_finished");
657-
let followup_scheduled = app.schedule_turn_end_followups();
657+
let followup_scheduled = app.schedule_turn_end_followups_after_response(
658+
app.last_stream_error.is_none() && app.streaming.response_output.has_final_response(),
659+
);
658660
if !followup_scheduled {
659661
app.clear_visible_turn_started();
660662
if !app.pending_queued_dispatch && app.queued_messages.is_empty() {

‎crates/jcode-tui/src/tui/app/misc_ui.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl App {
121121
}
122122

123123
pub(super) fn reset_streaming_tps(&mut self) {
124+
self.streaming.response_output.reset();
124125
self.streaming.streaming_tps_start = None;
125126
self.streaming.streaming_tps_elapsed = Duration::ZERO;
126127
self.streaming.streaming_tps_collect_output = false;

‎crates/jcode-tui/src/tui/app/remote/server_events.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ pub(in crate::tui::app) fn handle_server_event(
606606
| ServerEvent::StatusDetail { .. }
607607
);
608608
if externally_started_turn_event {
609+
app.streaming.response_output.reset();
609610
crate::logging::info(
610611
"Adopting externally started turn: stream event received while idle with no current_message_id",
611612
);
@@ -658,6 +659,7 @@ pub(in crate::tui::app) fn handle_server_event(
658659
app.insert_thought_line(thought_line);
659660
return eager_stream_redraw;
660661
}
662+
app.streaming.response_output.text(&text);
661663
let mut needs_redraw = false;
662664
if matches!(
663665
app.status,
@@ -678,6 +680,7 @@ pub(in crate::tui::app) fn handle_server_event(
678680
eager_stream_redraw && needs_redraw
679681
}
680682
ServerEvent::TextReplace { text } => {
683+
app.streaming.response_output.replace_text(&text);
681684
let ops = app.stream_buffer.flush();
682685
app.apply_stream_ops(ops);
683686
app.replace_streaming_text(text);
@@ -732,6 +735,7 @@ pub(in crate::tui::app) fn handle_server_event(
732735
eager_stream_redraw
733736
}
734737
ServerEvent::ToolStart { id, name } => {
738+
app.streaming.response_output.tool();
735739
app.usage_reset.openai_quota_rejected = false;
736740
// Tool-call JSON is provider-generated output and is included in output-token
737741
// usage. Keep the TPS timer running until the server reports ToolExec; actual
@@ -758,6 +762,9 @@ pub(in crate::tui::app) fn handle_server_event(
758762
false
759763
}
760764
ServerEvent::ToolExec { id, name } => {
765+
// A reconnect can resume during execution, without replaying the
766+
// ToolStart event that normally marks this response as tool work.
767+
app.streaming.response_output.tool();
761768
// Provider output generation for this tool call is complete, but final usage
762769
// snapshots often arrive later. Keep collecting deltas while excluding tool
763770
// runtime from the elapsed TPS denominator.
@@ -1099,7 +1106,8 @@ pub(in crate::tui::app) fn handle_server_event(
10991106
true
11001107
}
11011108
ServerEvent::TextDone => false,
1102-
ServerEvent::MessageEnd { .. } => {
1109+
ServerEvent::MessageEnd { stop_reason } => {
1110+
app.streaming.response_output.finish(stop_reason.as_deref());
11031111
app.pause_streaming_tps(true);
11041112
app.stream_message_ended = true;
11051113
true
@@ -1342,7 +1350,9 @@ pub(in crate::tui::app) fn handle_server_event(
13421350
"client_turn_completed",
13431351
std::time::Duration::from_secs(30),
13441352
);
1345-
auto_poked = app.schedule_turn_end_followups();
1353+
auto_poked = app.schedule_turn_end_followups_after_response(
1354+
app.streaming.response_output.has_final_response(),
1355+
);
13461356
if !auto_poked {
13471357
app.clear_visible_turn_started();
13481358
if app.queued_messages.is_empty() {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/// Output of the current provider response, independent of the render buffer.
2+
/// Compaction may commit that buffer before the turn's Done event arrives.
3+
#[derive(Clone, Debug, Default)]
4+
pub(super) struct ResponseOutput {
5+
has_text: bool,
6+
has_tools: bool,
7+
ended_normally: bool,
8+
}
9+
10+
impl ResponseOutput {
11+
pub(super) fn reset(&mut self) {
12+
*self = Self::default();
13+
}
14+
15+
pub(super) fn text(&mut self, text: &str) {
16+
self.has_text |= !text.trim().is_empty();
17+
}
18+
19+
pub(super) fn replace_text(&mut self, text: &str) {
20+
self.has_text = !text.trim().is_empty();
21+
}
22+
23+
pub(super) fn tool(&mut self) {
24+
self.has_tools = true;
25+
}
26+
27+
pub(super) fn finish(&mut self, stop_reason: Option<&str>) {
28+
// Unknown, truncated, tool-use, and guardrail endings are not evidence
29+
// that a final answer was delivered. None is normal for some providers.
30+
self.ended_normally = matches!(
31+
stop_reason,
32+
None | Some("end_turn" | "stop" | "stop_sequence" | "completed")
33+
);
34+
}
35+
36+
pub(super) fn has_final_response(&self) -> bool {
37+
self.has_text && !self.has_tools && self.ended_normally
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use super::*;
44+
45+
#[test]
46+
fn final_response_requires_text_without_tools_and_a_normal_end() {
47+
for reason in [None, Some("end_turn"), Some("stop"), Some("completed")] {
48+
let mut output = ResponseOutput::default();
49+
output.text(" \n");
50+
output.finish(reason);
51+
assert!(!output.has_final_response());
52+
output.text("Done.");
53+
assert!(output.has_final_response());
54+
output.tool();
55+
assert!(!output.has_final_response());
56+
output.text("Commentary after a tool call");
57+
assert!(!output.has_final_response());
58+
output.reset();
59+
assert!(!output.has_final_response());
60+
}
61+
for reason in [
62+
"max_tokens",
63+
"length",
64+
"refusal",
65+
"tool_use",
66+
"pause_turn",
67+
"unknown",
68+
] {
69+
let mut output = ResponseOutput::default();
70+
output.text("Partial answer");
71+
output.finish(Some(reason));
72+
assert!(!output.has_final_response(), "{reason}");
73+
}
74+
}
75+
76+
#[test]
77+
fn replacement_can_remove_an_answer() {
78+
let mut output = ResponseOutput::default();
79+
output.text("Tool markup, not an answer");
80+
output.finish(None);
81+
output.replace_text("");
82+
assert!(!output.has_final_response());
83+
}
84+
}

‎crates/jcode-tui/src/tui/app/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ include!("tests/smoothness_benchmark.rs");
5252
include!("tests/hotkey_feedback_e2e.rs");
5353
include!("tests/todo_card.rs");
5454
include!("tests/todo_completion.rs");
55+
include!("tests/final_response_followup.rs");
5556
include!("tests/issue_496_input_routing.rs");
5657
include!("tests/oauth_login_progress.rs");
5758
include!("tests/issue_544_paste_enter.rs");

0 commit comments

Comments
 (0)