From c28c6aa7a060e6ac421317432ee3b4549069f823 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:48:52 +0000 Subject: [PATCH 001/236] docs: update downloads badge --- docs/badges/downloads.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/badges/downloads.json b/docs/badges/downloads.json index 46c297ef22a..791a9497a87 100644 --- a/docs/badges/downloads.json +++ b/docs/badges/downloads.json @@ -1 +1 @@ -{"schemaVersion": 1, "label": "downloads", "message": "3.14M", "color": "brightgreen"} +{"schemaVersion": 1, "label": "downloads", "message": "3.15M", "color": "brightgreen"} From 9d00b3371f21702fd0e65eb0f7d7d6d8d8a75e4a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 03:22:24 +0000 Subject: [PATCH 002/236] docs: update weekly stars chart --- docs/images/star-history.svg | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/images/star-history.svg b/docs/images/star-history.svg index 6216bae4e56..71029313518 100644 --- a/docs/images/star-history.svg +++ b/docs/images/star-history.svg @@ -1,6 +1,6 @@ 1jehuang/jcode cumulative GitHub stars over time -Cumulative GitHub stars sampled weekly over the last 26 weeks, ending at 21,533 stars. +Cumulative GitHub stars sampled weekly over the last 26 weeks, ending at 21,566 stars. From ad6843abe9e2d4fff21c2a6687b08de8daa95de5 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 18:44:06 +0000 Subject: [PATCH 003/236] docs: update downloads badge --- docs/badges/downloads.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/badges/downloads.json b/docs/badges/downloads.json index 791a9497a87..fdd8a44a36b 100644 --- a/docs/badges/downloads.json +++ b/docs/badges/downloads.json @@ -1 +1 @@ -{"schemaVersion": 1, "label": "downloads", "message": "3.15M", "color": "brightgreen"} +{"schemaVersion": 1, "label": "downloads", "message": "3.16M", "color": "brightgreen"} From a0e31e4d3314fa78b0c5a94fbcfc4220e3713088 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:24:27 +0000 Subject: [PATCH 004/236] docs: update weekly stars chart --- docs/images/star-history.svg | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/images/star-history.svg b/docs/images/star-history.svg index 71029313518..f945561f268 100644 --- a/docs/images/star-history.svg +++ b/docs/images/star-history.svg @@ -1,6 +1,6 @@ 1jehuang/jcode cumulative GitHub stars over time -Cumulative GitHub stars sampled weekly over the last 26 weeks, ending at 21,566 stars. +Cumulative GitHub stars sampled weekly over the last 26 weeks, ending at 21,599 stars. From 017e37da5f7e8a3863d0dd128a353bd1541b1988 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:23:15 -0800 Subject: [PATCH 005/236] Restore compact comprehensive TUI activity status --- crates/jcode-tui/src/tui/compact_status.rs | 149 ++++ crates/jcode-tui/src/tui/stable_status.rs | 766 +++++++----------- crates/jcode-tui/src/tui/ui_input.rs | 5 +- .../src/tui/ui_input/status_tests.rs | 166 ++-- crates/jcode-tui/src/tui/ui_tests/mod.rs | 18 +- .../src/tui/ui_tests/stable_status.rs | 230 ++++-- 6 files changed, 724 insertions(+), 610 deletions(-) create mode 100644 crates/jcode-tui/src/tui/compact_status.rs diff --git a/crates/jcode-tui/src/tui/compact_status.rs b/crates/jcode-tui/src/tui/compact_status.rs new file mode 100644 index 00000000000..d00de2251f9 --- /dev/null +++ b/crates/jcode-tui/src/tui/compact_status.rs @@ -0,0 +1,149 @@ +use super::*; + +pub(super) fn status_single_line(text: &str) -> String { + text.chars() + .map(|c| { + if c.is_control() + || c.is_whitespace() + || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') + { + ' ' + } else { + c + } + }) + .collect::() + .split_whitespace() + .collect::>() + .join(" ") +} + +/// Main composer activity. Only the phase occupies a fixed ten-cell field. +pub(in crate::tui) fn composer_processing_status_line( + status: &crate::protocol::SwarmLiveStatus, + stale_secs: Option, + spinner: &'static str, +) -> Line<'static> { + if matches!( + status.phase.as_str(), + "stopped" | "failed" | "crashed" | "completed" | "done" | "cancelled" + ) { + let mut line = live_processing_status_line(status, stale_secs, spinner); + for span in &mut line.spans { + span.content = status_single_line(&span.content).into(); + } + return line; + } + let connection = status.connection_phase.as_deref().unwrap_or("connecting"); + let phase = match status.phase.as_str() { + "running_tool" if status.tool_name.as_deref() == Some("batch") => "batch", + "running_tool" => "tool", + "running" => "thinking", + "waiting_network" => "offline", + "connecting" if connection.starts_with("retry") => "retry", + "sending" => "sending", + "connecting" => "connecting", + "thinking" => "thinking", + "streaming" => "streaming", + "building" => "building", + "retry" => "retry", + _ => "thinking", + }; + let seconds = status.elapsed_ms / 1000; + let elapsed = if seconds >= 3600 { + format!("{}h {}m", seconds / 3600, seconds / 60 % 60) + } else if seconds >= 60 { + format!("{}m {}s", seconds / 60, seconds % 60) + } else { + format!("{seconds}s") + }; + let warning = matches!(phase, "offline" | "retry" | "building"); + let color = if warning { + rgb(255, 193, 7) + } else { + dim_color() + }; + let mut spans = vec![ + Span::styled( + spinner, + Style::default().fg(if warning { color } else { ai_color() }), + ), + Span::styled( + format!(" {phase:<10} {elapsed}"), + Style::default().fg(color), + ), + ]; + let mut detail = |text: String| { + let text = status_single_line(&text); + if !text.is_empty() { + spans.push(Span::styled( + format!(" · {text}"), + Style::default().fg(color), + )); + } + }; + match status.phase.as_str() { + "connecting" => { + let label = match connection { + "authenticating" => "refreshing auth", + "sending_request" => "sending context", + "waiting_for_response" => "waiting for response", + other => other, + }; + if label != phase { + detail(label.into()); + } + } + "running_tool" => { + if let Some(name) = status + .tool_name + .as_ref() + .filter(|name| name.as_str() != "batch") + { + detail(name.clone()); + } + if let Some(intent) = &status.tool_intent { + detail(intent.clone()); + } + } + "streaming" if !status.stream_message_ended => match stale_secs { + Some(s) if s > 10.0 => detail(format!("(stalled {s:.0}s)")), + Some(s) if s > 2.0 => detail(format!("(no tokens {s:.0}s)")), + _ => {} + }, + "waiting_network" => detail("network disconnected, waiting to retry".into()), + _ => {} + } + let tokens = [ + status + .input_tokens + .map(|input| format!("↑{}", format_stream_tokens(input))), + status + .output_tokens + .map(|output| format!("↓{}", format_stream_tokens(output))), + ] + .into_iter() + .flatten() + .collect::>() + .join(" "); + if !tokens.is_empty() { + detail(tokens); + } + if let Some(tps) = status.output_tps_milli { + detail(format!("{:.1} tps", tps as f64 / 1000.0)); + } + for label in collect_transport_context_labels( + status + .status_detail + .as_deref() + .and_then(normalize_status_detail), + status + .connection_type + .as_deref() + .map(display_connection_type), + None, + ) { + detail(label); + } + Line::from(spans) +} diff --git a/crates/jcode-tui/src/tui/stable_status.rs b/crates/jcode-tui/src/tui/stable_status.rs index f49c940f9e5..0de64365db2 100644 --- a/crates/jcode-tui/src/tui/stable_status.rs +++ b/crates/jcode-tui/src/tui/stable_status.rs @@ -1,503 +1,353 @@ use super::*; -use unicode_width::UnicodeWidthStr; - -const PHASE_WIDTH: usize = 12; -const ELAPSED_WIDTH: usize = 8; - -fn status_single_line(text: &str) -> String { - text.chars() - .map(|c| { - if c.is_control() || c.is_whitespace() - || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') - { ' ' } else { c } - }) - .collect::() - .split_whitespace() - .collect::>() - .join(" ") -} - -pub(super) fn status_cell(text: &str, width: usize) -> String { - let line = jcode_tui_render::truncate_line_with_ellipsis_to_width( - &Line::from(status_single_line(text)), - width, - ); - let mut result = line - .spans - .iter() - .map(|s| s.content.as_ref()) - .collect::(); - result.push_str(&" ".repeat(width.saturating_sub(UnicodeWidthStr::width(result.as_str())))); - result -} - -pub(super) fn stable_status_elapsed(elapsed_ms: u64) -> String { - let seconds = elapsed_ms / 1000; - let hours = seconds / 3600; - let value = if hours == 0 { - format!("{:02}:{:02}", seconds / 60, seconds % 60) - } else if hours < 100 { - format!("{hours}:{:02}:{:02}", seconds / 60 % 60, seconds % 60) - } else if hours <= 999 { - format!("{hours}h{:02}m", seconds / 60 % 60) - } else if hours <= 999_999 { - format!("{hours}h") - } else { - ">999999h".into() - }; - status_cell(&value, ELAPSED_WIDTH) -} - -/// Values never resize a telemetry slot, including when telemetry is absent. -pub(super) fn stable_status_telemetry(status: &crate::protocol::SwarmLiveStatus) -> [String; 3] { - let token = |value: Option| { - value - .map(format_stream_tokens) - .unwrap_or_else(|| "-".into()) - }; - [ - format!("↑{}", status_cell(&token(status.input_tokens), 6)), - format!("↓{}", status_cell(&token(status.output_tokens), 6)), - status_cell( - &status - .output_tps_milli - .map(|v| format!("{:.1}/s", v as f64 / 1000.0)) - .unwrap_or_else(|| "-/s".into()), - 8, - ), - ] -} - -#[derive(Default)] -struct StatusBadges { - queued: usize, - background: usize, -} - -impl StatusBadges { - fn text(&self, width: usize) -> String { - let count = |n: usize| if n > 99 { "99+".into() } else { n.to_string() }; - let queued = if self.queued > 0 { - if width >= 22 { - format!("[+{} queued]", count(self.queued)) - } else { - format!("q:{}", count(self.queued)) - } - } else { - String::new() - }; - let background = if self.background > 0 { - if width >= 22 { - format!("[bg {}]", count(self.background)) - } else { - format!("bg:{}", count(self.background)) - } - } else { - String::new() - }; - let (queue_width, background_width) = match width { - 22.. => (13, 8), - 12.. => (5, 6), - _ => (3, 4), - }; - format!( - "{} {}", - status_cell(&queued, queue_width), - status_cell(&background, background_width) - ) - } -} - -#[cfg(test)] -pub(super) fn stable_status_line( - glyph: &str, - phase: &str, - elapsed_ms: u64, - activity: &str, - telemetry: &[String], - width: usize, -) -> Line<'static> { - compose_status( - glyph, - phase, - elapsed_ms, - activity, - telemetry, - &StatusBadges::default(), - width, - ) -} - -fn compose_status( - glyph: &str, - phase: &str, - elapsed_ms: u64, - activity: &str, - telemetry: &[String], - badges: &StatusBadges, - width: usize, -) -> Line<'static> { - let clock = if phase == "idle" { - " ".repeat(ELAPSED_WIDTH) - } else { - stable_status_elapsed(elapsed_ms) - }; - let prefix = format!(" {} {} │ ", status_cell(phase, PHASE_WIDTH), clock); - let prefix_width = 1 + UnicodeWidthStr::width(prefix.as_str()); - // These reservations depend only on terminal width, never on label/count length. - let badge_width = match width { - 100.. => 22, - 64.. => 12, - 40.. => 8, - _ => 0, - }; - let mut count = telemetry.len(); - while count > 0 - && prefix_width - + 8 - + badge_width - + 3 - + telemetry[..count] - .iter() - .map(|s| UnicodeWidthStr::width(s.as_str()) + 1) - .sum::() - > width - { - count -= 1; - } - let right = telemetry[..count].join(" "); - let right_width = UnicodeWidthStr::width(right.as_str()) + if count > 0 { 3 } else { 0 }; - let activity_width = width.saturating_sub(prefix_width + badge_width + right_width); - let warning = matches!( - phase, - "building" | "rate limited" | "offline" | "blocked" | "failed" | "crashed" - ); - let warning_color = rgb(255, 193, 7); - let activity_warning = warning - || activity.contains('⚠') - || activity.starts_with("stalled") - || activity.starts_with("no tokens"); - let mut spans = vec![ - Span::styled( - status_cell(glyph, 1), - Style::default().fg(if warning { warning_color } else { ai_color() }), - ), - Span::styled( - prefix, - Style::default().fg(if warning { warning_color } else { dim_color() }), - ), - Span::styled( - status_cell(activity, activity_width), - Style::default().fg(if activity_warning { - warning_color - } else { - ai_color() - }), - ), - ]; - if badge_width > 0 { - spans.push(Span::styled( - badges.text(badge_width), - Style::default().fg(queued_color()), - )); - } - if count > 0 { - spans.push(Span::styled( - format!(" │ {right}"), - Style::default().fg(dim_color()), - )); - } - jcode_tui_render::truncate_line_with_ellipsis_to_width(&Line::from(spans), width) -} - -fn live_row( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, - badges: &StatusBadges, - width: usize, -) -> Line<'static> { - let phase = match status.phase.as_str() { - "running_tool" if status.tool_name.as_deref() == Some("batch") => "batch", - "running_tool" => "tool", - "running" => "working", - "waiting_network" => "offline", - other => other, - }; - let terminal = matches!( - phase, - "completed" - | "done" - | "failed" - | "crashed" - | "stopped" - | "cancelled" - | "blocked" - | "waiting" - | "idle" - | "ready" - | "queued" - ); - let mut activity = Vec::new(); - // Warnings have a stable home, ahead of potentially unbounded tool intent. - if status.phase == "streaming" - && !status.stream_message_ended - && let Some(stale) = stale_secs.filter(|s| *s > 2.0) - { - activity.push(format!( - "{} {:.0}s", - if stale > 10.0 { "stalled" } else { "no tokens" }, - stale - )); - } - if terminal { - if let Some(reason) = status - .stop_reason - .as_ref() - .or(status.status_detail.as_ref()) - { - activity.push(reason.clone()); - } - } else { - // draw_status places important warnings at the start of this field. - if let Some(intent) = &status.tool_intent - && intent.contains('⚠') - { - activity.push(intent.clone()); - } - if let Some(name) = &status.tool_name { - activity.push(name.clone()); - } - if let Some(intent) = &status.tool_intent - && !intent.contains('⚠') - { - activity.push(intent.clone()); - } - if let Some(connection) = &status.connection_phase { - activity.push(match connection.as_str() { - "authenticating" => "refreshing auth".into(), - "sending_request" => "sending context".into(), - "waiting_for_response" => "waiting for response".into(), - other => other.into(), - }); - } - activity.extend(collect_transport_context_labels( - status - .status_detail - .as_deref() - .and_then(normalize_status_detail), - status - .connection_type - .as_deref() - .map(display_connection_type), - None, - )); - } - activity.retain(|detail| !detail.trim().is_empty()); - let glyph = match phase { - "completed" | "done" => "✓", - "failed" | "crashed" => "✗", - "blocked" => "⊘", - "waiting" | "offline" => "↻", - "idle" | "ready" | "stopped" | "cancelled" => "◼", - _ => spinner, - }; - let telemetry = if terminal { - [" ".repeat(7), " ".repeat(7), " ".repeat(8)] - } else { - stable_status_telemetry(status) - }; - compose_status( - glyph, - phase, - status.elapsed_ms, - &activity.join(" · "), - &telemetry, - badges, - width, - ) -} - -#[cfg(test)] -pub(super) fn live_processing_status_line_at_width( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, - width: usize, -) -> Line<'static> { - live_row(status, stale_secs, spinner, &StatusBadges::default(), width) -} +use crate::tui::info_widget::occasional_status_tip; +use crate::tui::ui::activity_indicator; pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { crate::tui::app::oauth_telemetry::note_status_render(area); - let notice = Line::from(status_single_line( - crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT, - )); frame.render_widget( - Paragraph::new(jcode_tui_render::truncate_line_with_ellipsis_to_width( - ¬ice, - area.width as usize, - )) - .style(Style::default().fg(accent_color())), + Paragraph::new(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) + .style(Style::default().fg(accent_color())), area, ); return; } let elapsed = app.elapsed().map(|d| d.as_secs_f32()).unwrap_or(0.0); - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.elapsed_ms = app - .elapsed() - .map(|d| d.as_millis().min(u64::MAX as u128) as u64) - .unwrap_or(0); - let mut activity = Vec::new(); - let build = crate::build::read_build_progress(); - snapshot.phase = if let Some(build) = build { - activity.push(build.to_string()); - "building".into() + let stale_secs = app.time_since_activity().map(|d| d.as_secs_f32()); + let (cache_read, cache_creation) = app.streaming_cache_tokens(); + let user_turn_count = app.display_user_message_count(); + let (streaming_input_tokens, _) = app.streaming_tokens(); + let provider_name = app.provider_name(); + let upstream_provider = app.upstream_provider(); + let cache_ttl = app.cache_ttl_status(); + let kv_cache_problem = detect_kv_cache_problem( + &provider_name, + upstream_provider.as_deref(), + user_turn_count, + streaming_input_tokens, + cache_read, + cache_creation, + cache_ttl.as_ref(), + ); + + let queued_suffix = if pending_count > 0 { + format!(" · +{} queued", pending_count) + } else { + String::new() + }; + + let line = if let Some(build_progress) = crate::build::read_build_progress() { + let spinner = activity_indicator(elapsed, 12.5); + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "building".into(); + let mut spans = composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + spans.push(Span::styled( + format!(" · {build_progress}"), + Style::default().fg(rgb(255, 193, 7)), + )); + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) } else if let Some(remaining) = app.rate_limit_remaining() { - activity.push(format!( - "auto-retry in {}", - stable_status_elapsed(remaining.as_millis().min(u64::MAX as u128) as u64).trim() + let secs = remaining.as_secs(); + let spinner = activity_indicator(elapsed, 4.0); + let time_str = if secs >= 3600 { + let hours = secs / 3600; + let mins = (secs % 3600) / 60; + format!("{}h {}m", hours, mins) + } else if secs >= 60 { + let mins = secs / 60; + let s = secs % 60; + format!("{}m {}s", mins, s) + } else { + format!("{}s", secs) + }; + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "retry".into(); + let mut spans = composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + spans.push(Span::styled( + format!(" · Rate limited. Auto-retry in {time_str}..."), + Style::default().fg(rgb(255, 193, 7)), )); - "rate limited".into() + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) } else if app.is_processing() { + let spinner = activity_indicator(elapsed, 12.5); + match app.status() { - ProcessingStatus::Idle => "idle".into(), - ProcessingStatus::Sending => "sending".into(), - ProcessingStatus::Thinking(_) => "thinking".into(), - ProcessingStatus::Streaming => "streaming".into(), + ProcessingStatus::Idle => Line::from(""), + ProcessingStatus::Sending => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "sending".into(); + let mut spans = + composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } ProcessingStatus::Connecting(ref phase) => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "connecting".into(); snapshot.connection_phase = Some(connection_phase_label(phase)); + // "Suspiciously long" is measured per connection attempt, not + // across the whole turn, so later round-trips don't immediately + // render yellow just because the turn has been running a while. let phase_elapsed = app .connection_phase_elapsed() .map_or(elapsed, |d| d.as_secs_f32()); - if matches!(phase, crate::message::ConnectionPhase::Retrying { .. }) - || (phase_elapsed > 10.0 - && matches!( - phase, - crate::message::ConnectionPhase::Authenticating - | crate::message::ConnectionPhase::Connecting - | crate::message::ConnectionPhase::SendingRequest - )) - { - activity.push("⚠ connection delayed".into()); + let label_color = match phase { + crate::message::ConnectionPhase::Retrying { .. } => rgb(255, 193, 7), + crate::message::ConnectionPhase::Authenticating if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + crate::message::ConnectionPhase::Connecting if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + crate::message::ConnectionPhase::SendingRequest if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + _ => dim_color(), + }; + let mut spans = + composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + if let Some(label) = spans.get_mut(1) { + label.style = label.style.fg(label_color); } - "connecting".into() + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Thinking(_start) => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "thinking".into(); + let mut spans = + composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Streaming => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "streaming".into(); + let mut spans = + composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + if let Some(upstream) = app.upstream_provider() { + spans.push(Span::styled( + format!(" · via {upstream}"), + Style::default().fg(dim_color()), + )); + } + if let Some(problem) = kv_cache_problem { + let miss_tokens = problem.affected_tokens.unwrap_or(0); + let miss_str = if miss_tokens >= 1000 { + format!("{}k", miss_tokens / 1000) + } else if miss_tokens > 0 { + format!("{}", miss_tokens) + } else { + "kv".to_string() + }; + spans.push(Span::styled( + format!(" · ⚠ {miss_str} cache miss"), + Style::default().fg(rgb(255, 193, 7)), + )); + } + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) } ProcessingStatus::WaitingForNetwork { listener } => { - activity.push(format!( - "network disconnected, waiting to retry · {listener}" + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "waiting_network".into(); + let mut spans = + composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + spans.push(Span::styled( + format!(" · {listener}"), + Style::default().fg(rgb(255, 193, 7)), )); - "waiting_network".into() + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) } ProcessingStatus::RunningTool(ref name) => { - snapshot.tool_name = Some(name.clone()); - if name == "batch" { - let calls = app.streaming_tool_calls(); - let initial = calls + let anim_color = ai_color(); + let batch_prog = app.batch_progress(); + let is_batch = name == "batch"; + // For batch: compute initial total from the streaming tool call input + let batch_total_initial = if is_batch { + app.streaming_tool_calls() .last() .and_then(|tc| tc.input.get("tool_calls")) .and_then(|v| v.as_array()) - .map(|a| a.len()); - let mut spans = Vec::new(); + .map(|a| a.len()) + } else { + None + }; + let tool_detail = if is_batch { + None // batch always uses progress display + } else { + app.streaming_tool_calls() + .last() + .map(get_tool_activity_detail) + .filter(|s| !s.is_empty()) + }; + let experimental_notice = app.active_experimental_feature_notice(); + let subagent = app.subagent_status(); + + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "running_tool".into(); + snapshot.tool_name = Some(name.clone()); + snapshot.tool_intent = tool_detail; + let mut spans = + composer_processing_status_line(&snapshot, stale_secs, spinner).spans; + + // For batch tool: show "completed/total · last_tool" progress + if is_batch { append_batch_progress_spans( &mut spans, - ai_color(), - app.batch_progress(), - initial, - ); - activity.push( - spans - .iter() - .map(|s| s.content.as_ref()) - .collect::() - .trim_start_matches(" · ") - .to_string(), + anim_color, + batch_prog, + batch_total_initial, ); - } else if let Some(detail) = app - .streaming_tool_calls() - .last() - .map(get_tool_activity_detail) - .filter(|d| !d.is_empty()) + } + + if let Some(notice) = experimental_notice { + spans.push(Span::styled( + format!(" · ⚠ {}", notice), + Style::default().fg(rgb(255, 193, 7)).bold(), + )); + } + + if let Some(status) = subagent { + spans.push(Span::styled( + format!(" ({})", status), + Style::default().fg(dim_color()), + )); + } + if let Some(label) = app + .upstream_provider() + .map(|provider| format!("via {provider}")) { - activity.push(detail); + spans.push(Span::styled( + format!(" · {}", label), + Style::default().fg(dim_color()), + )); } - "running_tool".into() - } - } - } else { - "idle".into() - }; - if app.is_processing() { - let mut warnings = Vec::new(); - if let Some(notice) = app.active_experimental_feature_notice() { - warnings.push(format!("⚠ {notice}")); - } - if matches!(snapshot.phase.as_str(), "streaming" | "running_tool") { - let (read, creation) = app.streaming_cache_tokens(); - if let Some(problem) = detect_kv_cache_problem( - &app.provider_name(), - app.upstream_provider().as_deref(), - app.display_user_message_count(), - app.streaming_tokens().0, - read, - creation, - app.cache_ttl_status().as_ref(), - ) { - warnings.push(format!( - "⚠ {} cache miss", - problem - .affected_tokens - .map(format_stream_tokens) - .unwrap_or_else(|| "kv".into()) + + if let Some(problem) = kv_cache_problem { + let miss_tokens = problem.affected_tokens.unwrap_or(0); + let miss_str = if miss_tokens >= 1000 { + format!("{}k", miss_tokens / 1000) + } else if miss_tokens > 0 { + format!("{}", miss_tokens) + } else { + "kv".to_string() + }; + spans.push(Span::styled( + format!(" · ⚠ {} cache miss", miss_str), + Style::default().fg(rgb(255, 193, 7)), + )); + } + + spans.push(Span::styled( + format!(" · {} bg", jcode_tui_core::keybind::alt_chord("B")), + Style::default().fg(rgb(100, 100, 100)), )); + + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) } } - warnings.append(&mut activity); - activity = warnings; - if let Some(subagent) = app.subagent_status() { - activity.push(subagent); - } - if let Some(upstream) = app.upstream_provider() { - activity.push(format!("via {upstream}")); + } else if let Some((total_in, total_out)) = app.total_session_tokens() { + let total = total_in + total_out; + if let Some(warning) = occasional_session_history_warning( + total, + app.session_compaction_count(), + app.context_limit(), + area.width as usize, + app.animation_elapsed() as u64, + ) { + let severe_token_threshold = app + .context_limit() + .and_then(|limit| u64::try_from(limit).ok()) + .map(|limit| limit.saturating_mul(3)) + .unwrap_or(1_000_000); + let warning_color = + if total >= severe_token_threshold || app.session_compaction_count() >= 3 { + rgb(255, 100, 100) + } else { + rgb(255, 193, 7) + }; + Line::from(vec![ + Span::styled("⚠ ", Style::default().fg(warning_color)), + Span::styled(warning, Style::default().fg(warning_color)), + ]) + } else if let Some(tip) = + occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) + { + Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) + } else { + Line::from("") } - } else if snapshot.phase == "idle" { - // Idle is steady except for actionable session-history warnings. No rotating tips. - if let Some((input, output)) = app.total_session_tokens() - && let Some(warning) = occasional_session_history_warning( - input.saturating_add(output), - app.session_compaction_count(), - app.context_limit(), - area.width as usize, - app.animation_elapsed() as u64, - ) + } else { + if let Some(tip) = + occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) { - activity.push(format!("⚠ {warning}")); + Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) + } else { + Line::from("") } - snapshot.status_detail = activity.first().cloned(); - snapshot.connection_type = None; - } - if snapshot.phase != "streaming" { - snapshot.output_tps_milli = None; - } - snapshot.tool_intent = Some(activity.join(" · ")); - let badges = StatusBadges { - queued: pending_count, - background: app - .background_task_rows() - .iter() - .filter(|t| t.status == crate::tui::BackgroundTaskRowStatus::Running) - .count(), }; - let line = live_row( - &snapshot, - app.time_since_activity().map(|d| d.as_secs_f32()), - super::super::activity_indicator(elapsed, 12.5), - &badges, + + let line = status_with_background_tasks( + line, + app.background_task_rows(), area.width as usize, + app.is_processing(), ); + crate::memory::check_staleness(); - // Center the composer container, never the changing status text inside it. - frame.render_widget(Paragraph::new(line).alignment(Alignment::Left), area); + + frame.render_widget(Paragraph::new(line), area); +} + +/// Background launches finish their tool call immediately, but the work and +/// its intent must remain visible while the agent moves on to another tool. +fn status_with_background_tasks( + mut foreground: Line<'static>, + tasks: &[crate::tui::BackgroundTaskRow], + width: usize, + processing: bool, +) -> Line<'static> { + use crate::tui::BackgroundTaskRowStatus; + use jcode_tui_render::truncate_line_with_ellipsis_to_width as fit; + + for span in &mut foreground.spans { + // Preserve intentional prefix spacing while stripping terminal controls. + span.content = span.content.chars().map(|c| { + if c.is_control() || c.is_whitespace() + || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') + { ' ' } else { c } + }).collect::().into(); + } + let running: Vec<_> = tasks + .iter() + .filter(|task| task.status == BackgroundTaskRowStatus::Running) + .collect(); + if running.is_empty() { + return fit(&foreground, width); + } + let labels = running + .iter() + .map(|task| compact_status::status_single_line(&task.label)) + .collect::>() + .join(" · "); + let background = Line::from(Span::styled( + format!("bg {}: {}", running.len(), labels), + Style::default().fg(ai_color()), + )); + // Idle tips must not displace actual work. While processing, reserve space + // for both activities rather than letting a long foreground intent hide bg. + if !processing || foreground.width() == 0 || width < 24 { + return fit(&background, width); + } + let background_width = background.width().min(width / 2); + let mut line = fit(&foreground, width.saturating_sub(background_width + 3)); + line.spans + .push(Span::styled(" · ", Style::default().fg(dim_color()))); + line.spans.extend(fit(&background, background_width).spans); + line } diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index 2c71058a20f..27902b75919 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -671,10 +671,13 @@ fn append_batch_progress_spans( } } +#[path = "compact_status.rs"] +mod compact_status; #[path = "stable_status.rs"] mod stable_status; +pub(super) use compact_status::composer_processing_status_line; -/// Compact live activity for todo-lane observers. The composer uses stable_status. +/// Compact live activity for todo-lane observers. The composer uses compact_status. /// No provider or worker state is inferred here: absent telemetry stays absent. pub(super) fn live_processing_status_line( status: &crate::protocol::SwarmLiveStatus, diff --git a/crates/jcode-tui/src/tui/ui_input/status_tests.rs b/crates/jcode-tui/src/tui/ui_input/status_tests.rs index 2b1eaf2d9f1..4f2a27226a7 100644 --- a/crates/jcode-tui/src/tui/ui_input/status_tests.rs +++ b/crates/jcode-tui/src/tui/ui_input/status_tests.rs @@ -1,107 +1,89 @@ use super::*; use ratatui::style::Modifier; -use super::stable_status::{ - live_processing_status_line_at_width, stable_status_elapsed, stable_status_line, - stable_status_telemetry, status_cell, -}; - -#[test] -fn stable_status_elapsed_keeps_cell_width_across_time_boundaries() { - for elapsed in [ - 0, - 999, - 59_999, - 60_000, - 3_599_999, - 3_600_000, - 360_000_000, - u64::MAX, +use super::compact_status::composer_processing_status_line; + +#[test] +fn compact_status_absent_telemetry_does_not_render_placeholders_or_gaps() { + let status = crate::protocol::SwarmLiveStatus { + phase: "streaming".into(), + elapsed_ms: 12_300, + ..Default::default() + }; + let text = composer_processing_status_line(&status, None, "*").to_string(); + assert_eq!(text, "* streaming 12s"); + assert!(!text.contains('│') && !text.contains('↑') && !text.contains('↓')); + assert!(!text.ends_with(' ')); +} + +#[test] +fn compact_status_partial_telemetry_only_shows_known_counts() { + for (input_tokens, output_tokens, expected) in [ + (Some(1200), None, "↑1k"), + (None, Some(456), "↓456"), + (Some(0), Some(0), "↑0 ↓0"), ] { - let value = stable_status_elapsed(elapsed); - assert_eq!( - unicode_width::UnicodeWidthStr::width(value.as_str()), - 8, - "{value}" - ); + let status = crate::protocol::SwarmLiveStatus { + phase: "streaming".into(), + input_tokens, + output_tokens, + ..Default::default() + }; + let text = composer_processing_status_line(&status, None, "*").to_string(); + assert!(text.contains(expected), "{text}"); + assert!(!text.contains('?') && !text.contains('│'), "{text}"); } - assert_eq!(stable_status_elapsed(12_300).trim(), "00:12"); - assert_eq!(stable_status_elapsed(3_600_000).trim(), "1:00:00"); } #[test] -fn stable_status_cells_fit_unicode_and_normalize_controls() { - for width in 0..40 { - let value = status_cell("界\n tests\tand \x1bmore", width); - assert_eq!(unicode_width::UnicodeWidthStr::width(value.as_str()), width); - assert!(!value.chars().any(char::is_control)); +fn compact_status_prefix_clock_is_consistent_across_phases() { + let mut column = None; + for phase in [ + "sending", + "thinking", + "connecting", + "streaming", + "running_tool", + ] { + let status = crate::protocol::SwarmLiveStatus { + phase: phase.into(), + elapsed_ms: 12_300, + tool_name: Some("bash".into()), + ..Default::default() + }; + let text = composer_processing_status_line(&status, None, "*").to_string(); + let offset = text.find("12s").expect(&text); + assert_eq!(*column.get_or_insert(offset), offset, "{text}"); + assert!(!text.contains('│'), "{text}"); } } #[test] -fn stable_status_telemetry_stays_anchored_as_values_and_intent_change() { - let mut status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), +fn compact_status_detail_normalizes_unicode_and_controls() { + let status = crate::protocol::SwarmLiveStatus { + phase: "running_tool".into(), elapsed_ms: 12_300, + tool_name: Some("bash".into()), + tool_intent: Some("Verify 界\n tests\tand \u{001b}more".into()), ..Default::default() }; - for width in [60, 80, 100, 120] { - let unknown = live_processing_status_line_at_width(&status, None, "*", width).to_string(); - status.input_tokens = Some(123_456); - status.output_tokens = Some(1234); - status.output_tps_milli = Some(12_500); - status.tool_intent = Some("界 long intent ".repeat(30)); - let known = live_processing_status_line_at_width(&status, None, "*", width).to_string(); - let column = |text: &str, needle: char| { - text.find(needle) - .map(|offset| unicode_width::UnicodeWidthStr::width(&text[..offset])) - }; - assert_eq!( - column(&unknown, '↑'), - column(&known, '↑'), - "{unknown}\n{known}" - ); - assert_eq!( - column(&unknown, '↓'), - column(&known, '↓'), - "{unknown}\n{known}" - ); - assert_eq!(unicode_width::UnicodeWidthStr::width(known.as_str()), width); - status.input_tokens = None; - status.output_tokens = None; - status.output_tps_milli = None; - status.tool_intent = None; - } + let text = composer_processing_status_line(&status, None, "*").to_string(); + assert!(text.contains("Verify 界 tests and more"), "{text}"); + assert!(!text.chars().any(char::is_control), "{text}"); } #[test] -fn stable_status_empty_intent_does_not_add_a_leading_separator() { +fn compact_status_empty_intent_does_not_add_a_leading_separator() { let status = crate::protocol::SwarmLiveStatus { phase: "streaming".into(), + elapsed_ms: 12_300, tool_intent: Some(String::new()), status_detail: Some("fresh websocket".into()), ..Default::default() }; - let text = live_processing_status_line_at_width(&status, None, "*", 120).to_string(); - assert!(text.contains("│ opening websocket"), "{text}"); - assert!(!text.contains("│ ·"), "{text}"); -} - -#[test] -fn stable_status_fits_every_terminal_cell_width() { - let telemetry = stable_status_telemetry(&crate::protocol::SwarmLiveStatus::default()); - for width in 0..160 { - let line = stable_status_line( - "*", - "thinking", - 123_456, - &"界\nlong\tintent".repeat(40), - &telemetry, - width, - ); - assert!(line.width() <= width, "width={width}: {line}"); - assert!(!line.to_string().chars().any(char::is_control)); - } + let text = composer_processing_status_line(&status, None, "*").to_string(); + assert!(text.ends_with("12s · opening websocket"), "{text}"); + assert!(!text.contains("· ·"), "{text}"); } #[test] @@ -238,7 +220,7 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { tool_intent: Some("Verify 界\noutput".into()), ..Default::default() }; - let line = live_processing_status_line_at_width(&status, Some(0.0), "⠋", 160); + let line = composer_processing_status_line(&status, Some(0.0), "⠋"); let text = line.to_string(); assert!( text.contains("bash") && text.contains("Verify 界 output"), @@ -248,7 +230,7 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { text.contains("↑12k") && text.contains("↓456") && text.contains("existing websocket") - && text.contains("00:12"), + && text.contains("12s"), "{text}" ); assert_eq!(line.spans[0].style.fg, Some(ai_color())); @@ -256,19 +238,19 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { status.phase = "streaming".into(); status.output_tps_milli = Some(12_500); assert!( - live_processing_status_line_at_width(&status, None, "⠋", 160) + composer_processing_status_line(&status, None, "⠋") .to_string() - .contains("12.5/s") + .contains("12.5 tps") ); status.input_tokens = None; status.output_tokens = None; status.connection_type = None; - let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); + let text = composer_processing_status_line(&status, None, "⠋").to_string(); assert!(!text.contains("↑0") && !text.contains("websocket")); - assert!(text.contains("↑-") && text.contains("↓-"), "{text}"); + assert!(!text.contains('↑') && !text.contains('↓'), "{text}"); status.phase = "stopped".into(); status.stop_reason = Some("Rate limit\nretry later".into()); - let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); + let text = composer_processing_status_line(&status, None, "⠋").to_string(); assert!(text.contains("Rate limit retry later") && !text.contains('⠋')); } @@ -724,13 +706,13 @@ fn streaming_liveness_stays_in_activity_and_only_warns_during_streaming() { elapsed_ms: 12_000, ..Default::default() }; - let quiet = live_processing_status_line_at_width(&status, Some(3.4), "*", 120).to_string(); + let quiet = composer_processing_status_line(&status, Some(3.4), "*").to_string(); assert!(quiet.contains("no tokens 3s"), "{quiet}"); - let stalled = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); + let stalled = composer_processing_status_line(&status, Some(12.1), "*").to_string(); assert!(stalled.contains("stalled 12s"), "{stalled}"); - assert_eq!(quiet.find("00:12"), stalled.find("00:12")); + assert_eq!(quiet.find("12s"), stalled.find("12s")); status.stream_message_ended = true; - let ended = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); + let ended = composer_processing_status_line(&status, Some(12.1), "*").to_string(); assert!( ended.starts_with('*') && !ended.contains("stalled"), "{ended}" @@ -738,7 +720,7 @@ fn streaming_liveness_stays_in_activity_and_only_warns_during_streaming() { status.stream_message_ended = false; for phase in ["thinking", "running_tool", "sending", "connecting"] { status.phase = phase.into(); - let text = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); + let text = composer_processing_status_line(&status, Some(12.1), "*").to_string(); assert!( !text.contains("stalled") && !text.contains("no tokens"), "{text}" diff --git a/crates/jcode-tui/src/tui/ui_tests/mod.rs b/crates/jcode-tui/src/tui/ui_tests/mod.rs index 987b4a21798..fbc3ef7b1c7 100644 --- a/crates/jcode-tui/src/tui/ui_tests/mod.rs +++ b/crates/jcode-tui/src/tui/ui_tests/mod.rs @@ -138,6 +138,12 @@ struct TestState { status_elapsed: Option, status_rate_limit: Option, status_background: Vec, + status_tool_calls: Vec, + status_tokens: (u64, u64), + status_tps: Option, + status_transport: Option, + status_upstream: Option, + status_subagent: Option, queue_mode: bool, active_skill: Option, centered_mode: bool, @@ -249,13 +255,13 @@ impl crate::tui::TuiState for TestState { .unwrap_or_else(|| "mock-model".to_string()) } fn upstream_provider(&self) -> Option { - None + self.status_upstream.clone() } fn connection_type(&self) -> Option { None } fn status_detail(&self) -> Option { - None + self.status_transport.clone() } fn mcp_servers(&self) -> Vec<(String, usize)> { Vec::new() @@ -264,16 +270,16 @@ impl crate::tui::TuiState for TestState { Vec::new() } fn streaming_tokens(&self) -> (u64, u64) { - (0, 0) + self.status_tokens } fn streaming_cache_tokens(&self) -> (Option, Option) { (None, None) } fn output_tps(&self) -> Option { - None + self.status_tps } fn streaming_tool_calls(&self) -> Vec { - Vec::new() + self.status_tool_calls.clone() } fn elapsed(&self) -> Option { self.status_elapsed @@ -288,7 +294,7 @@ impl crate::tui::TuiState for TestState { self.active_skill.clone() } fn subagent_status(&self) -> Option { - None + self.status_subagent.clone() } fn batch_progress(&self) -> Option { self.batch_progress.clone() diff --git a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs b/crates/jcode-tui/src/tui/ui_tests/stable_status.rs index 0703f0a4cd3..c05fbaf1f06 100644 --- a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs +++ b/crates/jcode-tui/src/tui/ui_tests/stable_status.rs @@ -5,9 +5,7 @@ fn render_status(state: &TestState, width: u16, queued: usize) -> Vec { // A second sentinel row catches accidental wrapping or layout spillover. let mut terminal = Terminal::new(TestBackend::new(width.max(1), 2)).unwrap(); terminal - .draw(|frame| { - input_ui::draw_status(frame, state, Rect::new(0, 0, width, 1), queued); - }) + .draw(|frame| input_ui::draw_status(frame, state, Rect::new(0, 0, width, 1), queued)) .unwrap(); let buffer = terminal.backend().buffer(); (0..2) @@ -26,6 +24,10 @@ fn phases() -> Vec { ProcessingStatus::Connecting(crate::message::ConnectionPhase::Connecting), ProcessingStatus::Connecting(crate::message::ConnectionPhase::SendingRequest), ProcessingStatus::Connecting(crate::message::ConnectionPhase::WaitingForResponse), + ProcessingStatus::Connecting(crate::message::ConnectionPhase::Retrying { + attempt: 2, + max: 3, + }), ProcessingStatus::Thinking(std::time::Instant::now()), ProcessingStatus::Streaming, ProcessingStatus::RunningTool("bash".into()), @@ -36,10 +38,29 @@ fn phases() -> Vec { ] } +fn tool(name: &str, intent: &str) -> ToolCall { + serde_json::from_value(serde_json::json!({ + "id": "fixture", "name": name, "intent": intent, "input": { "command": "true" } + })) + .unwrap() +} + +fn background( + label: &str, + status: crate::tui::BackgroundTaskRowStatus, +) -> crate::tui::BackgroundTaskRow { + crate::tui::BackgroundTaskRow { + task_id: label.into(), + label: label.into(), + percent: None, + status, + completed_at: None, + } +} + #[test] -fn stable_status_phase_transitions_preserve_clock_and_activity_columns() { +fn compact_status_phase_transitions_keep_clock_column_without_dashboard() { let mut clock_column = None; - let mut activity_column = None; for status in phases() { let state = TestState { status, @@ -47,17 +68,17 @@ fn stable_status_phase_transitions_preserve_clock_and_activity_columns() { ..Default::default() }; let rows = render_status(&state, 120, 0); - let text = &rows[0]; - let clock = text.find("00:12").expect(text); - let activity = text.find('│').expect(text); + let text = rows[0].trim_end(); + let offset = text.find("12s").expect(text); + let clock = unicode_width::UnicodeWidthStr::width(&text[..offset]); assert_eq!(*clock_column.get_or_insert(clock), clock, "{text}"); - assert_eq!(*activity_column.get_or_insert(activity), activity, "{text}"); + assert!(!text.contains('│') && !text.contains("-/s"), "{text}"); assert!(rows[1].trim().is_empty(), "{rows:?}"); } } #[test] -fn stable_status_centered_mode_does_not_move_changing_text() { +fn compact_status_centered_mode_does_not_move_changing_text() { for status in phases() { let mut state = TestState { status, @@ -71,18 +92,16 @@ fn stable_status_centered_mode_does_not_move_changing_text() { } #[test] -fn stable_status_narrow_widths_do_not_wrap_or_panic() { +fn compact_status_narrow_unicode_widths_do_not_wrap_or_panic() { for status in phases() { let state = TestState { status, status_elapsed: Some(Duration::from_secs(360_000)), - status_background: vec![crate::tui::BackgroundTaskRow { - task_id: "bg".into(), - label: "界\nlong\tbackground intent".repeat(20), - percent: None, - status: crate::tui::BackgroundTaskRowStatus::Running, - completed_at: None, - }], + status_tool_calls: vec![tool("bash", &"界\nlong\tintent ".repeat(30))], + status_background: vec![background( + &"界\nlong\tbackground intent".repeat(20), + crate::tui::BackgroundTaskRowStatus::Running, + )], ..Default::default() }; for width in 0..=120 { @@ -94,7 +113,37 @@ fn stable_status_narrow_widths_do_not_wrap_or_panic() { } #[test] -fn stable_status_rate_limit_uses_the_same_clock_position() { +fn compact_status_connection_details_and_retry_counts_are_preserved() { + for (phase, expected) in [ + ( + crate::message::ConnectionPhase::Authenticating, + "refreshing auth", + ), + ( + crate::message::ConnectionPhase::SendingRequest, + "sending context", + ), + ( + crate::message::ConnectionPhase::WaitingForResponse, + "waiting for response", + ), + ( + crate::message::ConnectionPhase::Retrying { attempt: 2, max: 3 }, + "2/3", + ), + ] { + let state = TestState { + status: ProcessingStatus::Connecting(phase), + status_elapsed: Some(Duration::from_secs(12)), + ..Default::default() + }; + let rows = render_status(&state, 160, 0); + assert!(rows[0].contains(expected), "{rows:?}"); + } +} + +#[test] +fn compact_status_rate_limit_keeps_clock_and_human_retry_time() { let mut state = TestState { status: ProcessingStatus::Sending, status_elapsed: Some(Duration::from_secs(12)), @@ -102,52 +151,127 @@ fn stable_status_rate_limit_uses_the_same_clock_position() { }; let active = render_status(&state, 100, 0); state.status_rate_limit = Some(Duration::from_secs(80)); - let limited = render_status(&state, 100, 0); - assert_eq!(active[0].find("00:12"), limited[0].find("00:12")); - assert!(limited[0].to_lowercase().contains("retry"), "{limited:?}"); + let limited = render_status(&state, 100, 2); + assert_eq!(active[0].find("12s"), limited[0].find("12s")); + assert!( + limited[0].contains("1m 20s") && limited[0].contains("+2 queued"), + "{limited:?}" + ); } #[test] -fn stable_status_idle_does_not_rotate_tips() { - let mut state = TestState::default(); - let initial = render_status(&state, 100, 0); - for elapsed in [1.0, 15.0, 60.0, 120.0, 1000.0] { - state.anim_elapsed = elapsed; - assert_eq!(initial, render_status(&state, 100, 0)); +fn compact_status_streaming_keeps_real_tokens_speed_and_transport() { + let state = TestState { + status: ProcessingStatus::Streaming, + status_elapsed: Some(Duration::from_secs(12)), + status_tokens: (12_345, 456), + status_tps: Some(35.2), + status_transport: Some("fresh websocket".into()), + status_upstream: Some("upstream".into()), + ..Default::default() + }; + let rows = render_status(&state, 160, 0); + for expected in [ + "streaming", + "12s", + "↑12k ↓456", + "35.2 tps", + "opening websocket", + "via upstream", + ] { + assert!(rows[0].contains(expected), "missing {expected}: {rows:?}"); } + assert!( + rows[0].trim_end().len() < 140, + "unexpected padding: {rows:?}" + ); } #[test] -fn stable_status_badges_do_not_move_activity_or_telemetry() { +fn compact_status_tool_keeps_intent_subagent_shortcut_and_queue() { + let state = TestState { + status: ProcessingStatus::RunningTool("bash".into()), + status_elapsed: Some(Duration::from_secs(12)), + status_tool_calls: vec![tool("bash", "Run focused tests")], + status_tokens: (12_345, 456), + status_subagent: Some("worker fox".into()), + status_upstream: Some("upstream".into()), + ..Default::default() + }; + let rows = render_status(&state, 180, 2); + for expected in [ + "bash", + "Run focused tests", + "↑12k ↓456", + "worker fox", + "via upstream", + " bg", + "+2 queued", + ] { + assert!(rows[0].contains(expected), "missing {expected}: {rows:?}"); + } +} + +#[test] +fn compact_status_batch_keeps_completed_running_and_last_done() { + let state = TestState { + status: ProcessingStatus::RunningTool("batch".into()), + status_elapsed: Some(Duration::from_secs(12)), + batch_progress: Some(crate::bus::BatchProgress { + session_id: "fixture".into(), + tool_call_id: "batch".into(), + total: 5, + completed: 2, + last_completed: Some("read".into()), + running: vec![tool("bash", "Run tests")], + subcalls: Vec::new(), + }), + ..Default::default() + }; + let rows = render_status(&state, 180, 0); + for expected in ["batch", "2/5 done", "running:", "bash", "last done: read"] { + assert!(rows[0].contains(expected), "missing {expected}: {rows:?}"); + } +} + +#[test] +fn compact_status_background_keeps_actual_intents_and_filters_finished_tasks() { let mut state = TestState { status: ProcessingStatus::RunningTool("bash".into()), status_elapsed: Some(Duration::from_secs(12)), + status_background: vec![ + background("Run tests", crate::tui::BackgroundTaskRowStatus::Running), + background("Build app", crate::tui::BackgroundTaskRowStatus::Running), + background( + "Already done", + crate::tui::BackgroundTaskRowStatus::Completed, + ), + ], ..Default::default() }; - let before = render_status(&state, 120, 0); - state.status_background = (0..100) - .map(|index| crate::tui::BackgroundTaskRow { - task_id: index.to_string(), - label: "unbounded background intent".repeat(20), - percent: None, - status: crate::tui::BackgroundTaskRowStatus::Running, - completed_at: None, - }) - .collect(); - let after = render_status(&state, 120, 100); - assert_eq!(before[0].find("bash"), after[0].find("bash")); - assert_eq!(before[0].find('↑'), after[0].find('↑')); + let active = render_status(&state, 160, 0); + for expected in ["bg 2:", "Run tests", "Build app"] { + assert!(active[0].contains(expected), "{active:?}"); + } + assert!(!active[0].contains("Already done")); + state.status = ProcessingStatus::Idle; + let idle = render_status(&state, 160, 0); assert!( - after[0].contains("[+99+ queued]") && after[0].contains("[bg 99+]"), - "{after:?}" + idle[0].contains("Run tests") && idle[0].contains("Build app"), + "{idle:?}" + ); + assert!( + !idle[0].contains('│') && !idle[0].contains("◼ idle"), + "{idle:?}" + ); +} + +#[test] +fn compact_status_idle_has_no_dashboard_label_or_telemetry() { + let state = TestState::default(); + let rows = render_status(&state, 120, 0); + assert!( + !rows[0].contains('│') && !rows[0].contains("◼ idle") && !rows[0].contains("-/s"), + "{rows:?}" ); - state.status_background[0].status = crate::tui::BackgroundTaskRowStatus::Completed; - let completed = render_status(&state, 120, 1); - assert!(completed[0].contains("[bg 99]"), "{completed:?}"); - assert!(!completed[0].contains("unbounded")); - let without_queue = render_status(&state, 120, 0); - assert_eq!(completed[0].find("[bg"), without_queue[0].find("[bg")); - state.status = ProcessingStatus::Idle; - let idle = render_status(&state, 120, 0); - assert_eq!(without_queue[0].find("[bg"), idle[0].find("[bg")); } From 3025aafe8a7c953d62368cd6cc8e0de9b0e3a062 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:24:52 -0800 Subject: [PATCH 006/236] fix(tui): seed pinned todos before resumed session history --- .../src/tui/app/remote/server_events.rs | 8 ++ .../jcode-tui/src/tui/app/tests/todo_card.rs | 107 ++++++++++++++++++ crates/jcode-tui/src/tui/app/todos_view.rs | 10 +- crates/jcode-tui/src/tui/app/tui_lifecycle.rs | 3 + 4 files changed, 127 insertions(+), 1 deletion(-) diff --git a/crates/jcode-tui/src/tui/app/remote/server_events.rs b/crates/jcode-tui/src/tui/app/remote/server_events.rs index dd68a3569d8..df72da7f469 100644 --- a/crates/jcode-tui/src/tui/app/remote/server_events.rs +++ b/crates/jcode-tui/src/tui/app/remote/server_events.rs @@ -1854,6 +1854,11 @@ fn handle_server_event_inner( app.note_client_focus(true); let session_changed = prev_session_id.as_deref() != Some(session_id.as_str()); if session_changed { + // Drop the previous session's snapshot and polling deadlines. + // Otherwise attach can inherit a 1s local / 2s SSH todo delay. + app.remote_todos_payload = None; + app.remote_todos_requested_at = None; + app.pinned_todos_checked_at = None; app.remote_available_service_tiers = None; app.usage_reset.openai_quota_rejected = false; } @@ -1931,6 +1936,9 @@ fn handle_server_event_inner( app.swarm_plan_swarm_id = None; remote.reset_call_output_tokens_seen(); } + if session_changed { + app.refresh_pinned_todos_now(); + } // Facts seeded from the last launch are only a first-frame guess. // When History reports a different model, drop them so the snapshot // below is applied exactly as on a cold start (including the diff --git a/crates/jcode-tui/src/tui/app/tests/todo_card.rs b/crates/jcode-tui/src/tui/app/tests/todo_card.rs index d28c1363c97..2802eac2ff7 100644 --- a/crates/jcode-tui/src/tui/app/tests/todo_card.rs +++ b/crates/jcode-tui/src/tui/app/tests/todo_card.rs @@ -340,6 +340,113 @@ fn pinned_todos_payload_stays_empty_when_config_off() { let _ = crate::todo::save_todos(&session_id, &[]); } +#[test] +fn pinned_todos_render_before_resumed_server_history() { + let _env_lock = crate::storage::lock_test_env(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + let session_id = app.session.id.clone(); + crate::todo::save_todos( + &session_id, + &[pinned_band_todo( + "resume-first-frame", + "Resumed pinned task", + "pending", + )], + ) + .unwrap(); + let startup_started = std::time::Instant::now(); + let resumed = App::new_for_remote(Some(session_id.clone())); + assert!( + resumed + .pinned_todos_payload_ref() + .expect("startup constructor seeds pinned todos without server History") + .contains("Resumed pinned task") + ); + eprintln!( + "resume pinned todos ready in {:?} without server History", + startup_started.elapsed() + ); + app.is_remote = true; + app.remote_session_id = None; + app.resume_session_id = Some(session_id.clone()); + app.onboarding_flow = None; + app.onboarding_startup_checked = true; + app.push_display_message(crate::tui::DisplayMessage::system("Resumed transcript")); + + // This is the startup seed, before any server History or polling tick. + app.refresh_pinned_todos_now(); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Resumed pinned task") + ); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap(); + terminal + .draw(|frame| crate::tui::ui::draw(frame, &app)) + .unwrap(); + let text: String = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect(); + assert!(text.contains("Resumed pinned task"), "{text}"); + assert!( + !app.refresh_pinned_todos_if_needed(), + "unchanged state stays throttled" + ); + crate::todo::save_todos(&session_id, &[]).unwrap(); +} + +#[test] +fn pinned_todos_session_switch_bypasses_previous_snapshot_and_throttle() { + let _env_lock = crate::storage::lock_test_env(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + let session_id = app.session.id.clone(); + crate::todo::save_todos( + &session_id, + &[pinned_band_todo("new-task", "New session task", "pending")], + ) + .unwrap(); + app.is_remote = true; + app.remote_session_id = Some("previous-todo-session".into()); + app.receive_remote_todos( + "previous-todo-session".into(), + serde_json::json!({ + "todos": [pinned_band_todo("old-task", "Previous session task", "pending")], + "plan": {}, "goals": [] + }) + .to_string(), + ); + app.remote_todos_requested_at = Some(std::time::Instant::now()); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Previous session task") + ); + + let runtime = tokio::runtime::Runtime::new().unwrap(); + let _runtime_guard = runtime.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + let history = serde_json::from_value(serde_json::json!({ + "type": "history", "id": 1, "session_id": session_id, + "messages": [], "mcp_servers": [], "skills": [], "all_sessions": [] + })) + .unwrap(); + app.handle_server_event(history, &mut remote); + assert!(app.remote_todos_payload.is_none()); + assert!(app.remote_todos_requested_at.is_none()); + let payload = app + .pinned_todos_payload_ref() + .expect("new session todos loaded during History"); + assert!(payload.contains("New session task")); + assert!(!payload.contains("Previous session task")); + crate::todo::save_todos(&session_id, &[]).unwrap(); +} + #[test] fn pinned_todos_payload_refreshes_and_clears_with_config_and_todos() { let _env_lock = crate::storage::lock_test_env(); diff --git a/crates/jcode-tui/src/tui/app/todos_view.rs b/crates/jcode-tui/src/tui/app/todos_view.rs index 90474882cc3..a16f2541e68 100644 --- a/crates/jcode-tui/src/tui/app/todos_view.rs +++ b/crates/jcode-tui/src/tui/app/todos_view.rs @@ -20,7 +20,15 @@ impl App { crate::todo::TodoPlan, Vec, ) { - let session_id = self.active_client_session_id(); + // Local resumes know their session before the server sends History. + // SSH sessions must never read potentially unrelated local state. + let session_id = self.active_client_session_id().or_else(|| { + if crate::tui::is_ssh_remote() { + None + } else { + self.resume_session_id.as_deref() + } + }); if crate::tui::is_ssh_remote() || self.remote_todos_payload.is_some() { let payload = self .remote_todos_payload diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs index f3120c03bd5..09b9542cc44 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs @@ -1620,6 +1620,9 @@ impl App { } app.resume_session_id = resume_session; + // Paint persisted todos with the first frame, not a throttled tick after + // the server has finished loading the resumed conversation. + app.refresh_pinned_todos_now(); // After `resume_session_id`: the hint needs to know whether this is a // new session (configured effort applies) or a resumed one. app.apply_remote_header_hint(); From 325678fbc74890b16111b2f92ced64d190b345fd Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:31:21 -0800 Subject: [PATCH 007/236] Discover mobile-created cloud sessions in local resume picker --- crates/jcode-base/src/mobile_sync.rs | 144 ++++++++++++++ .../src/tui/session_picker/loading.rs | 34 +++- .../loading/mobile_inventory.rs | 152 +++++++++++++++ .../loading/mobile_inventory_tests.rs | 180 ++++++++++++++++++ 4 files changed, 509 insertions(+), 1 deletion(-) create mode 100644 crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs create mode 100644 crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index f9fb5aa2bf2..961cf83df72 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -54,6 +54,68 @@ pub struct WireMessage { pub created_at: Option, } +/// Session metadata returned by the account's mobile transcript API. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct SyncedSession { + pub session_id: String, + pub title: Option, + pub source: String, + pub host: Option, + pub cwd: Option, + pub updated_at: Option, + pub created_at: Option, +} + +/// Bounded account inventory read for UI background workers. +pub async fn discover_cloud_sessions() -> Result> { + let client = reqwest::Client::new(); + tokio::time::timeout(Duration::from_secs(8), list_cloud_sessions(&client)) + .await + .context("mobile session discovery timed out")? +} + +/// List cloud sessions synced to the configured Jcode account. +pub async fn list_cloud_sessions(client: &reqwest::Client) -> Result> { + let api_key = crate::subscription_catalog::configured_api_key() + .context("no Jcode account credential is configured")?; + list_cloud_sessions_with_endpoint(client, &subscription_api::configured_api_base(), &api_key) + .await +} + +async fn list_cloud_sessions_with_endpoint( + client: &reqwest::Client, + api_base: &str, + api_key: &str, +) -> Result> { + #[derive(Deserialize)] + struct SessionsResponse { + sessions: Vec, + } + + let response = client + .get(format!( + "{}/mobile/sessions", + api_base.trim_end_matches('/') + )) + .query(&[("limit", "500")]) + .bearer_auth(api_key) + .timeout(Duration::from_secs(30)) + .send() + .await + .context("mobile session listing failed")? + .error_for_status() + .context("mobile session listing returned an error")?; + let response: SessionsResponse = response + .json() + .await + .context("invalid mobile session listing response")?; + Ok(response + .sessions + .into_iter() + .filter(|session| session.source == "cloud" && valid_session_id(&session.session_id)) + .collect()) +} + #[derive(Debug, Clone, Serialize)] struct UploadBody<'a> { title: Option<&'a str>, @@ -436,6 +498,88 @@ mod tests { use super::*; use jcode_session_types::StoredMessage; + async fn mock_listing(status: &str, body: &str) -> (String, tokio::task::JoinHandle) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}/", listener.local_addr().unwrap()); + let response = format!( + "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + let task = tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut request = Vec::new(); + let mut buffer = [0; 1024]; + while !request.windows(4).any(|window| window == b"\r\n\r\n") { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&buffer[..count]); + } + socket.write_all(response.as_bytes()).await.unwrap(); + String::from_utf8(request).unwrap() + }); + (base, task) + } + + #[tokio::test] + async fn cloud_session_listing_filters_and_preserves_metadata() { + let body = serde_json::json!({"sessions": [ + {"session_id": "cloud-1", "source": "cloud", "title": "Title", + "host": "worker", "cwd": "/repo", "updated_at": "updated", "created_at": "created"}, + {"session_id": "cloud-2", "source": "cloud"}, + {"session_id": "local-1", "source": "local"}, + {"session_id": "../unsafe", "source": "cloud"}, + {"session_id": "", "source": "cloud"}, + {"session_id": "x".repeat(129), "source": "cloud"} + ]}) + .to_string(); + let (base, request) = mock_listing("200 OK", &body).await; + let sessions = + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "test-key") + .await + .unwrap(); + assert_eq!(sessions.len(), 2); + assert_eq!(sessions[0].session_id, "cloud-1"); + assert_eq!(sessions[0].title.as_deref(), Some("Title")); + assert_eq!(sessions[0].host.as_deref(), Some("worker")); + assert_eq!(sessions[0].cwd.as_deref(), Some("/repo")); + assert_eq!(sessions[0].updated_at.as_deref(), Some("updated")); + assert_eq!(sessions[0].created_at.as_deref(), Some("created")); + assert_eq!(sessions[1].title, None); + assert_eq!(sessions[1].created_at, None); + let request = request.await.unwrap().to_ascii_lowercase(); + assert!(request.starts_with("get /mobile/sessions?limit=500 http/1.1\r\n")); + assert!(request.contains("\r\nauthorization: bearer test-key\r\n")); + } + + #[tokio::test] + async fn cloud_session_listing_rejects_http_and_schema_errors() { + for (status, body) in [ + ("401 Unauthorized", "{\"sessions\":[]}"), + ("200 OK", "not json"), + ("200 OK", "{}"), + ("200 OK", "{\"sessions\":[{\"source\":\"cloud\"}]}"), + ("200 OK", "{\"sessions\":[{\"session_id\":\"s1\"}]}"), + ] { + let (base, request) = mock_listing(status, body).await; + assert!( + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") + .await + .is_err() + ); + request.await.unwrap(); + } + let (base, request) = mock_listing("200 OK", "{\"sessions\":[]}").await; + assert!( + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") + .await + .unwrap() + .is_empty() + ); + request.await.unwrap(); + } + fn msg(role: Role, content: Vec) -> StoredMessage { StoredMessage { id: "m".into(), diff --git a/crates/jcode-tui/src/tui/session_picker/loading.rs b/crates/jcode-tui/src/tui/session_picker/loading.rs index f0b27e18368..e96c2e072a3 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading.rs @@ -21,6 +21,8 @@ use super::{ SEARCH_CONTENT_BUDGET_BYTES, ServerGroup, SessionInfo, }; +mod mobile_inventory; + use super::{ResumeTarget, SessionSource}; #[cfg(test)] @@ -348,6 +350,35 @@ pub(crate) fn send_session_list_updates(sender: std::sync::mpsc::Sender result, + Err(error) => { + let _ = sender.send(super::SessionListUpdate::Fresh(Err(error))); + return; + } + }; + if sender + .send(super::SessionListUpdate::Cached(local.clone())) + .is_err() + { + return; + } + let result = match mobile_inventory::refresh() { + Ok(true) => { + invalidate_session_list_cache(); + load_sessions_grouped() + } + Ok(false) => Ok(local), + Err(error) => { + crate::logging::debug(&format!("Mobile resume discovery unavailable: {error:#}")); + Ok(local) + } + }; let _ = sender.send(super::SessionListUpdate::Fresh(result)); } @@ -2779,7 +2810,8 @@ fn cloud_session_stub(lease: &storage::SessionLease) -> SessionInfo { let updated_at = chrono::DateTime::parse_from_rfc3339(&lease.updated_at) .map(|time| time.with_timezone(&chrono::Utc)) .unwrap_or_default(); - let title = format!("{short_name} (cloud session)"); + let title = + mobile_inventory::title(id).unwrap_or_else(|| format!("{short_name} (cloud session)")); let search_index = build_search_index( id, &short_name, diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs new file mode 100644 index 00000000000..4fb0c6a53d0 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs @@ -0,0 +1,152 @@ +//! Discover mobile-created sessions without copying their transcripts locally. +use super::*; + +const VERIFY_SCRIPT: &str = r#" +python3 - "$1" <<'PY' +import json, os, socket, sys +from pathlib import Path +base = Path(os.environ.get('JCODE_HOME', str(Path.home() / '.jcode'))) +result = [] +for sid in sys.argv[1].splitlines(): + if not sid or not all(c.isascii() and (c.isalnum() or c in '_-') for c in sid): + continue + try: + session = json.loads((base / 'sessions' / (sid + '.json')).read_text()) + if session.get('id') != sid: + continue + lease_path = base / 'session_leases' / (sid + '.json') + lease = json.loads(lease_path.read_text()) if lease_path.exists() else {} + if lease.get('away_host'): + continue + result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir')}) + except (OSError, ValueError, TypeError): + continue +print(json.dumps({'host': socket.gethostname(), 'sessions': result})) +PY +"#; + +#[derive(Deserialize)] +struct VerifiedInventory { + host: String, + sessions: Vec, +} + +#[derive(Deserialize)] +struct VerifiedSession { + session_id: String, + epoch: u64, + cwd: Option, +} + +/// Called only after publishing local results, never on the render thread. +pub(super) fn available() -> bool { + crate::mobile_sync::session_source() != "cloud" + && crate::subscription_catalog::has_credentials() + && storage::jcode_dir().is_ok_and(|home| home.join("cloud/ssh_config").is_file()) +} + +pub(super) fn refresh() -> Result { + if !available() { + return Ok(false); + } + let home = storage::jcode_dir()?; + let config = home.join("cloud/ssh_config"); + if !config.is_file() { + return Ok(false); + } + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()?; + runtime.block_on(async { + let inventory = crate::mobile_sync::discover_cloud_sessions().await?; + let candidates: Vec<_> = inventory + .into_iter() + .filter(|s| { + storage::read_session_lease(&s.session_id).is_none() + && !home + .join("sessions") + .join(format!("{}.json", s.session_id)) + .exists() + }) + .collect(); + if candidates.is_empty() { + return Ok(false); + } + let ids = candidates + .iter() + .map(|s| s.session_id.as_str()) + .collect::>() + .join("\n"); + let command = crate::tui::cloud_git::compressed_script_command( + Some("jcode-cloud"), + Some(&config), + &ids, + VERIFY_SCRIPT, + ); + let (status, bytes) = + crate::tui::cloud_git::bounded_output(command, Duration::from_secs(10), 1024 * 1024) + .await + .ok_or_else(|| anyhow::anyhow!("cloud inventory verification unavailable"))?; + anyhow::ensure!(status.success(), "cloud inventory verification failed"); + let verified: VerifiedInventory = serde_json::from_slice(&bytes)?; + register(&home, candidates, verified) + }) +} + +fn register( + home: &Path, + candidates: Vec, + verified: VerifiedInventory, +) -> Result { + let mut changed = false; + for remote in verified.sessions { + let Some(metadata) = candidates.iter().find(|s| { + s.session_id == remote.session_id && s.host.as_deref() == Some(verified.host.as_str()) + }) else { + continue; + }; + // Recheck after network IO. Never replace a migration decision or local work. + if storage::read_session_lease(&remote.session_id).is_some() + || home + .join("sessions") + .join(format!("{}.json", remote.session_id)) + .exists() + { + continue; + } + let lease = storage::SessionLease { + session_id: remote.session_id, + epoch: remote.epoch, + away_host: Some("jcode-cloud".into()), + repo_root: remote.cwd.or_else(|| metadata.cwd.clone()), + last_sync_snapshot: None, + last_sync_head: None, + updated_at: metadata + .updated_at + .clone() + .unwrap_or_else(|| chrono::Utc::now().to_rfc3339()), + }; + storage::write_session_lease(&lease)?; + // A picker-only sidecar preserves the title, not a canonical transcript. + let path = home + .join("cloud/mobile-session-titles") + .join(format!("{}.json", lease.session_id)); + if let Some(title) = metadata.title.as_deref() { + storage::write_json(&path, &title)?; + } + changed = true; + } + Ok(changed) +} + +pub(super) fn title(id: &str) -> Option { + let path = storage::jcode_dir() + .ok()? + .join("cloud/mobile-session-titles") + .join(format!("{id}.json")); + storage::read_json(&path).ok() +} + +#[cfg(test)] +#[path = "mobile_inventory_tests.rs"] +mod tests; diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs new file mode 100644 index 00000000000..b82c23293cd --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs @@ -0,0 +1,180 @@ +use super::*; + +struct HomeGuard(Option); + +impl HomeGuard { + fn install(home: &Path) -> Self { + let previous = std::env::var_os("JCODE_HOME"); + crate::env::set_var("JCODE_HOME", home); + Self(previous) + } +} + +impl Drop for HomeGuard { + fn drop(&mut self) { + match self.0.take() { + Some(previous) => crate::env::set_var("JCODE_HOME", previous), + None => crate::env::remove_var("JCODE_HOME"), + } + super::super::invalidate_session_list_cache(); + } +} + +fn metadata(id: &str) -> crate::mobile_sync::SyncedSession { + crate::mobile_sync::SyncedSession { + session_id: id.into(), + source: "cloud".into(), + title: Some("Future orbital compiler work".into()), + host: Some("verified-worker".into()), + cwd: Some("/metadata/repository".into()), + updated_at: Some("2026-11-08T05:00:00Z".into()), + created_at: None, + } +} + +fn inventory(ids: &[(&str, u64)]) -> VerifiedInventory { + VerifiedInventory { + host: "verified-worker".into(), + sessions: ids + .iter() + .map(|(id, epoch)| VerifiedSession { + session_id: (*id).into(), + epoch: *epoch, + cwd: Some("/verified/repository".into()), + }) + .collect(), + } +} + +#[test] +fn discovered_future_session_has_verified_lease_searchable_title_and_no_transcript() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_future_orbital_1999999999999"; + assert!(register(home.path(), vec![metadata(id)], inventory(&[(id, 42)])).unwrap()); + let lease = storage::read_session_lease(id).expect("discovered cloud ownership"); + assert_eq!(lease.epoch, 42); + assert_eq!(lease.away_host.as_deref(), Some("jcode-cloud")); + assert_eq!(lease.repo_root.as_deref(), Some("/verified/repository")); + assert_eq!(lease.updated_at, "2026-11-08T05:00:00Z"); + assert!(lease.last_sync_snapshot.is_none()); + assert!(lease.last_sync_head.is_none()); + assert!( + !home + .path() + .join("sessions") + .join(format!("{id}.json")) + .exists() + ); + let stub = super::super::cloud_session_stub(&lease); + assert_eq!(stub.title, "Future orbital compiler work"); + assert!(stub.search_index.contains("orbital")); + assert!(stub.search_index.contains("compiler")); + super::super::invalidate_session_list_cache(); + let (groups, orphans) = super::super::load_sessions_grouped().unwrap(); + let group = groups + .iter() + .find(|group| group.name == "cloud: jcode-cloud") + .expect("cloud session picker group"); + assert_eq!(group.icon, "☁"); + assert_eq!(group.sessions.len(), 1); + assert_eq!(group.sessions[0].id, id); + assert!(group.sessions[0].search_index.contains("orbital")); + assert!(!orphans.iter().any(|session| session.id == id)); + assert!( + !home + .path() + .join("sessions") + .join(format!("{id}.json")) + .exists() + ); +} + +#[test] +fn registration_is_idempotent_and_preserves_returned_leases_and_local_snapshots() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_inventory_idempotent"; + assert!(register(home.path(), vec![metadata(id)], inventory(&[(id, 7)])).unwrap()); + let lease_path = home + .path() + .join("session_leases") + .join(format!("{id}.json")); + let original = std::fs::read(&lease_path).unwrap(); + let mut changed_metadata = metadata(id); + changed_metadata.title = Some("Must not replace original title".into()); + assert!(!register(home.path(), vec![changed_metadata], inventory(&[(id, 99)])).unwrap()); + assert_eq!(std::fs::read(&lease_path).unwrap(), original); + assert_eq!(title(id).as_deref(), Some("Future orbital compiler work")); + + let returned_id = "session_inventory_returned"; + let returned = storage::SessionLease { + session_id: returned_id.into(), + epoch: 100, + away_host: None, + repo_root: Some("/local/returned".into()), + last_sync_snapshot: Some("snapshot".into()), + last_sync_head: Some("head".into()), + updated_at: "2026-11-08T05:10:00Z".into(), + }; + storage::write_session_lease(&returned).unwrap(); + let returned_path = home + .path() + .join("session_leases") + .join(format!("{returned_id}.json")); + let returned_bytes = std::fs::read(&returned_path).unwrap(); + let local_id = "session_inventory_local"; + let local_path = home + .path() + .join("sessions") + .join(format!("{local_id}.json")); + std::fs::create_dir_all(local_path.parent().unwrap()).unwrap(); + let snapshot = b"{\"id\":\"session_inventory_local\",\"messages\":[]}"; + std::fs::write(&local_path, snapshot).unwrap(); + assert!( + !register( + home.path(), + vec![metadata(returned_id), metadata(local_id)], + inventory(&[(returned_id, 101), (local_id, 12)]) + ) + .unwrap() + ); + assert_eq!(std::fs::read(&returned_path).unwrap(), returned_bytes); + assert_eq!(std::fs::read(&local_path).unwrap(), snapshot); + assert!(storage::read_session_lease(local_id).is_none()); + assert!(title(returned_id).is_none()); + assert!(title(local_id).is_none()); +} + +#[test] +fn registration_skips_mismatched_hosts_and_unverified_missing_sessions() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let mismatch_id = "session_inventory_wrong_host"; + let missing_id = "session_inventory_missing"; + let unknown_id = "session_inventory_unknown"; + let mut mismatch = metadata(mismatch_id); + mismatch.host = Some("different-worker".into()); + assert!( + !register( + home.path(), + vec![mismatch, metadata(missing_id)], + inventory(&[(mismatch_id, 2), (unknown_id, 3)]) + ) + .unwrap() + ); + for id in [mismatch_id, missing_id, unknown_id] { + assert!(storage::read_session_lease(id).is_none()); + assert!(title(id).is_none()); + assert!( + !home + .path() + .join("sessions") + .join(format!("{id}.json")) + .exists() + ); + } +} From f42f41ce711b1da890e92c97feda277d95258a35 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:41:49 -0800 Subject: [PATCH 008/236] Reuse authenticated SSH startup connection and managed mux --- src/cli/ssh_transport.rs | 184 +++++++++++++++++++++++++++++++-------- 1 file changed, 148 insertions(+), 36 deletions(-) diff --git a/src/cli/ssh_transport.rs b/src/cli/ssh_transport.rs index b5bf69bf43e..e0e2ac9bc43 100644 --- a/src/cli/ssh_transport.rs +++ b/src/cli/ssh_transport.rs @@ -119,9 +119,8 @@ impl NativeSsh { } options.working_dir = remote_working_dir.map(str::to_owned); // Fail before entering the TUI, with authentication/host-key diagnostics. - let mut probe = SshConnection::connect(&options).await?; + let probe = SshConnection::connect(&options).await?; let handshake = probe.handshake.clone(); - probe.shutdown().await; // Remote --cwd already verified the directory and the header contains // its resolved path. Keep reconnects on that same verified workspace. options.working_dir = Some(handshake.working_dir.clone()); @@ -130,7 +129,7 @@ impl NativeSsh { let socket = directory.path().join("native.sock"); let listener = UnixListener::bind(&socket)?; let (stop, stopped) = watch::channel(false); - let manager = tokio::spawn(accept_connections(listener, options, stopped)); + let manager = tokio::spawn(accept_connections(listener, options, stopped, Some(probe))); Ok(Self { _directory: directory, socket, @@ -229,6 +228,7 @@ impl SshOptions { fn command(&self) -> Command { let mut command = Command::new("ssh"); + let mut managed_config = false; if let Some(id) = super::cloud_ephemeral::host_id(&self.host) { // The caller validates/prepares this private config before connecting. // Never fall through to the persistent host's SSH identity. @@ -239,38 +239,9 @@ impl SshOptions { && let Ok(args) = super::cloud_managed::ssh_args() { command.args(args); + managed_config = true; } - command.args([ - "-T", - "-o", - "BatchMode=yes", - "-o", - "StrictHostKeyChecking=yes", - "-o", - "ForwardAgent=no", - "-o", - "ClearAllForwardings=yes", - "-o", - "ServerAliveInterval=15", - "-o", - "ServerAliveCountMax=2", - "-o", - "PermitLocalCommand=no", - "-o", - "ForkAfterAuthentication=no", - "-o", - "StdinNull=no", - "-o", - "RemoteCommand=none", - "-o", - "SessionType=default", - "-o", - "ControlMaster=no", - "-S", - "none", - "-o", - "ConnectTimeout=30", - ]); + configure_transport(&mut command, managed_config); let binary = format!("'{}'", self.remote_binary.replace('\'', "'\\''")); let socket = self .daemon_socket @@ -290,6 +261,49 @@ impl SshOptions { } } +fn configure_transport(command: &mut Command, managed_config: bool) { + command.args([ + "-T", + "-C", + "-o", + "BatchMode=yes", + "-o", + "StrictHostKeyChecking=yes", + "-o", + "ForwardAgent=no", + "-o", + "ClearAllForwardings=yes", + "-o", + "ServerAliveInterval=15", + "-o", + "ServerAliveCountMax=2", + "-o", + "PermitLocalCommand=no", + "-o", + "ForkAfterAuthentication=no", + "-o", + "StdinNull=no", + "-o", + "RemoteCommand=none", + "-o", + "SessionType=default", + "-o", + "ControlMaster=no", + "-o", + "ControlPersist=no", + "-o", + "ConnectTimeout=30", + ]); + // Only the generated managed config has an account/API-scoped path in + // a private directory. Borrow an existing master, never create one or + // send it exit/stop commands. The owned child is just its session client. + // With no master, ControlMaster=no falls back to an owned direct SSH. + // Compression on a borrowed master is determined when it was created. + if !managed_config { + command.args(["-S", "none"]); + } +} + struct OwnedChild(Child); impl OwnedChild { fn kill(&mut self) { @@ -510,6 +524,7 @@ async fn accept_connections( listener: UnixListener, options: SshOptions, mut stopped: watch::Receiver, + mut initial: Option, ) { let mut connections = JoinSet::new(); let backoff = Arc::new(Mutex::new(ReconnectBackoff::default())); @@ -529,8 +544,10 @@ async fn accept_connections( let backoff = backoff.clone(); let options = options.clone(); let mut stopped = stopped.clone(); + let initial = initial.take(); connections.spawn(async move { - let mut ssh = match SshConnection::spawn_process(options.command()) { + let established = initial.is_some(); + let mut ssh = match initial.map(Ok).unwrap_or_else(|| SshConnection::spawn_process(options.command())) { Ok(ssh) => ssh, Err(error) => { let (log, failures) = record_reconnect_failure(&backoff); @@ -541,7 +558,7 @@ async fn accept_connections( let connected = tokio::select! { biased; _ = stopped.changed() => None, - connected = ssh.establish(STARTUP_TIMEOUT) => Some(connected), + connected = async { if established { Ok(()) } else { ssh.establish(STARTUP_TIMEOUT).await } } => Some(connected), }; if !matches!(connected, Some(Ok(()))) { ssh.shutdown().await; @@ -572,6 +589,9 @@ async fn accept_connections( } } drop(listener); + if let Some(mut ssh) = initial.take() { + ssh.shutdown().await; + } // Guard drop wakes all children, which explicitly kill and reap SSH. while connections.join_next().await.is_some() {} } @@ -658,6 +678,98 @@ async fn bridge_stream( mod tests { use super::*; + #[test] + fn managed_mux_only_borrows_existing_master_and_enables_compression() { + for managed in [false, true] { + let mut command = Command::new("ssh"); + configure_transport(&mut command, managed); + let args: Vec<_> = command + .as_std() + .get_args() + .map(|s| s.to_str().unwrap()) + .collect(); + for required in [ + "-C", + "ControlMaster=no", + "ControlPersist=no", + "ForwardAgent=no", + "ClearAllForwardings=yes", + ] { + assert!(args.contains(&required), "{required}"); + } + assert_eq!(args.contains(&"-S"), !managed); + assert!(!args.contains(&"-O")); + } + } + + #[tokio::test] + async fn accepted_socket_reuses_probe_and_preserves_buffered_bytes() { + let mut probe = SshConnection::spawn( + shell(&format!( + "printf '%s' '{}'; exec cat", + hello() + "buffered-history\n" + )), + Duration::from_secs(2), + ) + .await + .unwrap(); + let pid = probe.child.0.id().unwrap(); + // Ensure bytes past the handshake are already in the BufReader. + assert!(!probe.reader.fill_buf().await.unwrap().is_empty()); + let directory = private_directory().unwrap(); + let socket = directory.path().join("native.sock"); + let listener = UnixListener::bind(&socket).unwrap(); + let (stop, stopped) = watch::channel(false); + // Invalid hostname must never be contacted for the first socket. + let manager = tokio::spawn(accept_connections( + listener, + SshOptions::new("unused.invalid", "jcode").unwrap(), + stopped, + Some(probe), + )); + let mut stream = UnixStream::connect(&socket).await.unwrap(); + stream.write_all(b"request\n").await.unwrap(); + let mut received = vec![0; b"buffered-history\nrequest\n".len()]; + tokio::time::timeout(Duration::from_secs(2), stream.read_exact(&mut received)) + .await + .unwrap() + .unwrap(); + assert_eq!(received, b"buffered-history\nrequest\n"); + stop.send(true).unwrap(); + tokio::time::timeout(Duration::from_secs(3), manager) + .await + .unwrap() + .unwrap(); + assert_eq!(unsafe { libc::kill(pid as i32, 0) }, -1); + } + + #[tokio::test] + async fn unused_probe_is_reaped_when_manager_stops() { + let probe = SshConnection::spawn( + shell(&format!("printf '%s' '{}'; exec sleep 60", hello())), + Duration::from_secs(2), + ) + .await + .unwrap(); + let pid = probe.child.0.id().unwrap(); + let directory = private_directory().unwrap(); + let listener = UnixListener::bind(directory.path().join("native.sock")).unwrap(); + let (stop, stopped) = watch::channel(false); + stop.send(true).unwrap(); + tokio::time::timeout( + Duration::from_secs(3), + accept_connections( + listener, + SshOptions::new("unused.invalid", "jcode").unwrap(), + stopped, + Some(probe), + ), + ) + .await + .unwrap(); + assert_eq!(unsafe { libc::kill(pid as i32, 0) }, -1); + } + #[test] fn reconnect_backoff_grows_caps_and_throttles_logs() { let now = std::time::Instant::now(); From 9d08c53f90ab0d8dcb0ce30e52f65aff77250cc1 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:43:03 -0800 Subject: [PATCH 009/236] mcp: never open OAuth browser windows from background auto-connects --- crates/jcode-app-core/src/tool/mcp.rs | 2 +- crates/jcode-base/src/mcp/client.rs | 61 ++++++++++++++++++++++++++- crates/jcode-base/src/mcp/mod.rs | 4 +- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/crates/jcode-app-core/src/tool/mcp.rs b/crates/jcode-app-core/src/tool/mcp.rs index 22469c35dd7..0ec82ba2b8c 100644 --- a/crates/jcode-app-core/src/tool/mcp.rs +++ b/crates/jcode-app-core/src/tool/mcp.rs @@ -931,7 +931,7 @@ impl McpManagementTool { // Connect let manager = self.manager.read().await; - match manager.connect(&server_name, &config).await { + match crate::mcp::interactive_connect(manager.connect(&server_name, &config)).await { Ok(()) => { let tools = manager.all_tools().await; let connected = manager.connected_servers().await; diff --git a/crates/jcode-base/src/mcp/client.rs b/crates/jcode-base/src/mcp/client.rs index a26d382b588..2140a204156 100644 --- a/crates/jcode-base/src/mcp/client.rs +++ b/crates/jcode-base/src/mcp/client.rs @@ -211,9 +211,16 @@ impl McpClient { // its process environment, and blindly inheriting them exposes those // credentials to every configured MCP executable (issue #771). let inherited: HashMap = std::env::vars().collect(); - let env = mcp_child_env(inherited, &config.env); + let mut env = mcp_child_env(inherited, &config.env); + let interactive = INTERACTIVE_CONNECT.try_with(|v| *v).unwrap_or(false); + if !interactive { + suppress_browser_launch(&mut env, &config.env); + } let mut command = Command::new(&config.command); + if !interactive { + command.env_remove("DISPLAY").env_remove("WAYLAND_DISPLAY"); + } command .args(&config.args) .envs(&env) @@ -590,6 +597,34 @@ impl McpClient { } } +tokio::task_local! { + /// Set while a user explicitly asked to connect a server. Background + /// auto-connects (daemon startup, session start) leave it unset. + static INTERACTIVE_CONNECT: bool; +} + +/// Run `fut` as a user-initiated connect, allowing MCP children (for example +/// `mcp-remote`) to open an OAuth browser window. +pub async fn interactive_connect(fut: F) -> F::Output { + INTERACTIVE_CONNECT.scope(true, fut).await +} + +/// Background auto-connects must never pop browser windows. `mcp-remote` and +/// similar wrappers open an OAuth page whenever tokens are missing, which made +/// every reboot spawn a pile of sign-in tabs. Hide the graphical session and +/// point `BROWSER` at a no-op so openers fail and the server reports an auth +/// error instead. Explicitly configured env values are respected. +fn suppress_browser_launch(env: &mut HashMap, explicit: &HashMap) { + for key in ["DISPLAY", "WAYLAND_DISPLAY"] { + if !explicit.contains_key(key) { + env.remove(key); + } + } + if !explicit.contains_key("BROWSER") { + env.insert("BROWSER".to_string(), "true".to_string()); + } +} + /// Secrets that an MCP child must not receive merely because jcode has them. /// /// This intentionally applies only to inherited values. A server can still be @@ -632,6 +667,30 @@ mod tests { use crate::mcp::protocol::McpServerConfig; use std::collections::HashMap; + #[test] + fn background_connect_env_cannot_open_browser() { + let mut env: HashMap = [ + ("DISPLAY", ":0"), + ("WAYLAND_DISPLAY", "wayland-1"), + ("PATH", "/usr/bin"), + ] + .into_iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + super::suppress_browser_launch(&mut env, &HashMap::new()); + assert!(!env.contains_key("DISPLAY")); + assert!(!env.contains_key("WAYLAND_DISPLAY")); + assert_eq!(env.get("BROWSER").map(String::as_str), Some("true")); + assert_eq!(env.get("PATH").map(String::as_str), Some("/usr/bin")); + + let explicit: HashMap = [("DISPLAY".to_string(), ":1".to_string())] + .into_iter() + .collect(); + let mut env = explicit.clone(); + super::suppress_browser_launch(&mut env, &explicit); + assert_eq!(env.get("DISPLAY").map(String::as_str), Some(":1")); + } + #[test] fn inherited_mcp_env_scrubs_provider_credentials() { for key in [ diff --git a/crates/jcode-base/src/mcp/mod.rs b/crates/jcode-base/src/mcp/mod.rs index fe0ea3edadb..f20dd62f8e7 100644 --- a/crates/jcode-base/src/mcp/mod.rs +++ b/crates/jcode-base/src/mcp/mod.rs @@ -15,7 +15,9 @@ mod remote; pub mod schema_cache; mod tool; -pub use client::{DEFAULT_MCP_REQUEST_TIMEOUT, McpClient, McpHandle, request_timeout_for}; +pub use client::{ + DEFAULT_MCP_REQUEST_TIMEOUT, McpClient, McpHandle, interactive_connect, request_timeout_for, +}; pub use manager::McpManager; pub use pool::{SharedMcpPool, get_shared_pool, init_shared_pool}; pub use protocol::*; From 6428b57b3e98e59e4d1a4c9666254e77281c1be7 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:43:45 -0800 Subject: [PATCH 010/236] Send authoritative resume todos before large history replay --- .../src/server/client_session.rs | 41 +++++----- .../resume/busy_existing_attach.rs | 21 ++++++ .../jcode-app-core/src/server/client_state.rs | 30 ++++++++ .../src/server/client_state_tests.rs | 52 ++++++++++++- .../src/tui/app/remote/server_events.rs | 10 ++- .../jcode-tui/src/tui/app/tests/todo_card.rs | 74 +++++++++++++++++++ crates/jcode-tui/src/tui/app/todos_view.rs | 19 +++-- 7 files changed, 217 insertions(+), 30 deletions(-) diff --git a/crates/jcode-app-core/src/server/client_session.rs b/crates/jcode-app-core/src/server/client_session.rs index 32a66ce4085..5c28a82cbb1 100644 --- a/crates/jcode-app-core/src/server/client_session.rs +++ b/crates/jcode-app-core/src/server/client_session.rs @@ -1,6 +1,6 @@ #![cfg_attr(test, allow(clippy::await_holding_lock))] -use super::client_state::{handle_get_history, spawn_model_prefetch_update}; +use super::client_state::{handle_get_history, send_resume_todos, spawn_model_prefetch_update}; use super::swarm::notify_owner_of_unrestored_idle_workers; use super::{ ClientConnectionInfo, ClientDebugState, FileTouchService, SessionInterruptQueues, SwarmEvent, @@ -1495,6 +1495,7 @@ pub(super) async fn handle_resume_session( *client_session_id = session_id.clone(); + send_resume_todos(&session_id, writer).await?; handle_get_history( id, &session_id, @@ -1956,23 +1957,27 @@ pub(super) async fn handle_resume_session( )); } let pre_history_ms = resume_start.elapsed().as_millis(); - let history_result = handle_get_history( - id, - &session_id, - false, - agent, - provider, - sessions, - client_connections, - client_count, - writer, - server_name, - server_icon, - Some(was_interrupted), - supports_pdf_panels, - supports_turn_identity, - history_base, - ) + let history_result = async { + send_resume_todos(&session_id, writer).await?; + handle_get_history( + id, + &session_id, + false, + agent, + provider, + sessions, + client_connections, + client_count, + writer, + server_name, + server_icon, + Some(was_interrupted), + supports_pdf_panels, + supports_turn_identity, + history_base, + ) + .await + } .await; // Persist even if history delivery failed, but do not make inbound // controls wait for a recovery turn that already owns the agent. diff --git a/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs b/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs index c11689c8222..e588177f346 100644 --- a/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs +++ b/crates/jcode-app-core/src/server/client_session_tests/resume/busy_existing_attach.rs @@ -217,6 +217,27 @@ async fn handle_resume_session_allows_live_attach_when_existing_agent_is_busy() .await .expect("history should be written promptly")?; let event: ServerEvent = serde_json::from_str(line.trim())?; + match event { + ServerEvent::Todos { + session_id, + payload, + } => { + assert_eq!(session_id, target_session_id); + let payload: serde_json::Value = serde_json::from_str(&payload)?; + assert!(payload["todos"].is_array()); + } + other => { + panic!("authoritative todos must precede the potentially huge History, got {other:?}") + } + } + line.clear(); + tokio::time::timeout( + std::time::Duration::from_secs(1), + tokio::io::AsyncBufReadExt::read_line(&mut peer_reader, &mut line), + ) + .await + .expect("history follows the early todo snapshot")?; + let event: ServerEvent = serde_json::from_str(line.trim())?; match event { ServerEvent::History { session_id, diff --git a/crates/jcode-app-core/src/server/client_state.rs b/crates/jcode-app-core/src/server/client_state.rs index 7ce2cb64947..011f5d90307 100644 --- a/crates/jcode-app-core/src/server/client_state.rs +++ b/crates/jcode-app-core/src/server/client_state.rs @@ -25,6 +25,36 @@ use tokio::sync::{Mutex, RwLock}; const ATTACH_MODEL_PREFETCH_DEBOUNCE_SECS: u64 = 15; const RELOAD_RESTORE_MARKER_MAX_AGE: Duration = Duration::from_secs(60); +/// Resume chrome must not queue behind a multi-megabyte History frame. Write +/// the small, session-bound authoritative snapshot before acquiring that frame's +/// writer lock. This does not require the agent lock, even during a live turn. +pub(super) async fn send_resume_todos( + session_id: &str, + writer: &Arc>, +) -> Result<()> { + let started = Instant::now(); + let payload = serde_json::json!({ + "todos": crate::todo::load_todos(session_id).unwrap_or_else(|error| { + crate::logging::warn(&format!("Failed to load resume todos for {session_id}: {error}")); + Vec::new() + }), + "plan": crate::todo::load_plan(session_id).unwrap_or_default(), + "goals": crate::todo::load_goals(session_id).unwrap_or_default(), + }); + let event = ServerEvent::Todos { + session_id: session_id.to_owned(), + payload: payload.to_string(), + }; + let json = encode_event(&event); + writer.lock().await.write_all(json.as_bytes()).await?; + crate::logging::info(&format!( + "[TIMING] resume todos: session={session_id}, bytes={}, total={}ms", + json.len(), + started.elapsed().as_millis(), + )); + Ok(()) +} + /// Do not probe any identity until telemetry is enabled. Only the provider /// may identify its effective OAuth credential. An API-key route must not expose the email /// of an unrelated saved subscription account. diff --git a/crates/jcode-app-core/src/server/client_state_tests.rs b/crates/jcode-app-core/src/server/client_state_tests.rs index cebc7ee26f6..c6d128090cb 100644 --- a/crates/jcode-app-core/src/server/client_state_tests.rs +++ b/crates/jcode-app-core/src/server/client_state_tests.rs @@ -11,12 +11,62 @@ use async_trait::async_trait; use std::collections::HashMap; use std::io::BufRead as _; use std::sync::Arc; -use std::time::Instant; +use std::time::{Duration, Instant}; use tokio::io::AsyncReadExt; use tokio::sync::{Mutex, RwLock, mpsc}; struct MockProvider(Option<&'static str>); +#[tokio::test] +async fn resume_todos_arrive_before_backpressured_large_history() { + let _env = crate::storage::lock_test_env(); + let temp_home = tempfile::TempDir::new().unwrap(); + let previous_home = std::env::var_os("JCODE_HOME"); + crate::env::set_var("JCODE_HOME", temp_home.path()); + let session_id = "session_early_todo_backpressure"; + let item: crate::todo::TodoItem = serde_json::from_value(serde_json::json!({ + "id": "resume", "content": "Authoritative remote plan", "status": "in_progress", "priority": "high" + })).unwrap(); + crate::todo::save_todos(session_id, &[item]).unwrap(); + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + let writer = Arc::new(Mutex::new(write)); + let started = Instant::now(); + let transfer = tokio::spawn(async move { + super::send_resume_todos(session_id, &writer).await.unwrap(); + // An unread transcript cannot fit in the socket, reproducing the + // original head-of-line blocking without any model or cloud traffic. + tokio::io::AsyncWriteExt::write_all(&mut *writer.lock().await, &vec![b'x'; 14_000_000]) + .await + }); + let mut reader = tokio::io::BufReader::new(peer); + let mut line = String::new(); + tokio::time::timeout( + Duration::from_secs(1), + tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line), + ) + .await + .expect("todos must not wait for history transfer") + .unwrap(); + let elapsed = started.elapsed(); + let event: crate::protocol::ServerEvent = serde_json::from_str(&line).unwrap(); + assert!( + matches!(event, crate::protocol::ServerEvent::Todos { session_id: id, payload } + if id == session_id && payload.contains("Authoritative remote plan")) + ); + assert!( + !transfer.is_finished(), + "large transcript should still be blocked while todos are usable" + ); + eprintln!("authoritative todo arrival with 14 MB History blocked: {elapsed:?}"); + transfer.abort(); + let _ = transfer.await; + match previous_home { + Some(home) => crate::env::set_var("JCODE_HOME", home), + None => crate::env::remove_var("JCODE_HOME"), + } +} + #[derive(Clone)] struct StaticDiscoveryProvider(Arc); diff --git a/crates/jcode-tui/src/tui/app/remote/server_events.rs b/crates/jcode-tui/src/tui/app/remote/server_events.rs index df72da7f469..45f239caed9 100644 --- a/crates/jcode-tui/src/tui/app/remote/server_events.rs +++ b/crates/jcode-tui/src/tui/app/remote/server_events.rs @@ -1856,7 +1856,15 @@ fn handle_server_event_inner( if session_changed { // Drop the previous session's snapshot and polling deadlines. // Otherwise attach can inherit a 1s local / 2s SSH todo delay. - app.remote_todos_payload = None; + // A resume sends its authoritative todo snapshot before the + // large History frame. Keep that snapshot, never the old one. + if app + .remote_todos_payload + .as_ref() + .is_some_and(|(id, _)| id != &session_id) + { + app.remote_todos_payload = None; + } app.remote_todos_requested_at = None; app.pinned_todos_checked_at = None; app.remote_available_service_tiers = None; diff --git a/crates/jcode-tui/src/tui/app/tests/todo_card.rs b/crates/jcode-tui/src/tui/app/tests/todo_card.rs index 2802eac2ff7..b139cc6ef92 100644 --- a/crates/jcode-tui/src/tui/app/tests/todo_card.rs +++ b/crates/jcode-tui/src/tui/app/tests/todo_card.rs @@ -1,5 +1,79 @@ // Tests for the inline chat todo card (`/todos` command + todo-card hotkey). +#[test] +fn ssh_pinned_todos_arrive_before_history_and_survive_it() { + if super::commands_dispatch::ssh_test_runs_in_child( + "ssh_pinned_todos_arrive_before_history_and_survive_it", + ) { + return; + } + let _env_lock = crate::storage::lock_test_env(); + let _render_lock = crate::tui::ui::render_state_test_lock(); + let _pin = PinTodosEnvGuard::enable(); + let mut app = create_test_app(); + app.is_remote = true; + app.remote_session_id = None; + app.resume_session_id = Some("remote-early-todos".into()); + app.onboarding_flow = None; + app.onboarding_startup_checked = true; + crate::todo::save_todos( + "remote-early-todos", + &[pinned_band_todo( + "local", + "Unrelated local todo must not leak", + "pending", + )], + ) + .unwrap(); + app.refresh_pinned_todos_now(); + assert!(app.pinned_todos_payload_ref().is_none()); + let payload = serde_json::json!({ + "todos": [pinned_band_todo("remote", "Authoritative early remote task", "pending")], + "plan": {}, "goals": [] + }) + .to_string(); + app.receive_remote_todos("wrong-remote-session".into(), payload.clone()); + assert!(app.remote_todos_payload.is_none()); + let started = std::time::Instant::now(); + app.receive_remote_todos("remote-early-todos".into(), payload); + assert!( + app.remote_session_id.is_none(), + "todo snapshot must not claim the connection's session" + ); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap(); + let text = render_and_snap(&app, &mut terminal); + assert!(text.contains("Authoritative early remote task"), "{text}"); + assert!(!text.contains("Unrelated local todo"), "{text}"); + assert!(started.elapsed() < std::time::Duration::from_secs(1)); + eprintln!( + "authoritative pre-History todo rendered in {:?}", + started.elapsed() + ); + let runtime = tokio::runtime::Runtime::new().unwrap(); + let _runtime_guard = runtime.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + let history = serde_json::from_value(serde_json::json!({ + "type": "history", "id": 1, "session_id": "remote-early-todos", + "messages": [], "mcp_servers": [], "skills": [], "all_sessions": [] + })) + .unwrap(); + app.handle_server_event(history, &mut remote); + assert!(app.remote_todos_payload.is_some()); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Authoritative early remote task") + ); + // An old payload must still be rejected after the session is established. + app.receive_remote_todos("wrong-remote-session".into(), "{}".into()); + assert!( + app.pinned_todos_payload_ref() + .unwrap() + .contains("Authoritative early remote task") + ); + crate::todo::save_todos("remote-early-todos", &[]).unwrap(); +} + #[test] fn ssh_todos_command_renders_remote_card() { if super::commands_dispatch::ssh_test_runs_in_child("ssh_todos_command_renders_remote_card") { diff --git a/crates/jcode-tui/src/tui/app/todos_view.rs b/crates/jcode-tui/src/tui/app/todos_view.rs index a16f2541e68..513b73efdf1 100644 --- a/crates/jcode-tui/src/tui/app/todos_view.rs +++ b/crates/jcode-tui/src/tui/app/todos_view.rs @@ -20,15 +20,11 @@ impl App { crate::todo::TodoPlan, Vec, ) { - // Local resumes know their session before the server sends History. - // SSH sessions must never read potentially unrelated local state. - let session_id = self.active_client_session_id().or_else(|| { - if crate::tui::is_ssh_remote() { - None - } else { - self.resume_session_id.as_deref() - } - }); + // A session-bound remote snapshot can arrive before History. SSH still + // never reads local todo files, including for the requested resume id. + let session_id = self + .active_client_session_id() + .or(self.resume_session_id.as_deref()); if crate::tui::is_ssh_remote() || self.remote_todos_payload.is_some() { let payload = self .remote_todos_payload @@ -84,7 +80,10 @@ impl App { } pub(super) fn receive_remote_todos(&mut self, session_id: String, payload: String) { - if self.active_client_session_id() != Some(session_id.as_str()) { + let expected = self + .active_client_session_id() + .or(self.resume_session_id.as_deref()); + if expected != Some(session_id.as_str()) { return; } self.remote_todos_payload = Some((session_id, payload)); From 438398e1f9c81673530a2bb54b88c261dd2acf1f Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:45:22 -0800 Subject: [PATCH 011/236] Prepare managed cloud SSH locally for fresh resume clients --- src/cli/args.rs | 3 ++ src/cli/cloud_managed.rs | 99 ++++++++++++++++++++++++++++++++++++++++ src/cli/cloud_move.rs | 1 + 3 files changed, 103 insertions(+) diff --git a/src/cli/args.rs b/src/cli/args.rs index 1d6c349be6f..95e5ac1bb63 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -1049,6 +1049,9 @@ pub(crate) enum CloudMachineAction { Status, Wake, Stop, + /// Internal: prepare local managed SSH credentials without contacting cloud. + #[value(hide = true)] + PrepareSsh, } #[derive(Parser, Debug, Clone, Default)] diff --git a/src/cli/cloud_managed.rs b/src/cli/cloud_managed.rs index 202f8ef1113..f2be4717d51 100644 --- a/src/cli/cloud_managed.rs +++ b/src/cli/cloud_managed.rs @@ -66,6 +66,11 @@ pub(crate) fn ssh_config_path() -> Result { Ok(dir()?.join("ssh_config")) } +/// Prepare only local SSH files. Do not provision, wake, authorize, or connect. +pub(crate) fn prepare_ssh() -> Result<()> { + ssh_args().map(|_| ()) +} + /// Ensure the client key and private SSH config exist. Returns `-F ` /// arguments for an `ssh` invocation targeting [`HOST`]. pub(crate) fn ssh_args() -> Result> { @@ -811,6 +816,100 @@ mod upgrade_tests; mod tests { use super::*; + #[test] + fn prepare_ssh_cli_is_hidden_and_only_prepares_local_files() { + use super::super::args::{Args, CloudCommand, CloudMachineAction}; + use clap::{Parser, ValueEnum}; + + let _lock = crate::storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let base = format!("https://{}/v1", listener.local_addr().unwrap()); + struct RestoreEnv(Vec<(&'static str, Option)>); + impl Drop for RestoreEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + match value { + Some(value) => crate::env::set_var(name, value), + None => crate::env::remove_var(name), + } + } + } + } + let _restore = RestoreEnv( + ["JCODE_HOME", "JCODE_API_KEY", "JCODE_API_BASE"] + .into_iter() + .map(|name| (name, std::env::var_os(name))) + .collect(), + ); + crate::env::set_var("JCODE_HOME", home.path()); + crate::env::set_var("JCODE_API_KEY", "offline-test-account"); + crate::env::set_var("JCODE_API_BASE", &base); + + let args = Args::try_parse_from([ + "jcode", + "--no-update", + "--no-selfdev", + "cloud", + "machine", + "prepare-ssh", + ]) + .unwrap(); + assert!(matches!( + args.command, + Some(super::super::args::Command::Cloud(CloudCommand::Machine { + action: CloudMachineAction::PrepareSsh + })) + )); + assert!( + CloudMachineAction::PrepareSsh + .to_possible_value() + .unwrap() + .is_hide_set() + ); + + super::super::cloud_move::run_cli(CloudCommand::Machine { + action: CloudMachineAction::PrepareSsh, + }) + .unwrap(); + let config = ssh_config_path().unwrap(); + let original_key = std::fs::read(key_path().unwrap()).unwrap(); + assert!(key_path().unwrap().with_extension("pub").is_file()); + assert!( + std::fs::read_to_string(&config) + .unwrap() + .contains("cloud proxy") + ); + assert!(!known_hosts_path().unwrap().exists()); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + for (path, mode) in [ + (dir().unwrap(), 0o700), + (config.clone(), 0o600), + (key_path().unwrap(), 0o600), + ] { + assert_eq!( + std::fs::metadata(path).unwrap().permissions().mode() & 0o777, + mode + ); + } + } + std::fs::write(&config, "stale config").unwrap(); + prepare_ssh().unwrap(); + assert!( + std::fs::read_to_string(config) + .unwrap() + .contains("Host jcode-cloud") + ); + assert_eq!(std::fs::read(key_path().unwrap()).unwrap(), original_key); + assert_eq!( + listener.accept().unwrap_err().kind(), + std::io::ErrorKind::WouldBlock + ); + } + #[test] fn proxy_executable_survives_replaced_binary() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/cli/cloud_move.rs b/src/cli/cloud_move.rs index cc5fe446481..f4343c92091 100644 --- a/src/cli/cloud_move.rs +++ b/src/cli/cloud_move.rs @@ -2249,6 +2249,7 @@ pub(crate) fn run_cli(command: super::args::CloudCommand) -> Result<()> { C::Machine { action } => { use super::args::CloudMachineAction as A; let reply = match action { + A::PrepareSsh => return super::cloud_managed::prepare_ssh(), A::Status => super::cloud_managed::status()?, A::Wake => super::cloud_managed::connect(&mut |m| eprintln!("{m}"))?, A::Stop => super::cloud_managed::stop()?, From 096ef173d0be782cf8e2eee5dc5c093d7060e9eb Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:46:32 -0800 Subject: [PATCH 012/236] Improve usage snapshots, session picker mobile inventory, and cloud transport --- crates/jcode-base/src/prompt/system_prompt.md | 3 +- crates/jcode-base/src/usage.rs | 5 +- .../jcode-base/src/usage/remote_accounts.rs | 72 +++++- .../jcode-base/src/usage/startup_snapshot.rs | 13 +- crates/jcode-provider-anthropic/src/lib.rs | 5 +- .../src/oauth_tool_schema_tests.rs | 5 +- crates/jcode-sdk/src/cloud.rs | 26 ++- .../src/tui/app/inline_interactive.rs | 9 + .../app/tests/commands_accounts_01/part_01.rs | 56 +++++ crates/jcode-tui/src/tui/backend.rs | 45 +++- crates/jcode-tui/src/tui/backend_tests.rs | 48 ++++ crates/jcode-tui/src/tui/session_picker.rs | 13 ++ .../src/tui/session_picker/loading.rs | 60 ++++- .../loading/mobile_inventory.rs | 123 +++++++--- .../loading/mobile_inventory_tests.rs | 218 ++++++++++++++++++ .../src/tui/session_picker/loading_tests.rs | 67 +++++- .../src/tui/session_picker/render.rs | 21 ++ .../loading_and_benchmarks.rs | 53 +++++ .../src/tui/ui_header/account_router.rs | 8 +- .../src/tui/ui_messages/todo_completion.rs | 3 +- src/cli/cloud_move/transport.rs | 10 +- 21 files changed, 791 insertions(+), 72 deletions(-) diff --git a/crates/jcode-base/src/prompt/system_prompt.md b/crates/jcode-base/src/prompt/system_prompt.md index bcf58a1a365..b1ad74eecc3 100644 --- a/crates/jcode-base/src/prompt/system_prompt.md +++ b/crates/jcode-base/src/prompt/system_prompt.md @@ -5,8 +5,7 @@ Jcode is open source: ## Autonomy and persistence -Use todo tool extensively -Define todo plans as phases containing lanes containing items, even for a single phase or lane. Phases run sequentially, lanes within a phase may run in parallel, and items within a lane run in order. Use incremental item updates by name after defining the hierarchy. Give every task a globally unique, nonblank name and put longer context in description. Rename explicitly with rename_to. Use task names for remove, add.after, lane after, and update.lane_after references, not internal IDs. +Use todo tool extensively. Have autonomy. Persist to completing a task. Fix problems over surfacing them. Accomplish user intent over literals diff --git a/crates/jcode-base/src/usage.rs b/crates/jcode-base/src/usage.rs index 6cdca9f2fd5..a6444444aa3 100644 --- a/crates/jcode-base/src/usage.rs +++ b/crates/jcode-base/src/usage.rs @@ -47,7 +47,10 @@ pub use remote_accounts::{ clear_remote_account_usage, remote_account_banked_resets, remote_account_list, remote_account_usage_received, set_remote_account_usage, }; -pub use startup_snapshot::{AccountUsageStartupPublisher, hydrate_remote_account_usage}; +pub use startup_snapshot::{ + AccountUsageStartupPublisher, hydrate_remote_account_usage, + hydrate_remote_account_usage_for_generation, +}; use anyhow::{Context, Result}; pub use display::{format_reset_time, format_usage_bar}; diff --git a/crates/jcode-base/src/usage/remote_accounts.rs b/crates/jcode-base/src/usage/remote_accounts.rs index 6f30ba399a1..3540d367bdb 100644 --- a/crates/jcode-base/src/usage/remote_accounts.rs +++ b/crates/jcode-base/src/usage/remote_accounts.rs @@ -5,6 +5,7 @@ use std::time::Instant; #[derive(Default)] struct RemoteAccounts { + generation: u64, received: Option, anthropic: Vec, openai: Vec, @@ -26,12 +27,28 @@ fn try_remote_lock( } /// Enter daemon-only mode without claiming that a response has arrived. -pub fn clear_remote_account_usage() { +pub fn clear_remote_account_usage() -> u64 { if let Ok(mut state) = REMOTE.get_or_init(Mutex::default).lock() { - *state = RemoteAccounts::default(); + let generation = state.generation.wrapping_add(1); + *state = RemoteAccounts { + generation, + ..Default::default() + }; + generation + } else { + 0 } } +/// Capture the current connection generation before reading advisory disk data. +pub(super) fn remote_account_usage_generation() -> u64 { + REMOTE + .get_or_init(Mutex::default) + .lock() + .map(|state| state.generation) + .unwrap_or(0) +} + /// An empty authoritative response is received; an initial reset is not. pub fn remote_account_usage_received() -> bool { REMOTE @@ -57,6 +74,7 @@ pub(super) fn set_remote_account_usage_with_age( ) { if let Ok(mut state) = REMOTE.get_or_init(Mutex::default).lock() { *state = RemoteAccounts { + generation: state.generation.wrapping_add(1), received: Instant::now().checked_sub(age), anthropic, openai, @@ -64,6 +82,36 @@ pub(super) fn set_remote_account_usage_with_age( } } +pub(super) fn import_remote_account_usage( + generation: u64, + anthropic: Vec, + openai: Vec, + age: std::time::Duration, +) -> bool { + let Ok(mut state) = REMOTE.get_or_init(Mutex::default).lock() else { + return false; + }; + state.import_snapshot(generation, anthropic, openai, age) +} + +impl RemoteAccounts { + fn import_snapshot( + &mut self, + generation: u64, + anthropic: Vec, + openai: Vec, + age: std::time::Duration, + ) -> bool { + if self.generation != generation || self.received.is_some() { + return false; + } + self.received = Instant::now().checked_sub(age); + self.anthropic = anthropic; + self.openai = openai; + true + } +} + /// Daemon-reported OAuth accounts `(anthropic, openai)`, once a response arrived. pub fn remote_account_list() -> Option<(Vec, Vec)> { let state = try_remote_lock(REMOTE.get()?)?; @@ -155,6 +203,26 @@ pub fn remote_account_banked_resets( mod tests { use super::*; + #[test] + fn delayed_snapshot_cannot_overwrite_live_or_reconnected_usage() { + let mut state = RemoteAccounts { + generation: 7, + ..Default::default() + }; + let age = std::time::Duration::from_secs(20); + assert!(!state.import_snapshot(6, Vec::new(), Vec::new(), age)); + assert!(state.received.is_none()); + assert!(state.import_snapshot(7, Vec::new(), Vec::new(), age)); + assert!(state.received.unwrap().elapsed() >= age); + // Even an empty live response is authoritative over a startup hint. + state.received = Some(Instant::now()); + assert!(!state.import_snapshot(7, Vec::new(), Vec::new(), age)); + state.generation = 8; + state.received = None; + assert!(!state.import_snapshot(7, Vec::new(), Vec::new(), age)); + assert!(state.import_snapshot(8, Vec::new(), Vec::new(), age)); + } + #[test] fn connection_reset_is_not_an_empty_authoritative_response() { clear_remote_account_usage(); diff --git a/crates/jcode-base/src/usage/startup_snapshot.rs b/crates/jcode-base/src/usage/startup_snapshot.rs index 663ac028694..7bd0d7ee5b7 100644 --- a/crates/jcode-base/src/usage/startup_snapshot.rs +++ b/crates/jcode-base/src/usage/startup_snapshot.rs @@ -202,18 +202,25 @@ fn snapshot_age( /// stays unknown. Callers must skip this entirely for SSH-forwarded sockets. /// Returns true only when an actual (possibly empty) snapshot was imported. pub fn hydrate_remote_account_usage(socket: &Path) -> bool { + let generation = super::remote_accounts::remote_account_usage_generation(); + hydrate_remote_account_usage_for_generation(socket, generation) +} + +/// Read a startup hint off the async connection path. A live daemon reply or +/// another connection reset invalidates the generation while disk I/O runs. +pub fn hydrate_remote_account_usage_for_generation(socket: &Path, generation: u64) -> bool { let Some(socket) = normalized_socket(socket) else { return false; }; let Some((snapshot, age)) = read_snapshot(&socket, &cache_path(&socket)) else { return false; }; - super::remote_accounts::set_remote_account_usage_with_age( + super::remote_accounts::import_remote_account_usage( + generation, snapshot.anthropic, snapshot.openai, age, - ); - true + ) } fn read_snapshot(socket: &Path, path: &Path) -> Option<(Snapshot, Duration)> { diff --git a/crates/jcode-provider-anthropic/src/lib.rs b/crates/jcode-provider-anthropic/src/lib.rs index 7c4fb9efcb1..c54708a8fed 100644 --- a/crates/jcode-provider-anthropic/src/lib.rs +++ b/crates/jcode-provider-anthropic/src/lib.rs @@ -1195,7 +1195,10 @@ mod cache_prefix_invariant_tests { assert_eq!(names.iter().filter(|n| **n == "bash").count(), 1); assert_eq!(names.iter().filter(|n| **n == "read").count(), 1); for legacy in ["Bash", "Read", "Agent"] { - assert!(!names.contains(&legacy), "legacy Claude Code name {legacy} sent"); + assert!( + !names.contains(&legacy), + "legacy Claude Code name {legacy} sent" + ); } } diff --git a/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs b/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs index 46cc0c7238b..7d1ffbf8c78 100644 --- a/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs +++ b/crates/jcode-provider-anthropic/src/oauth_tool_schema_tests.rs @@ -99,10 +99,7 @@ fn oauth_schedule_wakeup_forwards_the_real_schedule_schema() { "fabricated schema leaked back in: {props:?}" ); assert_eq!( - formatted - .iter() - .filter(|t| t.name == "schedule") - .count(), + formatted.iter().filter(|t| t.name == "schedule").count(), 1, "schedule must not be advertised twice" ); diff --git a/crates/jcode-sdk/src/cloud.rs b/crates/jcode-sdk/src/cloud.rs index 87bb65c490d..f4f5cd04e52 100644 --- a/crates/jcode-sdk/src/cloud.rs +++ b/crates/jcode-sdk/src/cloud.rs @@ -412,12 +412,21 @@ impl CloudClient { /// Reset the account's unread badge (the app came to the foreground). pub fn reset_push_badge(&self, account: &Account) -> Result<()> { - self.account_call(account, "POST", "/push/badge", serde_json::json!({"count": 0})) + self.account_call( + account, + "POST", + "/push/badge", + serde_json::json!({"count": 0}), + ) } /// Ask the control plane to push `notification` to every device on the /// account. Servers call this with their own account key. - pub fn send_push(&self, account: &Account, notification: &PushNotification) -> Result { + pub fn send_push( + &self, + account: &Account, + notification: &PushNotification, + ) -> Result { let client = Self { api_base: account.api_base.trim_end_matches('/').to_string(), ..self.clone() @@ -766,7 +775,10 @@ mod tests { (200, r#"{"ok":true}"#), (200, r#"{"sent":1,"devices":1}"#), (200, r#"{"ok":true}"#), - (401, r#"{"error":{"code":"unauthorized","message":"revoked"}}"#), + ( + 401, + r#"{"error":{"code":"unauthorized","message":"revoked"}}"#, + ), ]); let cloud = CloudClient::new("t"); let account = Account { @@ -798,7 +810,13 @@ mod tests { }, ) .unwrap(); - assert_eq!(receipt, PushReceipt { sent: 1, devices: 1 }); + assert_eq!( + receipt, + PushReceipt { + sent: 1, + devices: 1 + } + ); assert!(seen.recv().unwrap().contains(r#""kind":"turn_done""#)); cloud.unregister_push_device(&account, "ab01").unwrap(); diff --git a/crates/jcode-tui/src/tui/app/inline_interactive.rs b/crates/jcode-tui/src/tui/app/inline_interactive.rs index cf9a058a153..e3e5f4d7d82 100644 --- a/crates/jcode-tui/src/tui/app/inline_interactive.rs +++ b/crates/jcode-tui/src/tui/app/inline_interactive.rs @@ -2772,6 +2772,15 @@ impl App { ); match recv_result { + Ok(session_picker::SessionListUpdate::CloudUnavailable(warning)) => { + if picker_active { + if let Some(picker) = self.session_picker_overlay.as_ref() { + picker.borrow_mut().set_cloud_warning(warning); + return true; + } + } + false + } Ok(session_picker::SessionListUpdate::Cached((server_groups, orphan_sessions))) => { if picker_active { let changed = self.apply_loaded_session_picker(server_groups, orphan_sessions); diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs index a3fbf26a42b..46c9b56a994 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs @@ -15,6 +15,62 @@ fn slash_resume_opens_session_picker_overlay_locally() { assert!(app.input.is_empty()); } +#[test] +fn session_picker_cloud_warning_keeps_refresh_pending_and_survives_fresh() { + use crate::tui::session_picker::{SessionListUpdate, SessionPicker}; + let mut app = create_test_app(); + app.session_picker_mode = SessionPickerMode::Resume; + app.session_picker_overlay = Some(RefCell::new(SessionPicker::loading())); + let (sender, receiver) = std::sync::mpsc::channel(); + app.pending_session_picker_load = Some(PendingSessionPickerLoad { receiver }); + let warning = "Cloud sessions could not be refreshed. Reopen /resume to retry."; + sender + .send(SessionListUpdate::CloudUnavailable(warning.into())) + .unwrap(); + assert!(app.poll_session_picker_load()); + assert!(app.pending_session_picker_load.is_some()); + assert_eq!( + app.session_picker_overlay + .as_ref() + .unwrap() + .borrow() + .cloud_warning + .as_deref(), + Some(warning) + ); + sender + .send(SessionListUpdate::Fresh(Ok((vec![], vec![])))) + .unwrap(); + assert!(app.poll_session_picker_load()); + assert!(app.pending_session_picker_load.is_none()); + assert_eq!( + app.session_picker_overlay + .as_ref() + .unwrap() + .borrow() + .cloud_warning + .as_deref(), + Some(warning) + ); +} + +#[test] +fn session_picker_cloud_warning_does_not_reopen_closed_overlay() { + use crate::tui::session_picker::SessionListUpdate; + let mut app = create_test_app(); + app.session_picker_mode = SessionPickerMode::Resume; + let (sender, receiver) = std::sync::mpsc::channel(); + app.pending_session_picker_load = Some(PendingSessionPickerLoad { receiver }); + sender + .send(SessionListUpdate::CloudUnavailable( + "Reopen /resume to retry.".into(), + )) + .unwrap(); + assert!(!app.poll_session_picker_load()); + assert!(app.session_picker_overlay.is_none()); + assert!(app.pending_session_picker_load.is_some()); +} + #[test] fn session_picker_cached_update_keeps_fresh_refresh_pending() { use crate::tui::session_picker::{SessionListUpdate, SessionPicker}; diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index d8b2bc64777..f33e4fb1419 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -384,16 +384,25 @@ impl RemoteConnection { ) -> Result { let connect_start = Instant::now(); let socket_connect_start = Instant::now(); - let stream = Stream::connect(server::socket_path()).await?; + let socket_path = server::socket_path(); + let stream = Stream::connect(&socket_path).await?; let socket_connect_ms = socket_connect_start.elapsed().as_millis(); let (reader, writer) = stream.into_split(); + let quota_setup_start = Instant::now(); // Quotas for headed clients belong to this daemon connection. Clear // previous/local snapshots before any startup account rows are drawn. - crate::usage::clear_remote_account_usage(); + let usage_generation = crate::usage::clear_remote_account_usage(); if !super::is_ssh_remote() { - crate::usage::hydrate_remote_account_usage(&server::socket_path()); + Self::spawn_usage_hydration(move || { + crate::usage::hydrate_remote_account_usage_for_generation( + &socket_path, + usage_generation, + ) + }); } + let quota_setup_ms = quota_setup_start.elapsed().as_millis(); + let connection_setup_start = Instant::now(); let mut conn = Self { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), @@ -418,12 +427,15 @@ impl RemoteConnection { call_output_tokens_seen: 0, }; + let connection_setup_ms = connection_setup_start.elapsed().as_millis(); + let ssh_ready_start = Instant::now(); // The local SSH adapter accepts before OpenSSH authenticates. A Unix // connect/write alone must not reset reconnect backoff or show success. if super::is_ssh_remote() { conn.verify_ssh_ready(Duration::from_secs(35)).await?; } + let ssh_ready_ms = ssh_ready_start.elapsed().as_millis(); // Subscribe to events let subscribe_start = Instant::now(); let (working_dir, selfdev) = super::subscribe_metadata(remote_working_dir); @@ -491,8 +503,11 @@ impl RemoteConnection { let bootstrap_request_ms = bootstrap_request_start.elapsed().as_millis(); crate::logging::info(&format!( - "[TIMING] remote connect: socket={}ms, subscribe={}ms, bootstrap_request={}ms, total={}ms, resumed={}, request={}", + "[TIMING] remote connect: socket={}ms, quota_setup={}ms, connection_setup={}ms, ssh_ready={}ms, subscribe={}ms, bootstrap_request={}ms, total={}ms, resumed={}, request={}", socket_connect_ms, + quota_setup_ms, + connection_setup_ms, + ssh_ready_ms, subscribe_ms, bootstrap_request_ms, connect_start.elapsed().as_millis(), @@ -503,6 +518,28 @@ impl RemoteConnection { Ok(conn) } + /// Advisory disk snapshots must not delay Subscribe/History or block the + /// async runtime. The generation check in the importer gives live replies + /// and subsequent connections precedence over a late disk read. + fn spawn_usage_hydration( + hydrate: impl FnOnce() -> bool + Send + 'static, + ) -> tokio::task::JoinHandle<()> { + let queued = Instant::now(); + tokio::task::spawn_blocking(move || { + let started = Instant::now(); + let imported = hydrate(); + crate::logging::info(&format!( + "[TIMING] remote quota hydration: queue={}ms, read={}ms, imported={}", + queued + .elapsed() + .saturating_sub(started.elapsed()) + .as_millis(), + started.elapsed().as_millis(), + imported, + )); + }) + } + fn interrupt_request_log_fields( &self, request: &Request, diff --git a/crates/jcode-tui/src/tui/backend_tests.rs b/crates/jcode-tui/src/tui/backend_tests.rs index cbdb9199bf0..62ffb78c431 100644 --- a/crates/jcode-tui/src/tui/backend_tests.rs +++ b/crates/jcode-tui/src/tui/backend_tests.rs @@ -1,6 +1,54 @@ use super::*; use std::time::Duration; +#[tokio::test] +async fn slow_usage_hydration_does_not_block_subscribe_or_async_runtime() { + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let hydration = RemoteConnection::spawn_usage_hydration(move || { + started_tx.send(()).unwrap(); + release_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + false + }); + started_rx.await.unwrap(); + let remote = RemoteConnection::dummy(); + let peer = remote._dummy_peer.as_ref().unwrap(); + let start = Instant::now(); + let result = tokio::time::timeout(Duration::from_millis(250), async { + remote + .send_request(Request::GetHistory { id: 9 }) + .await + .unwrap(); + let mut bytes = [0; 256]; + peer.readable().await.unwrap(); + let count = peer.try_read(&mut bytes).unwrap(); + assert!(matches!( + serde_json::from_slice::(&bytes[..count]).unwrap(), + Request::GetHistory { id: 9 } + )); + }) + .await; + let elapsed = start.elapsed(); + release_tx.send(()).unwrap(); + hydration.await.unwrap(); + result.expect("quota disk reads must not delay bootstrap requests"); + eprintln!( + "bootstrap while snapshot read blocked: {}us", + elapsed.as_micros() + ); +} + +#[test] +fn measure_local_quota_hydration_cost() { + let socket = server::socket_path(); + let started = Instant::now(); + let imported = crate::usage::hydrate_remote_account_usage(&socket); + eprintln!( + "local quota hydration: {}us, imported={imported}", + started.elapsed().as_micros() + ); +} + #[tokio::test] async fn ssh_readiness_waits_for_daemon_and_preserves_buffered_events() { let mut remote = RemoteConnection::dummy(); diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index f39436f706c..32e7a60cf84 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -79,6 +79,7 @@ type GroupedSessionList = (Vec, Vec); pub(crate) enum SessionListUpdate { Cached(GroupedSessionList), + CloudUnavailable(String), Fresh(Result), } @@ -311,6 +312,7 @@ pub struct SessionPicker { pending_search_submission: Option, /// Lightweight placeholder shown while the picker list is loading. loading_message: Option, + pub(crate) cloud_warning: Option, resume_latency: latency::Latency, latency_transcript_rendered: bool, pending_preview_load: Option, @@ -402,6 +404,7 @@ impl SessionPicker { search_selection: Default::default(), pending_search_submission: None, loading_message: None, + cloud_warning: None, resume_latency: Default::default(), latency_transcript_rendered: false, pending_preview_load: None, @@ -461,6 +464,7 @@ impl SessionPicker { search_selection: Default::default(), pending_search_submission: None, loading_message: Some("Loading sessions…".to_string()), + cloud_warning: None, resume_latency: Default::default(), latency_transcript_rendered: false, pending_preview_load: None, @@ -552,6 +556,7 @@ impl SessionPicker { search_selection: Default::default(), pending_search_submission: None, loading_message: None, + cloud_warning: None, resume_latency: Default::default(), latency_transcript_rendered: false, pending_preview_load: None, @@ -2399,9 +2404,17 @@ impl SessionPicker { self.run_with_session_updates(None) } + pub(crate) fn set_cloud_warning(&mut self, warning: String) { + self.cloud_warning = Some(warning); + } + fn apply_session_list_update(&mut self, update: SessionListUpdate) -> Result { let (sessions, finished) = match update { SessionListUpdate::Cached(sessions) => (sessions, false), + SessionListUpdate::CloudUnavailable(warning) => { + self.set_cloud_warning(warning); + return Ok(false); + } SessionListUpdate::Fresh(sessions) => (sessions?, true), }; self.reseed_grouped(sessions.0, sessions.1); diff --git a/crates/jcode-tui/src/tui/session_picker/loading.rs b/crates/jcode-tui/src/tui/session_picker/loading.rs index e96c2e072a3..2b565bed1e0 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading.rs @@ -307,6 +307,18 @@ fn load_recently_closed_sessions() -> Vec { /// Shared staged loader for the standalone and in-session pickers. A just-closed /// session is actionable before loading either the cache or the historical list. pub(crate) fn send_session_list_updates(sender: std::sync::mpsc::Sender) { + send_session_list_updates_with_discovery( + sender, + mobile_inventory::available(), + mobile_inventory::refresh, + ); +} + +fn send_session_list_updates_with_discovery( + sender: std::sync::mpsc::Sender, + discovery_available: bool, + refresh: impl FnOnce() -> Result, +) { let recent = load_recently_closed_sessions(); if !recent.is_empty() && sender @@ -347,10 +359,10 @@ pub(crate) fn send_session_list_updates(sender: std::sync::mpsc::Sender { invalidate_session_list_cache(); - load_sessions_grouped() + load_sessions_grouped().map(|(groups, mut orphans)| { + let loaded: HashSet<_> = orphans + .iter() + .map(|s| s.id.as_str()) + .chain( + groups + .iter() + .flat_map(|g| g.sessions.iter().map(|s| s.id.as_str())), + ) + .collect(); + let recovered: Vec<_> = recent + .into_iter() + .filter(|s| !loaded.contains(s.id.as_str())) + .collect(); + orphans.extend(recovered); + (groups, orphans) + }) } Ok(false) => Ok(local), Err(error) => { crate::logging::debug(&format!("Mobile resume discovery unavailable: {error:#}")); + let _ = sender.send(super::SessionListUpdate::CloudUnavailable( + "Cloud sessions could not be refreshed. Reopen /resume to retry.".into(), + )); Ok(local) } }; @@ -2793,6 +2824,27 @@ fn include_cloud_sessions( sessions.push(session); } if let Some(session) = sessions.iter_mut().find(|session| session.id == id) { + // A migrated local snapshot is historical. Verified cloud metadata + // must win for display, project scoping, sorting, and search. + if let Some(title) = mobile_inventory::title(id) { + session.title = title; + session.working_dir = lease.repo_root.clone(); + if let Ok(updated_at) = chrono::DateTime::parse_from_rfc3339(&lease.updated_at) { + let updated_at = updated_at.with_timezone(&chrono::Utc); + session.last_message_time = updated_at; + session.last_active_at = Some(updated_at); + session.needs_catchup = + catchup_seen.needs_catchup(id, updated_at, &session.status); + } + session.search_index = build_search_index_from_summary( + id, + &session.short_name, + &session.title, + session.working_dir.as_deref(), + session.save_label.as_deref(), + &session.search_index, + ); + } session.server_name = Some(format!("cloud: {host}")); session.server_icon = Some("☁".to_string()); session diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs index 4fb0c6a53d0..56f77024dae 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs @@ -18,7 +18,7 @@ for sid in sys.argv[1].splitlines(): lease = json.loads(lease_path.read_text()) if lease_path.exists() else {} if lease.get('away_host'): continue - result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir')}) + result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir'), 'title': session.get('custom_title') if session.get('custom_title') is not None else session.get('title'), 'updated_at': session.get('updated_at')}) except (OSError, ValueError, TypeError): continue print(json.dumps({'host': socket.gethostname(), 'sessions': result})) @@ -36,13 +36,15 @@ struct VerifiedSession { session_id: String, epoch: u64, cwd: Option, + title: Option, + updated_at: Option, } /// Called only after publishing local results, never on the render thread. pub(super) fn available() -> bool { crate::mobile_sync::session_source() != "cloud" - && crate::subscription_catalog::has_credentials() - && storage::jcode_dir().is_ok_and(|home| home.join("cloud/ssh_config").is_file()) + && crate::subscription_catalog::configured_api_key() + .is_some_and(|key| !key.trim().is_empty()) } pub(super) fn refresh() -> Result { @@ -51,9 +53,6 @@ pub(super) fn refresh() -> Result { } let home = storage::jcode_dir()?; let config = home.join("cloud/ssh_config"); - if !config.is_file() { - return Ok(false); - } let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() .build()?; @@ -61,17 +60,12 @@ pub(super) fn refresh() -> Result { let inventory = crate::mobile_sync::discover_cloud_sessions().await?; let candidates: Vec<_> = inventory .into_iter() - .filter(|s| { - storage::read_session_lease(&s.session_id).is_none() - && !home - .join("sessions") - .join(format!("{}.json", s.session_id)) - .exists() - }) + .filter(|s| should_verify(&home, &s.session_id)) .collect(); if candidates.is_empty() { return Ok(false); } + prepare_ssh_config(&config).await?; let ids = candidates .iter() .map(|s| s.session_id.as_str()) @@ -93,6 +87,54 @@ pub(super) fn refresh() -> Result { }) } +fn valid_id(id: &str) -> bool { + !id.is_empty() + && id.len() <= 128 + && id + .bytes() + .all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c)) +} + +fn should_verify(home: &Path, id: &str) -> bool { + if !valid_id(id) { + return false; + } + match storage::read_session_lease(id) { + Some(lease) => lease.session_id == id && lease.away_host.as_deref() == Some("jcode-cloud"), + None => { + !home + .join("session_leases") + .join(format!("{id}.json")) + .exists() + && !home.join("sessions").join(format!("{id}.json")).exists() + } + } +} + +async fn prepare_ssh_config(config: &Path) -> Result<()> { + let mut command = tokio::process::Command::new(std::env::current_exe()?); + command + .args([ + "--no-update", + "--no-selfdev", + "cloud", + "machine", + "prepare-ssh", + ]) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::null()) + .kill_on_drop(true); + let (status, _) = crate::tui::cloud_git::bounded_output(command, Duration::from_secs(8), 4096) + .await + .ok_or_else(|| anyhow::anyhow!("cloud SSH setup unavailable"))?; + anyhow::ensure!( + status.success() && config.is_file(), + "cloud SSH setup failed" + ); + Ok(()) +} + fn register( home: &Path, candidates: Vec, @@ -105,36 +147,53 @@ fn register( }) else { continue; }; - // Recheck after network IO. Never replace a migration decision or local work. - if storage::read_session_lease(&remote.session_id).is_some() - || home - .join("sessions") - .join(format!("{}.json", remote.session_id)) - .exists() + if !valid_id(&remote.session_id) || !should_verify(home, &remote.session_id) { + continue; + } + // Never let metadata discovery supersede a migration. Only update an + // existing managed-cloud lease at the exact epoch we verified remotely. + let existing = storage::read_session_lease(&remote.session_id); + if existing + .as_ref() + .is_some_and(|lease| lease.epoch != remote.epoch) { continue; } - let lease = storage::SessionLease { - session_id: remote.session_id, + let mut lease = existing.clone().unwrap_or_else(|| storage::SessionLease { + session_id: remote.session_id.clone(), epoch: remote.epoch, away_host: Some("jcode-cloud".into()), - repo_root: remote.cwd.or_else(|| metadata.cwd.clone()), + repo_root: None, last_sync_snapshot: None, last_sync_head: None, - updated_at: metadata - .updated_at - .clone() - .unwrap_or_else(|| chrono::Utc::now().to_rfc3339()), - }; - storage::write_session_lease(&lease)?; - // A picker-only sidecar preserves the title, not a canonical transcript. + updated_at: String::new(), + }); + lease.repo_root = remote.cwd; + if let Some(updated_at) = remote.updated_at.or_else(|| metadata.updated_at.clone()) { + // Ignore malformed timestamps rather than poisoning picker sorting. + if chrono::DateTime::parse_from_rfc3339(&updated_at).is_ok() { + lease.updated_at = updated_at; + } + } + if lease.updated_at.is_empty() { + lease.updated_at = chrono::Utc::now().to_rfc3339(); + } + if existing.as_ref() != Some(&lease) { + storage::write_session_lease(&lease)?; + changed = true; + } + // A picker-only sidecar preserves metadata, never a canonical transcript. let path = home .join("cloud/mobile-session-titles") .join(format!("{}.json", lease.session_id)); - if let Some(title) = metadata.title.as_deref() { - storage::write_json(&path, &title)?; + let title = remote.title.unwrap_or_else(|| { + let name = extract_session_name(&lease.session_id).unwrap_or(&lease.session_id); + format!("{name} (cloud session)") + }); + if storage::read_json::(&path).ok().as_deref() != Some(&title) { + storage::write_json_secret(&path, &title)?; + changed = true; } - changed = true; } Ok(changed) } diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs index b82c23293cd..71cbbad78f0 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs @@ -32,6 +32,56 @@ fn metadata(id: &str) -> crate::mobile_sync::SyncedSession { } } +#[test] +fn signed_in_fresh_client_can_discover_without_existing_ssh_config() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _home = HomeGuard::install(home.path()); + struct Environment(Vec<(&'static str, Option)>); + impl Drop for Environment { + fn drop(&mut self) { + for (name, value) in self.0.drain(..) { + match value { + Some(value) => crate::env::set_var(name, value), + None => crate::env::remove_var(name), + } + } + } + } + let names = [ + "JCODE_API_KEY", + "JCODE_CLOUD_ENV_SESSION", + "JCODE_CLOUD_MACHINE", + "JCODE_SERVER_NAME", + ]; + let _environment = Environment( + names + .iter() + .map(|&name| (name, std::env::var_os(name))) + .collect(), + ); + for name in names { + crate::env::remove_var(name); + } + crate::env::set_var("JCODE_API_KEY", "test-account-key"); + assert!(!home.path().join("cloud/ssh_config").exists()); + assert!( + available(), + "sign-in, not preexisting SSH files, enables discovery" + ); + crate::env::set_var("JCODE_API_KEY", " "); + assert!( + !available(), + "blank credentials must not trigger network IO" + ); + crate::env::set_var("JCODE_API_KEY", "test-account-key"); + crate::env::set_var("JCODE_CLOUD_MACHINE", "1"); + assert!( + !available(), + "cloud hosts must not recursively discover themselves" + ); +} + fn inventory(ids: &[(&str, u64)]) -> VerifiedInventory { VerifiedInventory { host: "verified-worker".into(), @@ -41,6 +91,8 @@ fn inventory(ids: &[(&str, u64)]) -> VerifiedInventory { session_id: (*id).into(), epoch: *epoch, cwd: Some("/verified/repository".into()), + title: Some("Future orbital compiler work".into()), + updated_at: Some("2026-11-08T05:00:00Z".into()), }) .collect(), } @@ -178,3 +230,169 @@ fn registration_skips_mismatched_hosts_and_unverified_missing_sessions() { ); } } + +#[test] +fn known_cloud_metadata_refreshes_without_changing_migration_or_snapshot() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_metadata_refresh"; + assert!(register(home.path(), vec![metadata(id)], inventory(&[(id, 7)])).unwrap()); + let mut lease = storage::read_session_lease(id).unwrap(); + lease.last_sync_snapshot = Some("preserved-snapshot".into()); + lease.last_sync_head = Some("preserved-head".into()); + storage::write_session_lease(&lease).unwrap(); + let mut session = Session::create_with_id(id.into(), None, Some("Old local title".into())); + session.working_dir = Some("/old/local/project".into()); + session.add_message( + Role::User, + vec![crate::message::ContentBlock::Text { + text: "Historical local conversation".into(), + cache_control: None, + }], + ); + let path = home.path().join("sessions").join(format!("{id}.json")); + // Model the historical local copy left by migration, without attempting to + // save an active local session against its deliberately-away lease. + storage::write_json(&path, &session).unwrap(); + let original = std::fs::read(&path).unwrap(); + assert!( + should_verify(home.path(), id), + "known cloud snapshots must refresh too" + ); + let mut refreshed = inventory(&[(id, 7)]); + refreshed.sessions[0].title = Some("Updated lunar compiler".into()); + refreshed.sessions[0].cwd = Some("/cloud/new-project".into()); + refreshed.sessions[0].updated_at = Some("2026-11-09T01:00:00Z".into()); + assert!(register(home.path(), vec![metadata(id)], refreshed).unwrap()); + let updated = storage::read_session_lease(id).unwrap(); + assert_eq!(updated.epoch, 7); + assert_eq!(updated.last_sync_snapshot, lease.last_sync_snapshot); + assert_eq!(updated.last_sync_head, lease.last_sync_head); + assert_eq!(updated.repo_root.as_deref(), Some("/cloud/new-project")); + assert_eq!(std::fs::read(&path).unwrap(), original); + let mut sessions = Vec::new(); + super::super::include_cloud_sessions( + &home.path().join("sessions"), + &crate::catchup::CatchupSeenSnapshot::load(), + &mut sessions, + ); + assert_eq!(sessions.len(), 1); + assert_eq!(sessions[0].title, "Updated lunar compiler"); + assert_eq!( + sessions[0].working_dir.as_deref(), + Some("/cloud/new-project") + ); + assert!(sessions[0].search_index.contains("lunar")); + assert!(sessions[0].search_index.contains("/cloud/new-project")); + assert_eq!( + sessions[0].last_message_time.to_rfc3339(), + "2026-11-09T01:00:00+00:00" + ); + let mut identical = inventory(&[(id, 7)]); + identical.sessions[0].title = Some("Updated lunar compiler".into()); + identical.sessions[0].cwd = Some("/cloud/new-project".into()); + identical.sessions[0].updated_at = Some("2026-11-09T01:00:00Z".into()); + assert!(!register(home.path(), vec![metadata(id)], identical).unwrap()); +} + +#[test] +fn cleared_remote_title_and_directory_do_not_resurrect_stale_api_metadata() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_cleared_metadata"; + register(home.path(), vec![metadata(id)], inventory(&[(id, 3)])).unwrap(); + let mut cleared = inventory(&[(id, 3)]); + cleared.sessions[0].title = None; + cleared.sessions[0].cwd = None; + assert!(register(home.path(), vec![metadata(id)], cleared).unwrap()); + assert!(storage::read_session_lease(id).unwrap().repo_root.is_none()); + assert_ne!(title(id).as_deref(), Some("Future orbital compiler work")); + assert!(title(id).unwrap().contains("cloud session")); +} + +#[test] +fn returned_or_other_host_sessions_and_unsafe_ids_are_not_refresh_candidates() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_candidate_ownership"; + register(home.path(), vec![metadata(id)], inventory(&[(id, 9)])).unwrap(); + let mut lease = storage::read_session_lease(id).unwrap(); + for host in [None, Some("other-machine".to_string())] { + lease.away_host = host; + storage::write_session_lease(&lease).unwrap(); + assert!(!should_verify(home.path(), id)); + assert!(!register(home.path(), vec![metadata(id)], inventory(&[(id, 9)])).unwrap()); + assert_eq!(storage::read_session_lease(id), Some(lease.clone())); + } + for id in ["../escape", ".", "session:route", "", "session\nsecond"] { + assert!(!should_verify(home.path(), id)); + assert!(!register(home.path(), vec![metadata(id)], inventory(&[(id, 0)])).unwrap()); + } + let id = "session_broken_ownership"; + let path = home + .path() + .join("session_leases") + .join(format!("{id}.json")); + std::fs::write(&path, b"{broken-lease").unwrap(); + assert!(!should_verify(home.path(), id)); + assert!(!register(home.path(), vec![metadata(id)], inventory(&[(id, 0)])).unwrap()); + assert_eq!(std::fs::read(path).unwrap(), b"{broken-lease"); +} + +#[cfg(unix)] +#[test] +fn verification_script_reads_authoritative_metadata_and_skips_moved_sessions() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_verification_script"; + let mut session = + Session::create_with_id(id.into(), None, Some("Verified remote title".into())); + session.working_dir = Some("/remote/current".into()); + session.migration_epoch = 6; + session.save().unwrap(); + let run = || { + let output = std::process::Command::new("sh") + .args([ + "-c", + VERIFY_SCRIPT, + "verify", + &format!("{id}\nsession_missing\n../escape"), + ]) + .env("JCODE_HOME", home.path()) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + serde_json::from_slice::(&output.stdout).unwrap() + }; + let verified = run(); + assert_eq!(verified.sessions.len(), 1); + assert_eq!(verified.sessions[0].epoch, 6); + assert_eq!( + verified.sessions[0].title.as_deref(), + Some("Verified remote title") + ); + assert_eq!(verified.sessions[0].cwd.as_deref(), Some("/remote/current")); + assert!(verified.sessions[0].updated_at.is_some()); + session.custom_title = Some("Renamed by user".into()); + session.save().unwrap(); + assert_eq!(run().sessions[0].title.as_deref(), Some("Renamed by user")); + storage::write_session_lease(&storage::SessionLease { + session_id: id.into(), + epoch: 7, + away_host: Some("returned-machine".into()), + repo_root: None, + last_sync_snapshot: None, + last_sync_head: None, + updated_at: "2026-11-08T05:00:00Z".into(), + }) + .unwrap(); + assert!(run().sessions.is_empty()); +} diff --git a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs index aecc7b30cfa..05754dc4958 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs @@ -395,6 +395,7 @@ fn grouped_refresh_bypasses_fresh_in_process_session_cache() { #[test] fn recently_closed_session_is_sent_before_cache_and_not_rolled_back() { let _env_lock = crate::storage::lock_test_env(); + let _account = EnvVarGuard::set_str("JCODE_API_KEY", ""); let temp = tempfile::tempdir().unwrap(); let _home = EnvVarGuard::set_path("JCODE_HOME", temp.path()); let _runtime = EnvVarGuard::set_path("JCODE_RUNTIME_DIR", temp.path()); @@ -430,8 +431,8 @@ fn recently_closed_session_is_sent_before_cache_and_not_rolled_back() { ); session::record_window_closed(id); let (sender, receiver) = std::sync::mpsc::channel(); - send_session_list_updates(sender); - for _ in 0..2 { + send_session_list_updates_with_discovery(sender, true, || Ok(true)); + for _ in 0..3 { let super::super::SessionListUpdate::Cached((groups, orphans)) = receiver.recv().unwrap() else { panic!("recent recovery and cache must precede the history refresh"); @@ -458,6 +459,68 @@ fn recently_closed_session_is_sent_before_cache_and_not_rolled_back() { invalidate_session_list_cache(); } +#[test] +fn failed_cloud_refresh_warns_without_discarding_local_or_last_good_cloud_rows() { + let _lock = crate::storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _home = EnvVarGuard::set_path("JCODE_HOME", home.path()); + let local_id = "session_cloud_failure_local"; + let mut session = Session::create_with_id(local_id.into(), None, Some("Local work".into())); + session.add_message( + Role::User, + vec![crate::message::ContentBlock::Text { + text: "Keep this local session usable".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + let cloud_id = "session_cloud_failure_cached"; + let lease = storage::SessionLease { + session_id: cloud_id.into(), + epoch: 5, + away_host: Some("jcode-cloud".into()), + repo_root: Some("/cloud/cached".into()), + last_sync_snapshot: None, + last_sync_head: None, + updated_at: "2026-11-08T05:00:00Z".into(), + }; + storage::write_session_lease(&lease).unwrap(); + let (sender, receiver) = std::sync::mpsc::channel(); + send_session_list_updates_with_discovery(sender, true, || { + anyhow::bail!("synthetic account failure") + }); + let mut warning_seen = false; + let mut fresh_seen = false; + for update in receiver { + match update { + super::super::SessionListUpdate::CloudUnavailable(warning) => { + assert!(warning.contains("Reopen /resume")); + warning_seen = true; + } + super::super::SessionListUpdate::Fresh(result) => { + assert!( + warning_seen, + "explain incomplete discovery before ending refresh" + ); + let (groups, orphans) = result.unwrap(); + let ids: HashSet<_> = groups + .iter() + .flat_map(|g| g.sessions.iter()) + .chain(orphans.iter()) + .map(|s| s.id.as_str()) + .collect(); + assert!(ids.contains(local_id)); + assert!(ids.contains(cloud_id)); + fresh_seen = true; + } + _ => {} + } + } + assert!(warning_seen && fresh_seen); + assert_eq!(storage::read_session_lease(cloud_id), Some(lease)); + invalidate_session_list_cache(); +} + #[test] fn load_sessions_includes_claude_code_sessions_from_external_home() { let _env_lock = crate::storage::lock_test_env(); diff --git a/crates/jcode-tui/src/tui/session_picker/render.rs b/crates/jcode-tui/src/tui/session_picker/render.rs index 64e81bfa374..6e697b580db 100644 --- a/crates/jcode-tui/src/tui/session_picker/render.rs +++ b/crates/jcode-tui/src/tui/session_picker/render.rs @@ -414,6 +414,27 @@ impl SessionPicker { virtualize: bool, ) { self.poll_transcript_search(); + // Reserve a compact warning row without substituting for local sessions. + // Keep it independent of loading state so Fresh(local) cannot erase it. + let area = if let Some(warning) = self.cloud_warning.as_deref().filter(|_| area.height > 2) + { + let line = Line::from(Span::styled( + warning.to_string(), + Style::default().fg(rgb(255, 200, 100)), + )); + let line = jcode_tui_render::truncate_line_with_ellipsis_to_width( + &line, + usize::from(area.width), + ); + frame.render_widget( + Paragraph::new(line), + Rect::new(area.x, area.y, area.width, 1), + ); + Rect::new(area.x, area.y + 1, area.width, area.height - 1) + } else { + area + }; + self.last_list_area = Some(area); // List rows never wrap. Determine exact heights without formatting the // offscreen titles, prompts, paths, timestamps, or highlighted spans. let mut item_heights: Vec = self diff --git a/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs b/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs index 0b312c421df..17e3dc48e2a 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs @@ -1,5 +1,58 @@ use super::*; +#[test] +fn cloud_warning_retains_local_sessions_and_survives_fresh_update() { + let local = make_session("local", "local session", false, SessionStatus::Closed); + let mut picker = SessionPicker::new(vec![local.clone()]); + let warning = "Cloud sessions could not be refreshed. Reopen /resume to retry."; + assert!( + !picker + .apply_session_list_update(SessionListUpdate::CloudUnavailable(warning.into())) + .unwrap() + ); + assert_eq!( + picker.selected_session().map(|s| s.id.as_str()), + Some("local") + ); + assert_eq!(picker.visible_sessions.len(), 1); + assert!( + picker + .apply_session_list_update(SessionListUpdate::Fresh(Ok((vec![], vec![local])))) + .unwrap() + ); + assert_eq!(picker.cloud_warning.as_deref(), Some(warning)); + assert_eq!( + picker.selected_session().map(|s| s.id.as_str()), + Some("local") + ); + assert!(SessionPicker::loading().cloud_warning.is_none()); + assert!(SessionPicker::new(vec![]).cloud_warning.is_none()); + assert!( + SessionPicker::new_grouped(vec![], vec![]) + .cloud_warning + .is_none() + ); + + for width in [12, 80] { + let mut terminal = + ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, 12)).unwrap(); + terminal + .draw(|frame| picker.render_session_list_impl(frame, frame.area(), false)) + .unwrap(); + assert_eq!(picker.last_list_area.unwrap().y, 1); + let buffer = terminal.backend().buffer(); + let first_row: String = (0..width).map(|x| buffer[(x, 0)].symbol()).collect(); + assert!(first_row.starts_with("Cloud")); + if width == 80 { + assert!(first_row.contains("Reopen /resume to retry.")); + let text: String = buffer.content().iter().map(|cell| cell.symbol()).collect(); + assert!(text.contains("Test session")); + } else { + assert!(first_row.ends_with('…')); + } + } +} + #[test] fn standalone_resume_renders_before_list_load_finishes() { let mut picker = SessionPicker::loading(); diff --git a/crates/jcode-tui/src/tui/ui_header/account_router.rs b/crates/jcode-tui/src/tui/ui_header/account_router.rs index 8019fca2075..4f96537e372 100644 --- a/crates/jcode-tui/src/tui/ui_header/account_router.rs +++ b/crates/jcode-tui/src/tui/ui_header/account_router.rs @@ -282,9 +282,8 @@ fn inventory_lines( Style::default().fg(dim_color()), )); } - if let Some(banked) = - header_account_banked_resets(usage_provider, &account.label) - .and_then(|banked| banked_resets_span(&banked)) + if let Some(banked) = header_account_banked_resets(usage_provider, &account.label) + .and_then(|banked| banked_resets_span(&banked)) { row.spans .push(Span::styled(" · ", Style::default().fg(dim_color()))); @@ -418,7 +417,8 @@ fn header_account_banked_resets( /// Warmer colors mean less time to spend a reset. Unknown expiry stays blue. fn banked_expiry_color(expires_at: Option<&str>, now: chrono::DateTime) -> Color { - let Some(expiry) = expires_at.and_then(|at| chrono::DateTime::parse_from_rfc3339(at).ok()) else { + let Some(expiry) = expires_at.and_then(|at| chrono::DateTime::parse_from_rfc3339(at).ok()) + else { return Color::Rgb(120, 190, 255); }; let remaining = expiry.signed_duration_since(now); diff --git a/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs b/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs index 81bd0dd4025..2ff3b10f952 100644 --- a/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs +++ b/crates/jcode-tui/src/tui/ui_messages/todo_completion.rs @@ -163,7 +163,8 @@ mod tests { for title in [serde_json::Value::Null, serde_json::json!(" \n\t ")] { msg.content = serde_json::json!({ "todo_completion_version": 1, "plan": {"title": title} - }).to_string(); + }) + .to_string(); let lines = render_with_gate_diagnostics(&msg, 80, false).unwrap(); assert_eq!(lines.len(), 1); } diff --git a/src/cli/cloud_move/transport.rs b/src/cli/cloud_move/transport.rs index 654fd04b178..d8109b0b5eb 100644 --- a/src/cli/cloud_move/transport.rs +++ b/src/cli/cloud_move/transport.rs @@ -86,10 +86,7 @@ fn output_with_ssh_retry( { return Ok(output); } - step( - "prepare", - &ssh_retry_message(host, attempt, &output.stderr), - ); + step("prepare", &ssh_retry_message(host, attempt, &output.stderr)); std::thread::sleep(std::time::Duration::from_secs(2 * attempt as u64)); attempt += 1; } @@ -399,10 +396,7 @@ impl Target { let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); // The prepare script is idempotent and serialized by its own lock. if ssh_connection_failed(&output.status) && attempt < SSH_ATTEMPTS { - step( - "prepare", - &ssh_retry_message(host, attempt, &output.stderr), - ); + step("prepare", &ssh_retry_message(host, attempt, &output.stderr)); std::thread::sleep(std::time::Duration::from_secs(2 * attempt as u64)); attempt += 1; continue; From fcae73283fd986706c7ae672b62228b1b0b53519 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:49:26 -0800 Subject: [PATCH 013/236] Resume picker: Alt+S cycles filters, Cloud follows All --- crates/jcode-tui-session-picker/src/lib.rs | 16 ++++++------ crates/jcode-tui/src/tui/session_picker.rs | 15 +++++++++++ .../jcode-tui/src/tui/session_picker_tests.rs | 26 ++++++++++++++----- .../loading_and_benchmarks.rs | 2 +- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/crates/jcode-tui-session-picker/src/lib.rs b/crates/jcode-tui-session-picker/src/lib.rs index 240877d9cba..ab0c8aa26b8 100644 --- a/crates/jcode-tui-session-picker/src/lib.rs +++ b/crates/jcode-tui-session-picker/src/lib.rs @@ -61,11 +61,11 @@ pub enum SessionFilterMode { impl SessionFilterMode { pub fn next(self) -> Self { match self { - Self::All => Self::CurrentDir, + Self::All => Self::Cloud, + Self::Cloud => Self::CurrentDir, Self::CurrentDir => Self::CatchUp, Self::CatchUp => Self::Saved, - Self::Saved => Self::Cloud, - Self::Cloud => Self::Active, + Self::Saved => Self::Active, Self::Active => Self::ClaudeCode, Self::ClaudeCode => Self::Codex, Self::Codex => Self::Pi, @@ -81,11 +81,11 @@ impl SessionFilterMode { pub fn previous(self) -> Self { match self { Self::All => Self::Cursor, - Self::CurrentDir => Self::All, + Self::Cloud => Self::All, + Self::CurrentDir => Self::Cloud, Self::CatchUp => Self::CurrentDir, Self::Saved => Self::CatchUp, - Self::Cloud => Self::Saved, - Self::Active => Self::Cloud, + Self::Active => Self::Saved, Self::ClaudeCode => Self::Active, Self::Codex => Self::ClaudeCode, Self::Pi => Self::Codex, @@ -270,13 +270,13 @@ mod tests { use super::*; #[test] - fn filter_cycle_includes_cloud_immediately_after_saved() { + fn filter_cycle_puts_cloud_immediately_after_all() { let cycle = [ SessionFilterMode::All, + SessionFilterMode::Cloud, SessionFilterMode::CurrentDir, SessionFilterMode::CatchUp, SessionFilterMode::Saved, - SessionFilterMode::Cloud, SessionFilterMode::Active, SessionFilterMode::ClaudeCode, SessionFilterMode::Codex, diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index 32e7a60cf84..3f3420688dd 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -1413,6 +1413,21 @@ impl SessionPicker { if let Some(action) = self.handle_claude_takeover_confirmation_key(code, modifiers) { return Ok(action); } + // Alt+S / Alt+Shift+S cycle filters from any mode, including while + // typing a search query where plain `s` must stay literal text. + if modifiers.contains(KeyModifiers::ALT) { + match code { + KeyCode::Char('s') if !modifiers.contains(KeyModifiers::SHIFT) => { + self.cycle_filter_mode(); + return Ok(OverlayAction::Continue); + } + KeyCode::Char('S') | KeyCode::Char('s') => { + self.cycle_filter_mode_backwards(); + return Ok(OverlayAction::Continue); + } + _ => {} + } + } // Search editing must work even before the first cached list arrives. // Keep the query in picker state so reseeding applies it to new entries. if self.search_active { diff --git a/crates/jcode-tui/src/tui/session_picker_tests.rs b/crates/jcode-tui/src/tui/session_picker_tests.rs index f0cd792778c..2b7707276c7 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests.rs @@ -129,7 +129,7 @@ fn cloud_filter_supports_flat_sessions_and_debug_visibility() { assert_eq!(picker.visible_sessions.len(), 2); assert_eq!(picker.hidden_test_count, 0); picker.cycle_filter_mode_backwards(); - assert_eq!(picker.filter_mode, SessionFilterMode::Saved); + assert_eq!(picker.filter_mode, SessionFilterMode::All); picker.cycle_filter_mode(); assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); } @@ -892,6 +892,10 @@ fn test_filter_mode_cycles_through_requested_session_sources() { assert_eq!(picker.filter_mode, SessionFilterMode::All); assert_eq!(picker.visible_sessions.len(), 6); + picker.cycle_filter_mode(); + assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); + assert!(picker.visible_sessions.is_empty()); + picker.cycle_filter_mode(); assert_eq!(picker.filter_mode, SessionFilterMode::CurrentDir); assert_eq!(picker.visible_sessions.len(), 1); @@ -916,10 +920,6 @@ fn test_filter_mode_cycles_through_requested_session_sources() { assert!(picker.visible_session_iter().all(|session| session.saved)); assert_eq!(picker.items.len(), picker.visible_sessions.len()); - picker.cycle_filter_mode(); - assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); - assert!(picker.visible_sessions.is_empty()); - picker.cycle_filter_mode(); assert_eq!(picker.filter_mode, SessionFilterMode::Active); // No live processes own these synthetic sessions, so the Active view is @@ -987,12 +987,26 @@ fn test_filter_mode_keyboard_shortcuts_cycle_both_directions() { picker .handle_overlay_key(KeyCode::Char('s'), KeyModifiers::empty()) .unwrap(); - assert_eq!(picker.filter_mode, SessionFilterMode::CurrentDir); + assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); picker .handle_overlay_key(KeyCode::Char('S'), KeyModifiers::empty()) .unwrap(); assert_eq!(picker.filter_mode, SessionFilterMode::All); + + // Alt+S cycles even while typing a search query, without inserting text. + picker + .handle_overlay_key(KeyCode::Char('/'), KeyModifiers::empty()) + .unwrap(); + picker + .handle_overlay_key(KeyCode::Char('s'), KeyModifiers::ALT) + .unwrap(); + assert_eq!(picker.filter_mode, SessionFilterMode::Cloud); + assert!(picker.search_query.is_empty()); + picker + .handle_overlay_key(KeyCode::Char('S'), KeyModifiers::ALT | KeyModifiers::SHIFT) + .unwrap(); + assert_eq!(picker.filter_mode, SessionFilterMode::All); } include!("session_picker_tests/live_presence.rs"); diff --git a/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs b/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs index 17e3dc48e2a..eab2f2b90b9 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/loading_and_benchmarks.rs @@ -264,7 +264,7 @@ fn loading_picker_filter_choice_survives_refresh() { .unwrap(); let chosen = picker.filter_mode; assert!(picker.filter_mode_user_chosen); - assert_eq!(chosen, SessionFilterMode::CurrentDir); + assert_eq!(chosen, SessionFilterMode::Cloud); picker .apply_session_list_update(SessionListUpdate::Cached(( Vec::new(), From 64c38a25a7e78793914994b1c5459c524c0353d9 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:03:48 -0800 Subject: [PATCH 014/236] Bound cloud previews remotely and stream inventory metadata --- .../src/tui/session_picker/cloud_preview.rs | 23 +- .../tui/session_picker/cloud_preview/cache.rs | 30 ++- .../cloud_preview/projection_tests.rs | 119 ++++++++++ .../session_picker/cloud_preview/remote.py | 207 ++++++++++++++++++ .../loading/mobile_inventory.rs | 26 +-- .../loading/mobile_inventory_tests.rs | 2 +- 6 files changed, 361 insertions(+), 46 deletions(-) create mode 100644 crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs create mode 100644 crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs index ac251d06d9c..565b9d906e8 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs @@ -2,21 +2,18 @@ use super::*; mod cache; +#[cfg(test)] +#[path = "cloud_preview/projection_tests.rs"] +mod projection_tests; type CloudSnapshot = (Session, Option); -const SCRIPT: &str = r#" -case "$1" in ''|*[!a-zA-Z0-9_-]*) exit 1 ;; esac -base="${JCODE_HOME:-$HOME/.jcode}/sessions/$1" -cat -- "$base.json" || exit 1 -printf '\000' -if [ -f "$base.journal.jsonl" ]; then cat -- "$base.journal.jsonl" || exit 1; fi -printf '\000' -state="${JCODE_HOME:-$HOME/.jcode}/state" -if [ -n "$JCODE_RUNTIME_DIR" ]; then state="$JCODE_RUNTIME_DIR/durable-state"; fi -telemetry="$state/session-telemetry/$1.json" -if [ -f "$telemetry" ]; then cat -- "$telemetry" || :; fi -"#; +pub(in crate::tui::session_picker) const REMOTE_PROJECTOR: &str = + include_str!("cloud_preview/remote.py"); + +pub(in crate::tui::session_picker) fn projection_script(mode: &str) -> String { + format!("python3 - {mode} \"$1\" <<'JCODE_REMOTE_PY'\n{REMOTE_PROJECTOR}\nJCODE_REMOTE_PY\n") +} pub(super) fn load(session_id: &str, host: &str) -> Option { if session_id.is_empty() @@ -116,7 +113,7 @@ fn fetch_bytes(session_id: &str, host: &str) -> Option> { Some(host), config.is_file().then_some(config.as_path()), session_id, - SCRIPT, + &projection_script("preview"), ); let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs index 0decac2b930..12091ffa693 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs @@ -7,22 +7,34 @@ use std::path::{Path, PathBuf}; use std::sync::{Mutex, OnceLock}; use std::time::Duration; -pub(super) const MAX_BYTES: usize = 64 * 1024 * 1024; -const CACHE_BUDGET: u64 = 256 * 1024 * 1024; +// Remote projection is at most 2 MiB plus bounded optional telemetry. +pub(super) const MAX_BYTES: usize = 3 * 1024 * 1024; +const CACHE_BUDGET: u64 = 24 * 1024 * 1024; const TTL: Duration = Duration::from_secs(15); pub(super) fn path(session_id: &str, host: &str, epoch: u64) -> Option { let root = super::super::preview_result(crate::storage::jcode_dir(), "cache directory")?; - Some(key_path(&root, session_id, host, epoch)) + let owner = crate::subscription_catalog::configured_api_key().unwrap_or_default(); + Some(scoped_key_path(&root, session_id, host, epoch, &owner)) } +#[cfg(test)] fn key_path(root: &Path, session_id: &str, host: &str, epoch: u64) -> PathBuf { + scoped_key_path(root, session_id, host, epoch, "test-owner") +} + +fn scoped_key_path(root: &Path, session_id: &str, host: &str, epoch: u64, owner: &str) -> PathBuf { let mut digest = Sha256::new(); - for field in [host.as_bytes(), session_id.as_bytes(), &epoch.to_be_bytes()] { + for field in [ + owner.as_bytes(), + host.as_bytes(), + session_id.as_bytes(), + &epoch.to_be_bytes(), + ] { digest.update((field.len() as u64).to_be_bytes()); digest.update(field); } - root.join("cache/cloud-previews/v1") + root.join("cache/cloud-previews/v2") .join(format!("{:x}.blob", digest.finalize())) } @@ -216,7 +228,7 @@ mod tests { fn keys_scope_host_session_epoch_and_field_boundaries() { let root = Path::new("private-root"); let baseline = key_path(root, "session", "host", 1); - assert!(baseline.starts_with(root.join("cache/cloud-previews/v1"))); + assert!(baseline.starts_with(root.join("cache/cloud-previews/v2"))); for other in [ key_path(root, "session2", "host", 1), key_path(root, "session", "host2", 1), @@ -224,11 +236,15 @@ mod tests { ] { assert_ne!(baseline, other); } + assert_ne!( + scoped_key_path(root, "session", "host", 1, "owner-one"), + scoped_key_path(root, "session", "host", 1, "owner-two") + ); assert_ne!(key_path(root, "bc", "a", 0), key_path(root, "c", "ab", 0)); let hostile = key_path(root, "../../session", "secret-host/..", 1); assert_eq!( hostile.parent(), - Some(root.join("cache/cloud-previews/v1").as_path()) + Some(root.join("cache/cloud-previews/v2").as_path()) ); assert_eq!(hostile.file_name().unwrap().to_str().unwrap().len(), 69); } diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs new file mode 100644 index 00000000000..1a8dedd2106 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs @@ -0,0 +1,119 @@ +use super::*; +use std::io::Write; + +fn run_projection(home: &std::path::Path, mode: &str, id: &str) -> std::process::Output { + let mut child = std::process::Command::new("python3") + .args(["-", mode, id]) + .env("JCODE_HOME", home) + .env_remove("JCODE_RUNTIME_DIR") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .expect("Python is required by the remote projection contract"); + child + .stdin + .take() + .unwrap() + .write_all(REMOTE_PROJECTOR.as_bytes()) + .unwrap(); + let output = child.wait_with_output().unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + output +} + +#[test] +fn oversized_remote_snapshot_is_bounded_valid_and_keeps_late_journal_updates() { + let home = tempfile::tempdir().unwrap(); + let sessions = home.path().join("sessions"); + std::fs::create_dir(&sessions).unwrap(); + let id = "oversized_preview"; + let session = Session::create_with_id(id.into(), None, Some("Old title".into())); + let mut snapshot = serde_json::to_value(&session).unwrap(); + snapshot["migration_epoch"] = serde_json::json!(19); + snapshot["messages"] = serde_json::json!([{ + "id": "huge", "role": "user", "content": [{"type": "text", "text": "x".repeat(65 * 1024 * 1024)}] + }]); + let path = sessions.join(format!("{id}.json")); + serde_json::to_writer(std::fs::File::create(&path).unwrap(), &snapshot).unwrap(); + drop(snapshot); + let before = std::fs::metadata(&path).unwrap(); + assert!(before.len() > 64 * 1024 * 1024); + let first_preview = run_projection(home.path(), "preview", id); + assert!(first_preview.stdout.len() < 2 * 1024 * 1024); + let (first, _) = parse_preview(&first_preview.stdout) + .expect("oversized individual message projection must remain valid"); + assert_eq!(first.messages.len(), 1); + assert_eq!(first.messages[0].id, "huge"); + let mut journal = std::fs::File::create(sessions.join(format!("{id}.journal.jsonl"))).unwrap(); + for i in 0..100 { + writeln!(journal, "{}", serde_json::json!({ + "meta": {"title": format!("Late journal title {i}"), "working_dir": "/latest/repository", "updated_at": "2026-11-08T05:00:00Z"}, + "append_messages": [{"id": format!("late-{i}"), "role": "user", "content": [{"type": "text", "text": format!("Late journal prompt {i}")}]}] + })).unwrap(); + } + drop(journal); + let preview = run_projection(home.path(), "preview", id); + assert!(preview.stdout.len() < 2 * 1024 * 1024); + let (loaded, _) = + parse_preview(&preview.stdout).expect("projection must remain valid Session JSON"); + assert_eq!(loaded.title.as_deref(), Some("Late journal title 99")); + assert_eq!(loaded.messages.len(), 80); + assert_eq!(loaded.messages.last().unwrap().id, "late-99"); + assert_eq!(loaded.migration_epoch, 19); + assert!( + build_messages_preview(&loaded) + .iter() + .any(|m| m.content.contains("Late journal prompt 99")) + ); + let inventory = run_projection(home.path(), "inventory", id); + let inventory: serde_json::Value = serde_json::from_slice(&inventory.stdout).unwrap(); + assert_eq!(inventory["sessions"][0]["title"], "Late journal title 99"); + assert_eq!(inventory["sessions"][0]["cwd"], "/latest/repository"); + assert_eq!(inventory["sessions"][0]["epoch"], 19); + assert_eq!( + inventory["sessions"][0]["updated_at"], + "2026-11-08T05:00:00Z" + ); + assert_eq!(std::fs::metadata(&path).unwrap().len(), before.len()); + assert_eq!( + std::fs::metadata(&path).unwrap().modified().unwrap(), + before.modified().unwrap() + ); + assert_eq!(std::fs::read_dir(&sessions).unwrap().count(), 2); +} + +#[test] +fn remote_projection_ignores_stale_sidecar_and_respects_away_lease() { + let home = tempfile::tempdir().unwrap(); + let sessions = home.path().join("sessions"); + std::fs::create_dir(&sessions).unwrap(); + let session = Session::create_with_id("authority".into(), None, Some("Canonical title".into())); + serde_json::to_writer( + std::fs::File::create(sessions.join("authority.json")).unwrap(), + &session, + ) + .unwrap(); + std::fs::write( + sessions.join("authority.meta.json"), + br#"{"title":"Stale sidecar","migration_epoch":999}"#, + ) + .unwrap(); + let result = run_projection(home.path(), "inventory", "authority"); + let result: serde_json::Value = serde_json::from_slice(&result.stdout).unwrap(); + assert_eq!(result["sessions"][0]["title"], "Canonical title"); + assert_eq!(result["sessions"][0]["epoch"], 0); + std::fs::create_dir(home.path().join("session_leases")).unwrap(); + std::fs::write( + home.path().join("session_leases/authority.json"), + br#"{"away_host":"other-owner","epoch":12}"#, + ) + .unwrap(); + let result = run_projection(home.path(), "inventory", "authority"); + let result: serde_json::Value = serde_json::from_slice(&result.stdout).unwrap(); + assert!(result["sessions"].as_array().unwrap().is_empty()); +} diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py new file mode 100644 index 00000000000..ab704cd182f --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py @@ -0,0 +1,207 @@ +# Read-only remote projection. No sidecars or canonical session writes. +import collections +import json +import mmap +import os +from pathlib import Path +import re +import socket +import sys + +STRUCT = re.compile(br'["{}\[\]]') +SPACE = b' \r\n\t' +BULKY = {'env_snapshots', 'memory_injections', 'replay_events', 'compaction', 'system_prompt'} +MAX_MESSAGES = 80 +MAX_TEXT = 8192 +MAX_OUTPUT = 2 * 1024 * 1024 +META_FIELDS = set('parent_id title custom_title updated_at provider_session_id model_usage_turn_id provider_key route_api_method model reasoning_effort context_cap subagent_model agent_model_overrides improve_mode autoreview_enabled autojudge_enabled is_canary testing_build working_dir original_working_dir short_name status last_pid last_active_at is_debug saved save_label'.split()) +INVENTORY_FIELDS = {'id', 'migration_epoch', 'working_dir', 'custom_title', 'title', 'updated_at'} + + +def string_end(data, start): + pos = start + 1 + while True: + end = data.find(b'"', pos) + if end < 0: + raise ValueError('unterminated string') + back = end - 1 + while back > start and data[back] == 92: + back -= 1 + if (end - back) % 2: + return end + 1 + pos = end + 1 + + +def skip_space(data, pos): + while pos < len(data) and data[pos] in SPACE: + pos += 1 + return pos + + +def value_end(data, pos): + if data[pos] == 34: + return string_end(data, pos) + if data[pos] not in (123, 91): + end = pos + while end < len(data) and data[end] not in b',}]\r\n \t': + end += 1 + return end + depth = 1 + pos += 1 + while depth: + match = STRUCT.search(data, pos) + if match is None: + raise ValueError('unterminated container') + pos = match.start() + char = data[pos] + if char == 34: + pos = string_end(data, pos) + continue + depth += 1 if char in (123, 91) else -1 + pos += 1 + return pos + + +def fields(data, pos=0): + pos = skip_space(data, pos) + if data[pos] != 123: + raise ValueError('expected object') + pos += 1 + while True: + pos = skip_space(data, pos) + if data[pos] == 125: + return + end = string_end(data, pos) + key = json.loads(data[pos:end]) + pos = skip_space(data, end) + if data[pos] != 58: + raise ValueError('expected colon') + pos = skip_space(data, pos + 1) + end = value_end(data, pos) + yield key, pos, end + pos = skip_space(data, end) + if data[pos] == 125: + return + if data[pos] != 44: + raise ValueError('expected comma') + pos += 1 + + +def recent_messages(data, start, end, messages): + # Locate the tail without deserializing old message bodies. The range deque + # is bounded even for snapshots with millions of messages. + ranges = collections.deque(maxlen=MAX_MESSAGES) + pos = skip_space(data, start + 1) + while pos < end and data[pos] != 93: + stop = value_end(data, pos) + ranges.append((pos, stop)) + pos = skip_space(data, stop) + if data[pos] == 44: + pos = skip_space(data, pos + 1) + for pos, stop in ranges: + # Oversized individual messages are projected without decoding their + # potentially enormous tool payloads. Recent ordinary text is retained. + if stop - pos <= 16 * 1024: + messages.append(json.loads(data[pos:stop])) + continue + message = {} + for key, begin, finish in fields(data, pos): + if key in ('id', 'role') and finish - begin < 4096: + message[key] = json.loads(data[begin:finish]) + elif key == 'content': + text = [] + block = skip_space(data, begin + 1) + while block < finish and data[block] != 93: + block_end = value_end(data, block) + for name, a, b in fields(data, block): + if name == 'text' and sum(map(len, text)) < MAX_TEXT: + # Decode only bounded strings, otherwise use a valid + # preview marker instead of slicing JSON mid-string. + text.append(json.loads(data[a:b]) if b - a <= MAX_TEXT else '[large text omitted from preview]') + block = skip_space(data, block_end) + if data[block] == 44: + block = skip_space(data, block + 1) + message['content'] = [{'type': 'text', 'text': '\n'.join(text)[:MAX_TEXT]}] + messages.append(message) + + +def project(path, preview): + session = {} + messages = collections.deque(maxlen=MAX_MESSAGES) + with path.open('rb') as source, mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) as data: + for key, start, end in fields(data): + if key == 'messages': + if preview: + recent_messages(data, start, end, messages) + elif key not in BULKY and (preview or key in INVENTORY_FIELDS) and end - start <= 256 * 1024: + session[key] = json.loads(data[start:end]) + journal = path.with_suffix('.journal.jsonl') + if journal.exists() and journal.stat().st_size: + with journal.open('rb') as source, mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) as data: + pos = 0 + while pos < len(data): + pos = skip_space(data, pos) + if pos == len(data): + break + end = value_end(data, pos) + for key, start, stop in fields(data, pos): + if key == 'meta': + meta = {} + route_present = False + for name, a, b in fields(data, start): + if name == 'route_api_method_present': + route_present = data[a:b] == b'true' + if name in META_FIELDS and (preview or name in INVENTORY_FIELDS) and b - a <= 256 * 1024: + meta[name] = json.loads(data[a:b]) + if not route_present: + meta.pop('route_api_method', None) + session.update(meta) + elif key == 'append_messages' and preview: + recent_messages(data, start, stop, messages) + pos = end + if preview: + session['messages'] = list(messages) + for key in ('env_snapshots', 'memory_injections', 'replay_events'): + session[key] = [] + return session + + +def main(): + mode, ids = sys.argv[1:3] + base = Path(os.environ.get('JCODE_HOME', str(Path.home() / '.jcode'))) + result = [] + for sid in ids.splitlines(): + if not sid or not all(c.isascii() and (c.isalnum() or c in '_-') for c in sid): + if mode == 'preview': + raise ValueError('invalid session id') + continue + try: + session = project(base / 'sessions' / (sid + '.json'), mode == 'preview') + if session.get('id') != sid: + raise ValueError('session identity mismatch') + if mode == 'preview': + encoded = json.dumps(session, separators=(',', ':'), ensure_ascii=False).encode() + while len(encoded) > MAX_OUTPUT and session['messages']: + session['messages'].pop(0) + encoded = json.dumps(session, separators=(',', ':'), ensure_ascii=False).encode() + if len(encoded) > MAX_OUTPUT: + raise ValueError('preview metadata too large') + sys.stdout.buffer.write(encoded + b'\0\0') + state = Path(os.environ['JCODE_RUNTIME_DIR']) / 'durable-state' if os.environ.get('JCODE_RUNTIME_DIR') else base / 'state' + telemetry = state / 'session-telemetry' / (sid + '.json') + if telemetry.exists() and telemetry.stat().st_size < 65536: + sys.stdout.buffer.write(telemetry.read_bytes()) + return + lease_path = base / 'session_leases' / (sid + '.json') + lease = json.loads(lease_path.read_bytes()) if lease_path.exists() else {} + if lease.get('away_host'): + continue + result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir'), 'title': session.get('custom_title') if session.get('custom_title') is not None else session.get('title'), 'updated_at': session.get('updated_at')}) + except (OSError, ValueError, TypeError, IndexError): + if mode == 'preview': + raise + print(json.dumps({'host': socket.gethostname(), 'sessions': result})) + + +if __name__ == '__main__': + main() diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs index 56f77024dae..a1451bed579 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs @@ -1,30 +1,6 @@ //! Discover mobile-created sessions without copying their transcripts locally. use super::*; -const VERIFY_SCRIPT: &str = r#" -python3 - "$1" <<'PY' -import json, os, socket, sys -from pathlib import Path -base = Path(os.environ.get('JCODE_HOME', str(Path.home() / '.jcode'))) -result = [] -for sid in sys.argv[1].splitlines(): - if not sid or not all(c.isascii() and (c.isalnum() or c in '_-') for c in sid): - continue - try: - session = json.loads((base / 'sessions' / (sid + '.json')).read_text()) - if session.get('id') != sid: - continue - lease_path = base / 'session_leases' / (sid + '.json') - lease = json.loads(lease_path.read_text()) if lease_path.exists() else {} - if lease.get('away_host'): - continue - result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir'), 'title': session.get('custom_title') if session.get('custom_title') is not None else session.get('title'), 'updated_at': session.get('updated_at')}) - except (OSError, ValueError, TypeError): - continue -print(json.dumps({'host': socket.gethostname(), 'sessions': result})) -PY -"#; - #[derive(Deserialize)] struct VerifiedInventory { host: String, @@ -75,7 +51,7 @@ pub(super) fn refresh() -> Result { Some("jcode-cloud"), Some(&config), &ids, - VERIFY_SCRIPT, + &super::super::cloud_preview::projection_script("inventory"), ); let (status, bytes) = crate::tui::cloud_git::bounded_output(command, Duration::from_secs(10), 1024 * 1024) diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs index 71cbbad78f0..a267884ad5e 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs @@ -358,7 +358,7 @@ fn verification_script_reads_authoritative_metadata_and_skips_moved_sessions() { let output = std::process::Command::new("sh") .args([ "-c", - VERIFY_SCRIPT, + &super::super::super::cloud_preview::projection_script("inventory"), "verify", &format!("{id}\nsession_missing\n../escape"), ]) From 116a18fdedc64eba9e0b542703aa33898ff9c3f3 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:04:50 -0800 Subject: [PATCH 015/236] Bound opt-in remote history bootstrap and page recent rendered windows --- .../src/protocol_tests/core_events.rs | 1 + .../src/server/client_lifecycle.rs | 2 + .../jcode-app-core/src/server/client_state.rs | 151 +++++++++- .../client_state_tests/history_delta.rs | 274 ++++++++++++++++++ crates/jcode-protocol/src/lib.rs | 21 ++ .../src/protocol_tests/core_events.rs | 35 +++ crates/jcode-protocol/src/wire.rs | 3 + 7 files changed, 474 insertions(+), 13 deletions(-) diff --git a/crates/jcode-app-core/src/protocol_tests/core_events.rs b/crates/jcode-app-core/src/protocol_tests/core_events.rs index 8561759384c..cd3b8d36a20 100644 --- a/crates/jcode-app-core/src/protocol_tests/core_events.rs +++ b/crates/jcode-app-core/src/protocol_tests/core_events.rs @@ -17,6 +17,7 @@ fn test_request_roundtrip() -> Result<()> { fn test_compacted_history_request_roundtrip() -> Result<()> { let req = Request::GetCompactedHistory { id: 7, + recent_history: false, visible_messages: 64, }; let json = serde_json::to_string(&req)?; diff --git a/crates/jcode-app-core/src/server/client_lifecycle.rs b/crates/jcode-app-core/src/server/client_lifecycle.rs index 4965d777fdf..55127193e4f 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle.rs @@ -1819,6 +1819,7 @@ pub(super) async fn handle_client( Request::GetCompactedHistory { id, visible_messages, + recent_history, } => { if handle_get_compacted_history( id, @@ -1826,6 +1827,7 @@ pub(super) async fn handle_client( &agent, &writer, visible_messages, + recent_history, ) .await .is_err() diff --git a/crates/jcode-app-core/src/server/client_state.rs b/crates/jcode-app-core/src/server/client_state.rs index 011f5d90307..d1ed3c0656e 100644 --- a/crates/jcode-app-core/src/server/client_state.rs +++ b/crates/jcode-app-core/src/server/client_state.rs @@ -490,31 +490,45 @@ pub(super) async fn handle_get_compacted_history( agent: &Arc>, writer: &Arc>, visible_messages: usize, + recent_history: bool, ) -> Result<()> { let started = Instant::now(); let (messages, images, compacted_info, source) = match agent.try_lock() { Ok(agent_guard) => { - let (messages, images, info) = agent_guard - .get_history_and_rendered_images_with_compacted_history(visible_messages); + let (messages, images, info) = if recent_history { + let (messages, images) = agent_guard.get_history_and_rendered_images(); + let (messages, images, info) = + recent_history_window(messages, images, visible_messages); + (messages, images, Some(info)) + } else { + agent_guard.get_history_and_rendered_images_with_compacted_history(visible_messages) + }; (messages, images, info, "live") } Err(_) => { let session = crate::session::Session::load_for_remote_startup(session_id) .or_else(|_| crate::session::Session::load_startup_stub(session_id))?; - let (rendered_messages, images, info) = + let (rendered_messages, images, info) = if recent_history { + let (messages, images) = crate::session::render_messages_and_images(&session); + (messages, images, None) + } else { crate::session::render_messages_and_images_with_compacted_history( &session, visible_messages, - ); - ( - rendered_messages - .into_iter() - .map(rendered_to_history_message) - .collect(), - images, - info, - "persisted", - ) + ) + }; + let messages = rendered_messages + .into_iter() + .map(rendered_to_history_message) + .collect(); + let (messages, images, info) = if recent_history { + let (messages, images, info) = + recent_history_window(messages, images, visible_messages); + (messages, images, Some(info)) + } else { + (messages, images, info) + }; + (messages, images, info, "persisted") } }; @@ -551,6 +565,113 @@ pub(super) async fn handle_get_compacted_history( .await } +/// Window canonical rendered entries, not raw provider messages. Images follow +/// their rendered entry and the synthetic marker occupies its own index. +pub(super) fn recent_history_window( + mut messages: Vec, + images: Vec, + visible: usize, +) -> ( + Vec, + Vec, + crate::session::RenderedCompactedHistoryInfo, +) { + let total = messages.len(); + let start = total.saturating_sub(visible.max(1)); + let archived_prompts = messages + .iter() + .filter(|message| { + message.role == "system" + && message + .content + .starts_with("Earlier conversation compacted - ") + }) + .filter_map(|message| { + message + .content + .split("Hidden user prompts: ") + .nth(1)? + .split('.') + .next()? + .parse::() + .ok() + }) + .sum::(); + let is_prompt = |message: &&HistoryMessage| { + message.role == "user" && !crate::session::is_attached_image_label_text(&message.content) + }; + let excluded_prompts = messages[..start].iter().filter(is_prompt).count(); + let hidden_user_prompts = archived_prompts + excluded_prompts; + let info = crate::session::RenderedCompactedHistoryInfo { + total_messages: total, + visible_messages: total - start, + remaining_messages: start, + hidden_user_prompts, + }; + if start == 0 { + return (messages, images, info); + } + messages = messages.split_off(start); + messages.insert(0, HistoryMessage { + response_stats: None, + role: "system".to_owned(), + content: format!("{}{start} older historical messages hidden. Showing {} of {total} messages. Scroll to the top to load more. Hidden user prompts: {hidden_user_prompts}.", crate::protocol::RECENT_HISTORY_MARKER_PREFIX, info.visible_messages), + tool_calls: None, + tool_data: None, + }); + let visible_prompts = messages.iter().filter(is_prompt).count(); + let images = images + .into_iter() + .filter_map(|mut image| { + match &mut image.anchor { + Some(crate::session::RenderedImageAnchor::UserPrompt { ordinal }) => { + *ordinal = ordinal.checked_sub(excluded_prompts)?; + if *ordinal >= visible_prompts { + return None; + } + } + Some(crate::session::RenderedImageAnchor::ToolCall { id }) => { + if !messages.iter().any(|message| { + message + .tool_data + .as_ref() + .is_some_and(|tool| &tool.id == id) + }) { + return None; + } + } + None if image.history_message_index.is_none() => return None, + None => {} + } + if let Some(index) = image.history_message_index { + if index < start || index > total { + return None; + } + image.history_message_index = Some(index - start + 1); + } + Some(image) + }) + .collect(); + (messages, images, info) +} + +fn apply_recent_history_base( + messages: Vec, + images: Vec, + history_base: Option, + messages_base_count: Option, +) -> (Vec, Vec) { + if messages_base_count.is_none() + && let Some(visible) = history_base + .filter(|base| base.count == 0) + .and_then(|base| base.recent_messages) + { + let (messages, images, _) = recent_history_window(messages, images, visible); + return (messages, images); + } + (messages, images) +} + /// Trim History to the messages after the client's verified prefix. /// /// Returns the full list with `None` when no base was offered, the base is @@ -821,6 +942,8 @@ async fn send_history_from_persisted_session( .map(rendered_to_history_message) .collect(); let (messages, messages_base_count) = apply_history_base(messages, history_base); + let (messages, images) = + apply_recent_history_base(messages, images, history_base, messages_base_count); let (images, images_unchanged) = apply_history_images_base( images, history_base.filter(|_| messages_base_count.is_some()), @@ -1154,6 +1277,8 @@ async fn send_history_with_guard( ) }); let (messages, messages_base_count) = apply_history_base(messages, history_base); + let (messages, images) = + apply_recent_history_base(messages, images, history_base, messages_base_count); let (images, images_unchanged) = apply_history_images_base( images, history_base.filter(|_| messages_base_count.is_some()), diff --git a/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs b/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs index a379d2d623a..64fb5aaa883 100644 --- a/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs +++ b/crates/jcode-app-core/src/server/client_state_tests/history_delta.rs @@ -24,6 +24,7 @@ fn delta_test_base( .map(crate::protocol::HistoryMessageRef::from), ), images_fingerprint: None, + recent_messages: None, } } @@ -80,6 +81,7 @@ fn history_images_base_omits_only_matching_image_sets() { count: 1, fingerprint: 0, images_fingerprint: Some(fingerprint), + recent_messages: None, }; let matching = crate::protocol::history_images_fingerprint(&images); let (sent, unchanged) = @@ -92,3 +94,275 @@ fn history_images_base_omits_only_matching_image_sets() { let (sent, unchanged) = super::super::apply_history_images_base(images, None); assert!(!unchanged && sent.len() == 2); } + +#[test] +fn recent_history_bounds_seven_thousand_message_bootstrap() { + let messages: Vec<_> = (0..7000) + .map(|index| crate::protocol::HistoryMessage { + response_stats: None, + role: if index % 2 == 0 { "user" } else { "assistant" }.to_owned(), + content: format!("{index}: {}", "x".repeat(2000)), + tool_calls: None, + tool_data: None, + }) + .collect(); + let full_bytes = serde_json::to_vec(&messages).unwrap().len(); + let (window, images) = super::super::apply_recent_history_base( + messages, + Vec::new(), + Some(crate::protocol::HistoryBase::recent(128)), + None, + ); + let window_bytes = serde_json::to_vec(&window).unwrap().len(); + assert!(full_bytes > 14_000_000); + assert!(window_bytes < 512 * 1024); + assert_eq!(window.len(), 129); + assert!(images.is_empty()); + assert_eq!( + window[0].content, + "Earlier remote history - 6872 older historical messages hidden. Showing 128 of 7000 messages. Scroll to the top to load more. Hidden user prompts: 3436." + ); + assert!(window[1].content.starts_with("6872:")); + eprintln!("recent history bootstrap: {full_bytes} -> {window_bytes} bytes"); +} + +#[test] +fn recent_history_preserves_archive_prompt_offset_and_rebases_images() { + let mut messages = delta_test_messages(); + messages.insert(0, crate::protocol::HistoryMessage { + response_stats: None, role: "system".to_owned(), + content: "Earlier conversation compacted - 10 historical messages hidden from the UI. Scroll to the top to load older history. Hidden user prompts: 7.".to_owned(), + tool_calls: None, tool_data: None, + }); + let image = |ordinal| crate::session::RenderedImage { + media_type: "image/png".to_owned(), + data: "data".to_owned(), + label: None, + source: crate::session::RenderedImageSource::UserInput, + anchor: Some(crate::session::RenderedImageAnchor::UserPrompt { ordinal }), + history_message_index: None, + }; + let (window, images, info) = + super::super::recent_history_window(messages.clone(), vec![image(0), image(1)], 2); + assert_eq!( + ( + info.total_messages, + info.visible_messages, + info.remaining_messages, + info.hidden_user_prompts + ), + (5, 2, 3, 8) + ); + assert_eq!(window.len(), 3); + assert_eq!(images.len(), 1); + assert_eq!( + images[0].anchor, + Some(crate::session::RenderedImageAnchor::UserPrompt { ordinal: 0 }) + ); + let (full, _, info) = super::super::recent_history_window(messages.clone(), Vec::new(), 100); + assert_eq!(info.remaining_messages, 0); + assert_eq!( + serde_json::to_value(&full).unwrap(), + serde_json::to_value(&messages).unwrap() + ); + assert!( + full[0] + .content + .starts_with("Earlier conversation compacted - ") + ); +} + +#[test] +fn recent_history_never_mixes_with_validated_or_stale_prefix() { + let messages = delta_test_messages(); + let mut base = delta_test_base(&messages, 2); + base.recent_messages = Some(1); + let (tail, count) = super::super::apply_history_base(messages.clone(), Some(base)); + let (tail, _) = super::super::apply_recent_history_base(tail, Vec::new(), Some(base), count); + assert_eq!(tail.len(), 2); + base.fingerprint = 0; + let (full, count) = super::super::apply_history_base(messages.clone(), Some(base)); + let (full, _) = super::super::apply_recent_history_base(full, Vec::new(), Some(base), count); + assert_eq!( + serde_json::to_value(&full).unwrap(), + serde_json::to_value(&messages).unwrap() + ); + let (full, _) = + super::super::apply_recent_history_base(messages.clone(), Vec::new(), None, None); + assert_eq!( + serde_json::to_value(&full).unwrap(), + serde_json::to_value(&messages).unwrap() + ); +} + +#[test] +fn recent_history_never_truncates_one_large_message() { + let mut messages = delta_test_messages(); + messages.last_mut().unwrap().content = "x".repeat(1024 * 1024); + let expected = messages.last().unwrap().content.clone(); + let (window, _, _) = super::super::recent_history_window(messages, Vec::new(), 1); + assert_eq!(window[1].content, expected); +} + +#[test] +fn recent_history_scopes_tool_images_and_rebases_append_boundaries() { + let mut messages = delta_test_messages(); + messages[3].role = "tool".to_owned(); + messages[3].tool_data = Some(crate::message::ToolCall { + id: "visible-tool".to_owned(), + name: "read".to_owned(), + ..Default::default() + }); + let image = |id: &str, index| crate::session::RenderedImage { + media_type: "image/png".to_owned(), + data: id.to_owned(), + label: None, + source: crate::session::RenderedImageSource::ToolResult { + tool_name: "read".to_owned(), + }, + anchor: Some(crate::session::RenderedImageAnchor::ToolCall { id: id.to_owned() }), + history_message_index: Some(index), + }; + let (window, images, _) = super::super::recent_history_window( + messages, + vec![image("hidden-tool", 1), image("visible-tool", 4)], + 2, + ); + assert_eq!(images.len(), 1); + assert_eq!(images[0].history_message_index, Some(window.len())); + assert_eq!(images[0].data, "visible-tool"); +} + +#[tokio::test] +#[expect( + clippy::await_holding_lock, + reason = "serialize storage and exercise busy-agent fallback" +)] +async fn recent_history_live_and_busy_bootstrap_and_expansion() { + use super::*; + let _env = crate::storage::lock_test_env(); + let home = tempfile::TempDir::new().unwrap(); + let previous = std::env::var_os("JCODE_HOME"); + crate::env::set_var("JCODE_HOME", home.path()); + let session_id = "session_recent_window_paths"; + let mut session = crate::session::Session::create_with_id(session_id.to_owned(), None, None); + for index in 0..8 { + session.append_stored_message(crate::session::StoredMessage { + id: format!("message-{index}"), + role: if index % 2 == 0 { + crate::message::Role::User + } else { + crate::message::Role::Assistant + }, + content: vec![crate::message::ContentBlock::Text { + text: format!("message {index}"), + cache_control: None, + }], + display_role: None, + timestamp: None, + tool_duration_ms: None, + token_usage: None, + }); + } + let provider: Arc = Arc::new(MockProvider(None)); + let agent = Arc::new(Mutex::new(Agent::new_with_session( + provider.clone(), + Registry::empty(), + session, + None, + ))); + let sessions = Arc::new(crate::server::session_manager::SessionManager::from_agents( + HashMap::from([(session_id.to_owned(), agent.clone())]), + )); + let connections = Arc::new(RwLock::new(HashMap::new())); + let count = Arc::new(RwLock::new(1)); + for busy in [false, true] { + let guard = if busy { Some(agent.lock().await) } else { None }; + for visible in [Some(2), None] { + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + let writer = Arc::new(Mutex::new(write)); + tokio::time::timeout( + Duration::from_secs(2), + super::super::handle_get_history( + 1, + session_id, + busy, + &agent, + &provider, + &sessions, + &connections, + &count, + &writer, + "test", + "", + None, + false, + false, + visible.map(crate::protocol::HistoryBase::recent), + ), + ) + .await + .expect("history must not wait for agent") + .unwrap(); + let mut reader = tokio::io::BufReader::new(peer); + let mut line = String::new(); + tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line) + .await + .unwrap(); + let event: crate::protocol::ServerEvent = serde_json::from_str(&line).unwrap(); + let crate::protocol::ServerEvent::History { + messages, + messages_base_count, + .. + } = event + else { + panic!("expected History") + }; + assert_eq!(messages_base_count, None); + assert_eq!(messages.len(), if visible.is_some() { 3 } else { 8 }); + assert_eq!(messages.last().unwrap().content, "message 7"); + } + for visible in [4, 100] { + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + let writer = Arc::new(Mutex::new(write)); + tokio::time::timeout( + Duration::from_secs(2), + super::super::handle_get_compacted_history( + 2, session_id, &agent, &writer, visible, true, + ), + ) + .await + .expect("paging must not wait for agent") + .unwrap(); + let mut reader = tokio::io::BufReader::new(peer); + let mut line = String::new(); + tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line) + .await + .unwrap(); + let event: crate::protocol::ServerEvent = serde_json::from_str(&line).unwrap(); + let crate::protocol::ServerEvent::CompactedHistory { + messages, + compacted_total, + compacted_visible, + compacted_remaining, + compacted_hidden_prompts, + .. + } = event + else { + panic!("expected CompactedHistory") + }; + assert_eq!(compacted_total, 8); + assert_eq!(compacted_visible, visible.min(8)); + assert_eq!(compacted_remaining, 8 - visible.min(8)); + assert_eq!(compacted_hidden_prompts, (8 - visible.min(8)) / 2); + assert_eq!(messages.len(), if visible == 4 { 5 } else { 8 }); + } + drop(guard); + } + match previous { + Some(value) => crate::env::set_var("JCODE_HOME", value), + None => crate::env::remove_var("JCODE_HOME"), + } +} diff --git a/crates/jcode-protocol/src/lib.rs b/crates/jcode-protocol/src/lib.rs index fa84ac664ab..c6b0cf8b550 100644 --- a/crates/jcode-protocol/src/lib.rs +++ b/crates/jcode-protocol/src/lib.rs @@ -90,6 +90,26 @@ pub struct HistoryBase { /// When it matches, History omits `images` and sets `images_unchanged`. #[serde(default, skip_serializing_if = "Option::is_none")] pub images_fingerprint: Option, + /// Opt into a recent rendered-history window on a full bootstrap. Only + /// honored when `count` is zero, never combined with prefix validation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recent_messages: Option, +} + +/// Synthetic lazy-window marker used by remote transcript bootstraps. +pub const RECENT_HISTORY_MARKER_PREFIX: &str = "Earlier remote history - "; + +impl HistoryBase { + /// Request a bounded initial view. Older servers ignore this hint and + /// return their ordinary full History payload. + pub fn recent(visible: usize) -> Self { + Self { + count: 0, + fingerprint: 0, + images_fingerprint: None, + recent_messages: Some(visible), + } + } } /// Stable fingerprint of a History image set (order, metadata and data). @@ -195,6 +215,7 @@ impl HistoryFingerprinter { count: self.count, fingerprint: self.finish(), images_fingerprint: None, + recent_messages: None, } } } diff --git a/crates/jcode-protocol/src/protocol_tests/core_events.rs b/crates/jcode-protocol/src/protocol_tests/core_events.rs index 053317f7ced..098d2436952 100644 --- a/crates/jcode-protocol/src/protocol_tests/core_events.rs +++ b/crates/jcode-protocol/src/protocol_tests/core_events.rs @@ -243,6 +243,7 @@ fn test_soft_interrupt_ids_wire_shape_and_legacy_defaults() -> Result<()> { fn test_compacted_history_request_roundtrip() -> Result<()> { let req = Request::GetCompactedHistory { id: 7, + recent_history: false, visible_messages: 64, }; let json = serde_json::to_string(&req)?; @@ -259,6 +260,40 @@ fn test_compacted_history_request_roundtrip() -> Result<()> { Ok(()) } +#[test] +fn recent_history_schema_is_opt_in_and_legacy_compatible() -> Result<()> { + let legacy: crate::HistoryBase = serde_json::from_str(r#"{"count":0,"fingerprint":0}"#)?; + assert_eq!(legacy.recent_messages, None); + assert!(!serde_json::to_string(&legacy)?.contains("recent_messages")); + let recent = crate::HistoryBase::recent(128); + assert_eq!(recent.count, 0); + assert_eq!(recent.fingerprint, 0); + assert_eq!(recent.images_fingerprint, None); + let decoded: crate::HistoryBase = serde_json::from_str(&serde_json::to_string(&recent)?)?; + assert_eq!(decoded.recent_messages, Some(128)); + let legacy = + parse_request_json(r#"{"type":"get_compacted_history","id":1,"visible_messages":64}"#)?; + assert!(matches!( + legacy, + Request::GetCompactedHistory { + recent_history: false, + .. + } + )); + let recent = parse_request_json( + r#"{"type":"get_compacted_history","id":1,"visible_messages":192,"recent_history":true}"#, + )?; + assert!(matches!( + recent, + Request::GetCompactedHistory { + recent_history: true, + visible_messages: 192, + .. + } + )); + Ok(()) +} + #[test] fn test_notify_auth_changed_provider_hint_is_optional() -> Result<()> { let legacy = r#"{"type":"notify_auth_changed","id":9}"#; diff --git a/crates/jcode-protocol/src/wire.rs b/crates/jcode-protocol/src/wire.rs index 6893d081c7e..16de8f32db7 100644 --- a/crates/jcode-protocol/src/wire.rs +++ b/crates/jcode-protocol/src/wire.rs @@ -228,6 +228,9 @@ pub enum Request { #[serde(rename = "get_compacted_history")] GetCompactedHistory { id: u64, + /// Expand a recent remote window rather than archived compaction. + #[serde(default)] + recent_history: bool, /// Number of leading compacted messages the client wants rendered before the live tail. visible_messages: usize, }, From 2dfce36efc00569b5fb6d5f2a20f90b5378a15a4 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:05:25 -0800 Subject: [PATCH 016/236] Sync opted-in mobile transcripts promptly after persistence --- crates/jcode-base/src/mobile_sync.rs | 576 ++++++++++++++++++- crates/jcode-base/src/session/persistence.rs | 1 + 2 files changed, 549 insertions(+), 28 deletions(-) diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index 961cf83df72..f2769c2aa33 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -4,7 +4,9 @@ //! session transcripts to the Jcode account API so the iOS app can show them. //! Local sessions and sessions running on Jcode Cloud machines both upload with //! the account API key. Uploads are incremental: per-session high-water marks -//! (message count + snapshot mtime) live in `~/.jcode/mobile_sync.json`. +//! (message count + snapshot/journal revisions) live in `~/.jcode/mobile_sync.json`. +//! Successful session persistence wakes a debounced incremental pass. A 20s +//! reconciliation also catches saves made by other processes and retries. //! //! Only user-visible text is uploaded: user/assistant text, tool calls as a //! one-line summary, and tool results truncated. Hidden reasoning, images and @@ -16,8 +18,10 @@ use crate::subscription_api; use anyhow::{Context, Result}; use jcode_message_types::{ContentBlock, Role}; use serde::{Deserialize, Serialize}; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; +use std::sync::Mutex; use std::time::{Duration, SystemTime, UNIX_EPOCH}; pub const SYNC_INTERVAL: Duration = Duration::from_secs(20); @@ -26,12 +30,43 @@ const MAX_TOOL_RESULT_CHARS: usize = 4_000; const MAX_TEXT_CHARS: usize = 100_000; /// Only sessions touched within this window are synced on enable. const BACKFILL_WINDOW: Duration = Duration::from_secs(14 * 24 * 3600); -const MAX_SESSIONS_PER_PASS: usize = 200; +const SYNC_DEBOUNCE: Duration = Duration::from_millis(150); +static PERSISTED: tokio::sync::Notify = tokio::sync::Notify::const_new(); +static DIRTY_SESSIONS: Mutex> = Mutex::new(BTreeSet::new()); +static SYNC_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); + +/// Called only after a durable session write, never after sync-state writes. +/// Notify retains a permit while an upload is running. The set also retains +/// same-millisecond writes which the reconciliation mtime cannot distinguish. +pub(crate) fn notify_session_persisted(id: &str) { + DIRTY_SESSIONS + .lock() + .unwrap_or_else(|e| e.into_inner()) + .insert(id.to_owned()); + PERSISTED.notify_one(); +} + +fn state_lock() -> Result { + let dir = storage::jcode_dir()?; + std::fs::create_dir_all(&dir)?; + let path = dir.join("mobile_sync.lock"); + let file = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(path)?; + file.lock()?; + Ok(file) +} #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] pub struct MobileSyncState { #[serde(default)] pub enabled: bool, + /// Fences uploads started before a disable/re-enable cycle. + #[serde(default)] + pub generation: u64, /// session id -> progress #[serde(default)] pub sessions: BTreeMap, @@ -43,9 +78,17 @@ pub struct SessionProgress { pub uploaded: usize, /// Snapshot+journal mtime (unix ms) at the last successful pass. pub mtime_ms: u64, + /// Snapshot and journal (nanosecond mtime, byte length). Older state files + /// omit this and receive one full reconciliation on upgrade. + #[serde(default)] + pub revision: Option<[u64; 4]>, + /// Hash of previously uploaded visible entries. Rewrites below the count + /// high-water mark must be replayed, not mistaken for metadata-only saves. + #[serde(default)] + pub fingerprint: Option, } -#[derive(Debug, Clone, Serialize, PartialEq)] +#[derive(Debug, Clone, Serialize, PartialEq, Hash)] pub struct WireMessage { pub seq: usize, pub role: &'static str, @@ -150,13 +193,59 @@ pub fn load_state() -> MobileSyncState { } pub fn save_state(state: &MobileSyncState) -> Result<()> { - storage::write_json_secret(&state_path()?, state) + let _lock = state_lock()?; + // Opt-in/out is authoritative, but an enable based on a stale read must + // not discard progress committed by another daemon in the meantime. + let mut next = load_state(); + if next.enabled != state.enabled { + next.generation = next.generation.wrapping_add(1); + } + next.enabled = state.enabled; + if state.enabled { + if next.generation == state.generation { + merge_progress(&mut next, &state.sessions); + } + } else { + next.sessions.clear(); + } + storage::write_json_secret(&state_path()?, &next) +} + +fn merge_progress(state: &mut MobileSyncState, updates: &BTreeMap) { + for (id, progress) in updates { + let version = |p: &SessionProgress| { + p.revision + .map(|r| r[0].max(r[2])) + .unwrap_or(p.mtime_ms.saturating_mul(1_000_000)) + }; + if state.sessions.get(id).is_none_or(|old| { + version(old) < version(progress) + || (version(old) == version(progress) && old.uploaded <= progress.uploaded) + }) { + state.sessions.insert(id.clone(), progress.clone()); + } + } +} + +fn commit_progress(generation: u64, updates: &BTreeMap) -> Result<()> { + let _lock = state_lock()?; + let mut current = load_state(); + if current.enabled && current.generation == generation { + merge_progress(&mut current, updates); + storage::write_json_secret(&state_path()?, ¤t)?; + } + Ok(()) } pub fn is_enabled() -> bool { load_state().enabled } +fn sync_still_enabled(generation: u64) -> bool { + let state = load_state(); + state.enabled && state.generation == generation +} + /// `cloud` when running on a Jcode Cloud machine, `local` otherwise. pub fn session_source() -> &'static str { if std::env::var_os("JCODE_CLOUD_ENV_SESSION").is_some() @@ -262,6 +351,25 @@ pub fn wire_messages(session: &Session, start: usize) -> Vec { .collect() } +fn wire_fingerprint(messages: &[WireMessage], end: usize) -> u64 { + let mut hash = std::collections::hash_map::DefaultHasher::new(); + for message in messages.iter().take_while(|message| message.seq < end) { + message.hash(&mut hash); + } + hash.finish() +} + +fn incremental_messages(session: &Session, progress: &SessionProgress) -> (Vec, u64) { + let mut messages = wire_messages(session, 0); + let fingerprint = wire_fingerprint(&messages, session.messages.len()); + let unchanged_prefix = progress.uploaded <= session.messages.len() + && progress.fingerprint == Some(wire_fingerprint(&messages, progress.uploaded)); + if unchanged_prefix { + messages.retain(|message| message.seq >= progress.uploaded); + } + (messages, fingerprint) +} + fn mtime_ms(path: &Path) -> u64 { match std::fs::metadata(path).and_then(|metadata| metadata.modified()) { Ok(modified) => match modified.duration_since(UNIX_EPOCH) { @@ -285,6 +393,26 @@ fn session_mtime_ms(snapshot: &Path) -> u64 { )) } +fn session_revision(snapshot: &Path) -> [u64; 4] { + fn stamp(path: &Path) -> (u64, u64) { + let Ok(meta) = std::fs::metadata(path) else { + return (0, 0); + }; + let modified = meta + .modified() + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|d| d.as_nanos().min(u64::MAX as u128) as u64) + .unwrap_or(0); + (modified, meta.len()) + } + let (snapshot_time, snapshot_len) = stamp(snapshot); + let (journal_time, journal_len) = stamp(&crate::session::session_journal_path_from_snapshot( + snapshot, + )); + [snapshot_time, snapshot_len, journal_time, journal_len] +} + /// Candidate sessions: real (non-debug) snapshots modified since the window. fn candidate_sessions(since_ms: u64) -> Result> { let dir = storage::jcode_dir()?.join("sessions"); @@ -310,7 +438,6 @@ fn candidate_sessions(since_ms: u64) -> Result> { } } out.sort_by(|a, b| b.2.cmp(&a.2)); - out.truncate(MAX_SESSIONS_PER_PASS); Ok(out) } @@ -364,6 +491,11 @@ fn valid_session_id(id: &str) -> bool { /// One incremental sync pass. Safe to call repeatedly. pub async fn sync_once(client: &reqwest::Client) -> Result { + sync_pass(client, true).await +} + +async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result { + let _pass = SYNC_PASS.lock().await; let mut state = load_state(); if !state.enabled { return Ok(SyncReport::default()); @@ -379,8 +511,26 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { let host = host_name(); let source = session_source(); let mut report = SyncReport::default(); + let mut candidates = if reconcile { + candidate_sessions(since)? + } else { + Vec::new() + }; + let dirty = std::mem::take(&mut *DIRTY_SESSIONS.lock().unwrap_or_else(|e| e.into_inner())); + for id in &dirty { + if valid_session_id(id) && !candidates.iter().any(|(candidate, _, _)| candidate == id) { + let path = storage::jcode_dir()? + .join("sessions") + .join(format!("{id}.json")); + candidates.push((id.clone(), path.clone(), session_mtime_ms(&path))); + } + } + candidates.sort_by_key(|(id, _, _)| !dirty.contains(id)); - for (id, path, mtime) in candidate_sessions(since)? { + for (id, path, mtime) in candidates { + if !sync_still_enabled(state.generation) { + break; + } if !valid_session_id(&id) { continue; } @@ -390,12 +540,19 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { .get(&id) .cloned() .unwrap_or_else(SessionProgress::default); - if progress.mtime_ms >= mtime { + let revision = session_revision(&path); + if progress.revision == Some(revision) && !dirty.contains(&id) { continue; } let session = match Session::load_from_path(&path) { Ok(s) => s, - Err(_) => continue, + Err(_) => { + DIRTY_SESSIONS + .lock() + .unwrap_or_else(|e| e.into_inner()) + .insert(id); + continue; + } }; if session.is_debug || session.messages.is_empty() { state.sessions.insert( @@ -403,12 +560,13 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { SessionProgress { uploaded: progress.uploaded, mtime_ms: mtime, + revision: Some(revision), + fingerprint: progress.fingerprint, }, ); continue; } - let start = progress.uploaded.min(session.messages.len()); - let messages = wire_messages(&session, start); + let (messages, fingerprint) = incremental_messages(&session, &progress); let title = session.custom_title.as_deref().or(session.title.as_deref()); let mut ok = true; // Always send at least one request so metadata (title, updated_at) refreshes. @@ -418,6 +576,10 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { messages.chunks(MAX_MESSAGES_PER_UPLOAD).collect() }; for chunk in chunks { + if !sync_still_enabled(state.generation) { + ok = false; + break; + } let body = UploadBody { title, source, @@ -443,15 +605,20 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { SessionProgress { uploaded: session.messages.len(), mtime_ms: mtime, + revision: Some(revision), + fingerprint: Some(fingerprint), }, ); + } else { + DIRTY_SESSIONS + .lock() + .unwrap_or_else(|e| e.into_inner()) + .insert(id); } } - // Reload `enabled` so a concurrent `jcode mobile disable` wins. - state.enabled = load_state().enabled; - if state.enabled { - save_state(&state)?; - } + // Merge under a cross-process lock, never write a stale enabled flag or + // overwrite another process's unrelated session progress. + commit_progress(state.generation, &state.sessions)?; Ok(report) } @@ -477,20 +644,30 @@ pub async fn wipe_remote(client: &reqwest::Client) -> Result<()> { /// Background loop used by the jcode server daemon. pub fn spawn_background_sync() { - tokio::spawn(async move { - let client = reqwest::Client::new(); - let mut ticks = tokio::time::interval(SYNC_INTERVAL); - ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); - loop { - ticks.tick().await; - if !is_enabled() { - continue; - } - if let Err(err) = sync_once(&client).await { - crate::logging::debug(&format!("mobile sync pass failed: {err:#}")); + tokio::spawn(background_sync()); +} + +async fn background_sync() { + let client = reqwest::Client::new(); + let mut ticks = tokio::time::interval(SYNC_INTERVAL); + ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + loop { + let reconcile = tokio::select! { + _ = ticks.tick() => true, + _ = PERSISTED.notified() => { + // A bounded coalescing window, not a trailing debounce: + // continuous token saves cannot postpone sync forever. + tokio::time::sleep(SYNC_DEBOUNCE).await; + false } + }; + if !is_enabled() { + continue; + } + if let Err(err) = sync_pass(&client, reconcile).await { + crate::logging::debug(&format!("mobile sync pass failed: {err:#}")); } - }); + } } #[cfg(test)] @@ -498,6 +675,308 @@ mod tests { use super::*; use jcode_session_types::StoredMessage; + struct TestEnv(Vec<(&'static str, Option)>); + + impl TestEnv { + fn set(values: &[(&'static str, &std::ffi::OsStr)]) -> Self { + Self( + values + .iter() + .map(|(name, value)| { + let previous = std::env::var_os(name); + crate::env::set_var(name, value); + (*name, previous) + }) + .collect(), + ) + } + } + + impl Drop for TestEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + if let Some(value) = value { + crate::env::set_var(name, value); + } else { + crate::env::remove_var(name); + } + } + } + } + + #[test] + fn state_commits_merge_concurrent_progress_and_never_reenable() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let generation = load_state().generation; + let progress = |uploaded, mtime_ms| SessionProgress { + uploaded, + mtime_ms, + ..Default::default() + }; + let stale = BTreeMap::from([("first".into(), progress(1, 1))]); + commit_progress( + generation, + &BTreeMap::from([("second".into(), progress(2, 2))]), + ) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert_eq!(load_state().sessions.len(), 2); + commit_progress( + generation, + &BTreeMap::from([("first".into(), progress(3, 3))]), + ) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert_eq!(load_state().sessions["first"].uploaded, 3); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(8)); + let writers: Vec<_> = (0..8) + .map(|index| { + let barrier = barrier.clone(); + std::thread::spawn(move || { + barrier.wait(); + commit_progress( + generation, + &BTreeMap::from([(format!("parallel-{index}"), progress(index, 10))]), + ) + .unwrap(); + }) + }) + .collect(); + for writer in writers { + writer.join().unwrap(); + } + assert_eq!(load_state().sessions.len(), 10); + save_state(&MobileSyncState::default()).unwrap(); + commit_progress(generation, &stale).unwrap(); + assert!(!load_state().enabled); + assert!(load_state().sessions.is_empty()); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert!( + load_state().sessions.is_empty(), + "old opt-in generation cannot restore wiped progress" + ); + } + + #[tokio::test] + async fn persistence_notifications_upload_under_one_second_and_retain_inflight_writes() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("test-key")), + ("JCODE_API_BASE", std::ffi::OsStr::new(&base)), + ]); + let (requests_tx, mut requests) = tokio::sync::mpsc::channel(8); + let server = tokio::spawn(async move { + loop { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0; 4096]; + let body_start = loop { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + if let Some(index) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { + break index + 4; + } + }; + let header = String::from_utf8_lossy(&bytes[..body_start]); + let length: usize = header + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length:") + .map(|s| s.trim().parse().unwrap()) + }) + .unwrap(); + while bytes.len() < body_start + length { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + } + let body: serde_json::Value = + serde_json::from_slice(&bytes[body_start..body_start + length]).unwrap(); + let (release, wait) = tokio::sync::oneshot::channel::<()>(); + requests_tx.send((body, release)).await.unwrap(); + wait.await.unwrap(); + socket + .write_all( + b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}", + ) + .await + .unwrap(); + } + }); + let background = tokio::spawn(background_sync()); + // Consume the initial reconciliation while disabled. + tokio::time::sleep(Duration::from_millis(30)).await; + let mut session = Session::create_with_id("event-sync".into(), None, None); + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "first".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + assert!(!is_enabled()); + assert!( + tokio::time::timeout(Duration::from_millis(220), requests.recv()) + .await + .is_err() + ); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let started = std::time::Instant::now(); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "second".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + for title in ["coalesce one", "coalesce two", "coalesce final"] { + session.custom_title = Some(title.into()); + session.save().unwrap(); + } + let (first, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + eprintln!( + "mobile persistence-to-upload notification: {}ms", + started.elapsed().as_millis() + ); + assert!(started.elapsed() < Duration::from_secs(1)); + assert_eq!(first["messages"].as_array().unwrap().len(), 2); + assert_eq!(first["title"], "coalesce final"); + // Persist while the first upload is still blocked in the network. + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "during-upload".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + release.send(()).unwrap(); + let (second, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(second["messages"].as_array().unwrap().len(), 1); + assert_eq!(second["messages"][0]["seq"], 2); + assert_eq!(second["messages"][0]["text"], "during-upload"); + release.send(()).unwrap(); + // Progress persistence itself must not schedule another upload. + assert!( + tokio::time::timeout(Duration::from_millis(350), requests.recv()) + .await + .is_err() + ); + assert_eq!(load_state().sessions["event-sync"].uploaded, 3); + // Simulate a writer in another process: no notification hook in this + // process, so only the reconciliation path can discover the update. + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "external writer".into(), + cache_control: None, + }], + ); + let path = home.path().join("sessions/event-sync.json"); + storage::write_json_fast(&path, &session).unwrap(); + session = Session::load_from_path(&path).unwrap(); + let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + let (external, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(external["messages"].as_array().unwrap().len(), 1); + assert_eq!(external["messages"][0]["text"], "external writer"); + release.send(()).unwrap(); + assert_eq!(reconcile.await.unwrap().messages_uploaded, 1); + session.custom_title = Some("metadata only".into()); + session.save().unwrap(); + let (metadata, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(metadata["title"], "metadata only"); + assert!(metadata["messages"].as_array().unwrap().is_empty()); + // A disable racing an upload must not be undone by its completion. + save_state(&MobileSyncState::default()).unwrap(); + release.send(()).unwrap(); + tokio::time::sleep(Duration::from_millis(220)).await; + assert!(!load_state().enabled); + assert!(load_state().sessions.is_empty()); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "disabled".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(250), requests.recv()) + .await + .is_err() + ); + background.abort(); + server.abort(); + let _ = background.await; + let _ = server.await; + } + + #[test] + fn reconciliation_does_not_starve_old_candidates_or_miss_same_millisecond_writes() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + let sessions = home.path().join("sessions"); + std::fs::create_dir_all(&sessions).unwrap(); + for index in 0..205 { + std::fs::write(sessions.join(format!("session-{index}.json")), b"{}").unwrap(); + } + assert_eq!(candidate_sessions(0).unwrap().len(), 205); + let path = sessions.join("session-0.json"); + let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); + let original = file.metadata().unwrap().modified().unwrap(); + let before = session_revision(&path); + let before_ms = session_mtime_ms(&path); + std::fs::write(&path, b"{\"changed\":true}").unwrap(); + file.set_times(std::fs::FileTimes::new().set_modified(original)) + .unwrap(); + assert_eq!(session_mtime_ms(&path), before_ms); + assert_ne!(session_revision(&path), before); + let journal = crate::session::session_journal_path_from_snapshot(&path); + std::fs::write(&journal, b"journal update\n").unwrap(); + let with_journal = session_revision(&path); + std::fs::remove_file(journal).unwrap(); + assert_ne!(session_revision(&path), with_journal); + assert_eq!(SYNC_INTERVAL, Duration::from_secs(20)); + } + async fn mock_listing(status: &str, body: &str) -> (String, tokio::task::JoinHandle) { use tokio::io::{AsyncReadExt, AsyncWriteExt}; @@ -652,4 +1131,45 @@ mod tests { assert!(!valid_session_id("bad id")); assert!(!valid_session_id("")); } + + #[test] + fn incremental_updates_replay_rewritten_and_shrunk_prefixes() { + let mut session = Session::create_with_id("rewrite".into(), None, None); + for text in ["first", "second"] { + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: text.into(), + cache_control: None, + }], + ); + } + let (initial, fingerprint) = incremental_messages(&session, &SessionProgress::default()); + assert_eq!(initial.len(), 2); + let progress = SessionProgress { + uploaded: 2, + fingerprint: Some(fingerprint), + ..Default::default() + }; + assert!(incremental_messages(&session, &progress).0.is_empty()); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "appended".into(), + cache_control: None, + }], + ); + let appended = incremental_messages(&session, &progress).0; + assert_eq!(appended.len(), 1); + assert_eq!(appended[0].seq, 2); + session.messages[0].content = vec![ContentBlock::Text { + text: "rewritten".into(), + cache_control: None, + }]; + let rewritten = incremental_messages(&session, &progress).0; + assert_eq!(rewritten.len(), 3); + assert_eq!(rewritten[0].text, "rewritten"); + session.truncate_messages(1); + assert_eq!(incremental_messages(&session, &progress).0.len(), 1); + } } diff --git a/crates/jcode-base/src/session/persistence.rs b/crates/jcode-base/src/session/persistence.rs index 88b2920fc00..5118f2c7747 100644 --- a/crates/jcode-base/src/session/persistence.rs +++ b/crates/jcode-base/src/session/persistence.rs @@ -875,6 +875,7 @@ impl Session { fields.push(("error", crate::util::format_error_chain(error))); crate::logging::event_warn("SESSION_PERSISTENCE", fields); } else { + crate::mobile_sync::notify_session_persisted(&self.id); if let Err(error) = crate::recent_session_index::upsert_session(self) { crate::logging::warn(&format!( "Failed to update recent-session metadata for {}: {error}", From 4c757af18f66b7fbb20f863f32610a0cb28d4b85 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:07:43 -0800 Subject: [PATCH 017/236] Fix CI: collapse clippy if, refresh size/safety budgets, include pending history changes --- crates/jcode-tui/src/tui/app.rs | 3 + .../src/tui/app/inline_interactive.rs | 8 +- crates/jcode-tui/src/tui/app/remote.rs | 14 +- .../src/tui/app/remote/server_events.rs | 12 +- .../src/tui/app/state_ui_messages.rs | 82 +++++- crates/jcode-tui/src/tui/app/tests.rs | 1 + .../src/tui/app/tests/recent_history.rs | 138 ++++++++++ .../tests/remote_events_reload_03/part_02.rs | 1 + crates/jcode-tui/src/tui/app/tui_lifecycle.rs | 4 + crates/jcode-tui/src/tui/backend.rs | 19 +- crates/jcode-tui/src/tui/backend_tests.rs | 35 +++ scripts/code_size_budget.json | 167 ++++++------ scripts/panic_budget.json | 14 +- scripts/swallowed_error_budget.json | 244 ++++++++---------- scripts/test_size_budget.json | 78 +++--- src/cli/cloud_managed.rs | 109 ++++++-- src/cli/cloud_managed_tests.rs | 170 +++++++++++- src/cli/cloud_move/transport.rs | 107 ++++++-- src/cli/cloud_move/transport/tests.rs | 157 +++++++++++ 19 files changed, 1019 insertions(+), 344 deletions(-) create mode 100644 crates/jcode-tui/src/tui/app/tests/recent_history.rs diff --git a/crates/jcode-tui/src/tui/app.rs b/crates/jcode-tui/src/tui/app.rs index 89daaa901ce..1f437657772 100644 --- a/crates/jcode-tui/src/tui/app.rs +++ b/crates/jcode-tui/src/tui/app.rs @@ -941,6 +941,9 @@ pub struct App { display_edit_line_counts: (usize, usize), terminal_title: RefCell, compacted_history_lazy: CompactedHistoryLazyState, + /// Request id, display generation and target size for a normal transcript page. + recent_history_request: Option<(u64, u64, usize)>, + recent_history_retry_at: Option, /// When older compacted history has just been loaded, this anchors the /// viewport to the content the reader was looking at so the prepend does not /// visibly jump. Resolved into `scroll_offset` by the next render frame. diff --git a/crates/jcode-tui/src/tui/app/inline_interactive.rs b/crates/jcode-tui/src/tui/app/inline_interactive.rs index e3e5f4d7d82..f19b1165ae7 100644 --- a/crates/jcode-tui/src/tui/app/inline_interactive.rs +++ b/crates/jcode-tui/src/tui/app/inline_interactive.rs @@ -2773,11 +2773,9 @@ impl App { match recv_result { Ok(session_picker::SessionListUpdate::CloudUnavailable(warning)) => { - if picker_active { - if let Some(picker) = self.session_picker_overlay.as_ref() { - picker.borrow_mut().set_cloud_warning(warning); - return true; - } + if picker_active && let Some(picker) = self.session_picker_overlay.as_ref() { + picker.borrow_mut().set_cloud_warning(warning); + return true; } false } diff --git a/crates/jcode-tui/src/tui/app/remote.rs b/crates/jcode-tui/src/tui/app/remote.rs index 6341c4363e2..528be595627 100644 --- a/crates/jcode-tui/src/tui/app/remote.rs +++ b/crates/jcode-tui/src/tui/app/remote.rs @@ -690,8 +690,18 @@ async fn dispatch_compacted_history_load(app: &mut App, remote: &mut RemoteConne let Some(visible_messages) = app.take_pending_compacted_history_load() else { return false; }; - match remote.get_compacted_history(visible_messages).await { - Ok(_) => true, + let recent_history = app.recent_history_window_active(); + match remote + .get_compacted_history(visible_messages, recent_history) + .await + { + Ok(id) => { + if recent_history { + app.recent_history_request = + Some((id, app.display_messages_version, visible_messages)); + } + true + } Err(error) => { app.restore_pending_compacted_history_load(visible_messages); app.set_status_notice(format!("Failed to request older history: {}", error)); diff --git a/crates/jcode-tui/src/tui/app/remote/server_events.rs b/crates/jcode-tui/src/tui/app/remote/server_events.rs index 45f239caed9..83979b9ce7e 100644 --- a/crates/jcode-tui/src/tui/app/remote/server_events.rs +++ b/crates/jcode-tui/src/tui/app/remote/server_events.rs @@ -2406,6 +2406,7 @@ fn handle_server_event_inner( true } ServerEvent::CompactedHistory { + id, session_id, messages, images, @@ -2422,7 +2423,10 @@ fn handle_server_event_inner( )); return false; } - let restored_messages = messages + if !app.accept_recent_history_response(id) { + return false; + } + let mut restored_messages: Vec<_> = messages .into_iter() .map(|msg| DisplayMessage { role: msg.role, @@ -2433,6 +2437,12 @@ fn handle_server_event_inner( tool_data: msg.tool_data, }) .collect(); + if !app.recent_history_page_contains_visible_tail( + &mut restored_messages, + compacted_visible, + ) { + return false; + } app.apply_compacted_history_window( restored_messages, images, diff --git a/crates/jcode-tui/src/tui/app/state_ui_messages.rs b/crates/jcode-tui/src/tui/app/state_ui_messages.rs index 6df95cab96f..53b54d68850 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_messages.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_messages.rs @@ -727,6 +727,7 @@ impl App { remaining_messages: usize, hidden_user_prompts: usize, ) { + let recent_history = self.recent_history_window_active(); compact_display_messages_for_storage(&mut messages); carry_over_tool_durations(&self.display_messages, &mut messages); self.display_messages = messages; @@ -739,6 +740,11 @@ impl App { hidden_user_prompts, pending_request_visible: None, }; + if recent_history { + // Once the current transcript is fully visible, its original + // compaction marker (if any) becomes the next lazy history source. + self.sync_compacted_history_lazy_from_display_messages(); + } self.auto_scroll_paused = true; // Older messages are prepended above the current view. If the reader had // an anchor captured (they scrolled up to trigger this load), leave the @@ -935,7 +941,7 @@ impl App { if self.is_remote { self.compacted_history_lazy.pending_request_visible = Some(next_visible); self.set_status_notice(format!( - "Loading older compacted history… {} of {}", + "Loading older history… {} of {}", next_visible, self.compacted_history_lazy.total_messages )); } else { @@ -944,9 +950,76 @@ impl App { } pub(super) fn take_pending_compacted_history_load(&mut self) -> Option { + if self.recent_history_retry_at.is_some_and(|deadline| Instant::now() < deadline) { + return None; + } + self.recent_history_retry_at = None; self.compacted_history_lazy.pending_request_visible.take() } + pub(super) fn recent_history_window_active(&self) -> bool { + self.display_messages.first().is_some_and(|message| { + message.role == "system" + && message + .content + .starts_with(crate::protocol::RECENT_HISTORY_MARKER_PREFIX) + }) + } + + /// A response computed before a live append or rewind must not discard it. + /// Retry against the new generation while retaining the viewport anchor. + pub(super) fn accept_recent_history_response(&mut self, id: u64) -> bool { + let Some((expected, version, visible)) = self.recent_history_request else { + return !self.recent_history_window_active(); + }; + if id != expected { + return false; + } + self.recent_history_request = None; + if version != self.display_messages_version { + if self.recent_history_window_active() { + self.restore_pending_compacted_history_load(visible); + self.recent_history_retry_at = Some(Instant::now() + Duration::from_millis(250)); + } + return false; + } + true + } + + pub(super) fn recent_history_page_contains_visible_tail( + &mut self, + messages: &mut [DisplayMessage], + visible: usize, + ) -> bool { + if !self.recent_history_window_active() { + return true; + } + compact_display_messages_for_storage(messages); + // A busy agent may supply a persisted snapshot. Generation fencing + // alone cannot detect a snapshot already stale when we sent the request. + // Require its real transcript tail to include the rows on screen, while + // ignoring local status notices which are never part of server history. + let meaningful = |role: &str| matches!(role, "user" | "assistant" | "tool"); + let current: Vec<_> = self + .display_messages + .iter() + .filter(|m| meaningful(&m.role)) + .collect(); + let incoming: Vec<_> = messages.iter().filter(|m| meaningful(&m.role)).collect(); + let matches = current.is_empty() + || incoming.windows(current.len()).any(|window| { + window + .iter() + .zip(¤t) + .all(|(new, old)| new.role == old.role && new.content == old.content) + }); + if !matches { + self.restore_pending_compacted_history_load(visible); + self.recent_history_retry_at = Some(Instant::now() + Duration::from_millis(250)); + } + matches + } + /// Whether there are older compacted-history messages not yet loaded into the /// display transcript. pub(super) fn compacted_history_has_remaining(&self) -> bool { @@ -1072,7 +1145,12 @@ fn parse_compacted_history_marker(message: &DisplayMessage) -> Option App { + let mut app = create_test_app(); + app.is_remote = true; + app.remote_session_id = Some("recent-test".into()); + app.replace_display_messages(vec![ + DisplayMessage::system("Earlier remote history - 872 older historical messages hidden. Showing 128 of 1000 messages. Scroll to the top to load more. Hidden user prompts: 99."), + DisplayMessage::user("latest prompt"), + DisplayMessage::assistant("latest answer"), + ]); + app +} + +#[test] +fn recent_history_bootstrap_renders_latest_frame_without_older_transcript() { + let _lock = scroll_render_test_lock(); + let mut app = recent_history_test_app(); + app.diagram_mode = crate::config::DiagramDisplayMode::None; + app.diagram_pane_enabled = false; + app.status = ProcessingStatus::Idle; + let messages = std::iter::once(app.display_messages[0].clone()) + .chain((0..128).map(|i| { + DisplayMessage::assistant(format!("RECENT{i:03} {}", "bounded history ".repeat(128))) + })) + .map(|message| crate::protocol::HistoryMessage { + role: message.role, + content: message.content, + tool_calls: None, + tool_data: None, + response_stats: None, + }) + .collect(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); + let mut remote = crate::tui::backend::RemoteConnection::dummy(); + let start = std::time::Instant::now(); + assert!(app.handle_server_event( + delta_history_event("recent-test", messages, None), + &mut remote + )); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap(); + render_and_snap(&app, &mut terminal); + let elapsed = start.elapsed(); + assert_eq!(app.display_messages.len(), 129); + assert!(buffer_to_text(&terminal).contains("RECENT127")); + assert_eq!(app.compacted_hidden_user_prompts(), 99); + eprintln!( + "recent_history initial event + 128-message frame: {:.2}ms", + elapsed.as_secs_f64() * 1000.0 + ); +} + +#[test] +fn recent_history_marker_preserves_absolute_prompt_numbers_and_queues_page() { + let mut app = recent_history_test_app(); + assert!(app.recent_history_window_active()); + assert_eq!(app.compacted_hidden_user_prompts(), 99); + assert_eq!( + crate::tui::ui::input_ui::next_input_prompt_number(&app), + 101 + ); + app.auto_scroll_paused = true; + app.scroll_offset = 0; + app.maybe_queue_compacted_history_load(); + assert_eq!(app.take_pending_compacted_history_load(), Some(192)); +} + +#[test] +fn recent_history_page_retries_instead_of_discarding_live_append() { + let mut app = recent_history_test_app(); + app.recent_history_request = Some((42, app.display_messages_version, 192)); + app.display_messages + .push(DisplayMessage::assistant("live response after request")); + app.bump_display_messages_version(); + assert!(!app.accept_recent_history_response(42)); + assert_eq!( + app.display_messages.last().unwrap().content, + "live response after request" + ); + assert_eq!(app.compacted_history_lazy.pending_request_visible, Some(192)); + assert_eq!(app.take_pending_compacted_history_load(), None); + app.recent_history_retry_at = Some(Instant::now()); + assert_eq!(app.take_pending_compacted_history_load(), Some(192)); +} + +#[test] +fn recent_history_page_rejects_wrong_request_and_rewind() { + let mut app = recent_history_test_app(); + app.recent_history_request = Some((42, app.display_messages_version, 192)); + assert!(!app.accept_recent_history_response(41)); + assert!(app.recent_history_request.is_some()); + app.replace_display_messages(vec![DisplayMessage::user("rewound transcript")]); + assert!(!app.accept_recent_history_response(42)); + assert_eq!(app.take_pending_compacted_history_load(), None); + assert_eq!(app.display_messages.len(), 1); +} + +#[test] +fn recent_history_full_window_restores_archived_compaction_loading() { + let mut app = recent_history_test_app(); + app.recent_history_request = Some((42, app.display_messages_version, 1000)); + assert!(app.accept_recent_history_response(42)); + app.apply_compacted_history_window( + vec![ + DisplayMessage::system("Earlier conversation compacted - 240 historical messages hidden from the UI. Scroll to the top to load older history. Hidden user prompts: 31."), + DisplayMessage::user("current context prompt"), + ], vec![], 1000, 1000, 0, 31, + ); + assert!(!app.recent_history_window_active()); + assert_eq!(app.compacted_history_lazy_state().total_messages, 240); + assert_eq!(app.compacted_history_lazy_state().remaining_messages, 240); + assert_eq!(app.compacted_hidden_user_prompts(), 31); +} + +#[test] +fn recent_history_page_accepts_unchanged_generation() { + let mut app = recent_history_test_app(); + app.recent_history_request = Some((42, app.display_messages_version, 192)); + assert!(app.accept_recent_history_response(42)); + assert!(app.recent_history_request.is_none()); + // Unsolicited page payloads cannot replace a bounded current-history view. + assert!(!app.accept_recent_history_response(42)); +} + +#[test] +fn recent_history_page_rejects_snapshot_missing_already_visible_rows() { + let mut app = recent_history_test_app(); + let mut stale = vec![DisplayMessage::user("latest prompt")]; + assert!(!app.recent_history_page_contains_visible_tail(&mut stale, 192)); + assert_eq!(app.compacted_history_lazy.pending_request_visible, Some(192)); + assert_eq!(app.display_messages.last().unwrap().content, "latest answer"); + let mut newer = vec![ + DisplayMessage::assistant("older page content"), + DisplayMessage::user("latest prompt"), + DisplayMessage::assistant("latest answer"), + DisplayMessage::assistant("even newer persisted response"), + ]; + assert!(app.recent_history_page_contains_visible_tail(&mut newer, 192)); +} diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs index 6a96f44c5b1..7bda7b6835d 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_03/part_02.rs @@ -755,6 +755,7 @@ fn test_delta_history_rebuilds_transcript_from_advertised_prefix() { count: 2, fingerprint: 0, images_fingerprint: None, + recent_messages: None, }, prefix: Some(prefix), images: Some(vec![crate::session::RenderedImage { diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs index 09b9542cc44..1061359b86b 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs @@ -332,6 +332,8 @@ impl App { display_edit_line_counts: (0, 0), terminal_title: RefCell::new(terminal_title::TerminalTitleState::default()), compacted_history_lazy: CompactedHistoryLazyState::default(), + recent_history_request: None, + recent_history_retry_at: None, pending_history_anchor: None, pending_resize_anchor: None, input: String::new(), @@ -836,6 +838,8 @@ impl App { display_edit_line_counts: (0, 0), terminal_title: RefCell::new(terminal_title::TerminalTitleState::default()), compacted_history_lazy: CompactedHistoryLazyState::default(), + recent_history_request: None, + recent_history_retry_at: None, pending_history_anchor: None, pending_resize_anchor: None, input: String::new(), diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index f33e4fb1419..5d0e4baab6d 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -8,7 +8,7 @@ mod ssh_sessions; use crate::message::ToolCall; -use crate::protocol::{AuthChanged, FeatureToggle, Request, ServerEvent}; +use crate::protocol::{AuthChanged, FeatureToggle, HistoryBase, Request, ServerEvent}; use crate::server; use crate::transport::{Stream, WriteHalf}; use crate::tui::remote_diff::RemoteDiffTracker; @@ -19,6 +19,14 @@ use std::time::{Duration, Instant}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::sync::Mutex; +fn subscribe_history_base(base: Option, ssh_remote: bool) -> Option { + if ssh_remote { + Some(HistoryBase::recent(128)) + } else { + base + } +} + /// Debug events broadcast by local harnesses via debug socket. /// These expose the full internal state for debugging/comparison. #[derive(Debug, Clone, Serialize, Deserialize)] @@ -456,7 +464,7 @@ impl RemoteConnection { target_session_id: resume_target.clone(), client_instance_id: conn.client_instance_id.clone(), client_has_local_history: client_has_local_history && !super::is_ssh_remote(), - history_base: history_base.filter(|_| !super::is_ssh_remote()), + history_base: subscribe_history_base(history_base, super::is_ssh_remote()), allow_session_takeover, crash_on_disconnect: false, continue_on_disconnect: super::is_ssh_remote(), @@ -891,11 +899,16 @@ impl RemoteConnection { } /// Request a wider compacted-history window for the active session. - pub async fn get_compacted_history(&mut self, visible_messages: usize) -> Result { + pub async fn get_compacted_history( + &mut self, + visible_messages: usize, + recent_history: bool, + ) -> Result { let id = self.next_request_id; let request = Request::GetCompactedHistory { id, visible_messages, + recent_history, }; self.next_request_id += 1; self.send_request(request).await?; diff --git a/crates/jcode-tui/src/tui/backend_tests.rs b/crates/jcode-tui/src/tui/backend_tests.rs index 62ffb78c431..642d19a83e9 100644 --- a/crates/jcode-tui/src/tui/backend_tests.rs +++ b/crates/jcode-tui/src/tui/backend_tests.rs @@ -1,6 +1,41 @@ use super::*; use std::time::Duration; +#[test] +fn recent_history_negotiation_preserves_local_verified_prefix() { + let base = HistoryBase { + count: 7, + fingerprint: 123, + images_fingerprint: Some(456), + recent_messages: None, + }; + let local = subscribe_history_base(Some(base), false).unwrap(); + assert_eq!(local.count, 7); + assert_eq!(local.fingerprint, 123); + let remote = subscribe_history_base(Some(base), true).unwrap(); + assert_eq!(remote.count, 0); + assert_eq!(remote.recent_messages, Some(128)); + assert!(subscribe_history_base(None, false).is_none()); + assert_eq!( + subscribe_history_base(None, true).unwrap().recent_messages, + Some(128) + ); +} + +#[tokio::test] +async fn recent_history_page_request_explicitly_selects_normal_transcript() { + let mut remote = RemoteConnection::dummy(); + let id = remote.get_compacted_history(192, true).await.unwrap(); + let peer = remote._dummy_peer.as_ref().unwrap(); + peer.readable().await.unwrap(); + let mut bytes = [0; 256]; + let count = peer.try_read(&mut bytes).unwrap(); + assert!( + matches!(serde_json::from_slice::(&bytes[..count]).unwrap(), + Request::GetCompactedHistory { id: actual, visible_messages: 192, recent_history: true } if actual == id) + ); +} + #[tokio::test] async fn slow_usage_hydration_does_not_block_subscribe_or_async_runtime() { let (started_tx, started_rx) = tokio::sync::oneshot::channel(); diff --git a/scripts/code_size_budget.json b/scripts/code_size_budget.json index a1df3982a48..c65229641f0 100644 --- a/scripts/code_size_budget.json +++ b/scripts/code_size_budget.json @@ -1,126 +1,117 @@ { "threshold_loc": 1200, "tracked_files": { - "crates/jcode-app-core/src/agent.rs": 1399, - "crates/jcode-app-core/src/agent/response_recovery.rs": 1274, - "crates/jcode-app-core/src/agent/turn_execution.rs": 1647, - "crates/jcode-app-core/src/agent/turn_loops.rs": 1364, - "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": 1916, + "crates/jcode-app-core/src/agent.rs": 1375, + "crates/jcode-app-core/src/agent/turn_execution.rs": 1628, + "crates/jcode-app-core/src/agent/turn_loops.rs": 1355, + "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": 1851, "crates/jcode-app-core/src/overnight.rs": 1275, - "crates/jcode-app-core/src/server.rs": 2653, - "crates/jcode-app-core/src/server/client_actions.rs": 1350, - "crates/jcode-app-core/src/server/client_lifecycle.rs": 3974, - "crates/jcode-app-core/src/server/client_session.rs": 2112, - "crates/jcode-app-core/src/server/client_state.rs": 1365, - "crates/jcode-app-core/src/server/comm_control.rs": 2640, - "crates/jcode-app-core/src/server/comm_session.rs": 1513, + "crates/jcode-app-core/src/server.rs": 2543, + "crates/jcode-app-core/src/server/client_actions.rs": 1335, + "crates/jcode-app-core/src/server/client_lifecycle.rs": 3481, + "crates/jcode-app-core/src/server/client_session.rs": 2072, + "crates/jcode-app-core/src/server/client_state.rs": 1488, + "crates/jcode-app-core/src/server/comm_control.rs": 2581, + "crates/jcode-app-core/src/server/comm_session.rs": 1314, "crates/jcode-app-core/src/server/debug_server_state.rs": 1269, - "crates/jcode-app-core/src/server/jade_relay.rs": 1429, - "crates/jcode-app-core/src/server/provider_control.rs": 1530, - "crates/jcode-app-core/src/server/swarm.rs": 3518, - "crates/jcode-app-core/src/tool/bash.rs": 1590, - "crates/jcode-app-core/src/tool/browser.rs": 1252, - "crates/jcode-app-core/src/tool/communicate.rs": 3159, + "crates/jcode-app-core/src/server/jade_relay.rs": 1338, + "crates/jcode-app-core/src/server/provider_control.rs": 1401, + "crates/jcode-app-core/src/server/swarm.rs": 2123, + "crates/jcode-app-core/src/tool/bash.rs": 1502, + "crates/jcode-app-core/src/tool/browser.rs": 1203, + "crates/jcode-app-core/src/tool/communicate.rs": 3149, "crates/jcode-app-core/src/tool/discover.rs": 2982, "crates/jcode-app-core/src/tool/mod.rs": 1924, - "crates/jcode-app-core/src/tool/session_search.rs": 1892, - "crates/jcode-app-core/src/tool/todo.rs": 2853, - "crates/jcode-app-core/src/update.rs": 1308, + "crates/jcode-app-core/src/tool/session_search.rs": 1869, + "crates/jcode-app-core/src/tool/todo.rs": 1648, + "crates/jcode-app-core/src/update.rs": 1293, "crates/jcode-base/src/auth/lifecycle.rs": 2882, - "crates/jcode-base/src/auth/mod.rs": 1596, - "crates/jcode-base/src/auth/oauth.rs": 1636, + "crates/jcode-base/src/auth/mod.rs": 1585, + "crates/jcode-base/src/auth/oauth.rs": 1469, "crates/jcode-base/src/auth/transfer.rs": 2185, - "crates/jcode-base/src/background.rs": 1713, + "crates/jcode-base/src/background.rs": 1657, "crates/jcode-base/src/browser.rs": 1545, - "crates/jcode-base/src/compaction.rs": 1833, + "crates/jcode-base/src/compaction.rs": 1832, "crates/jcode-base/src/gmail.rs": 1537, "crates/jcode-base/src/import.rs": 1502, "crates/jcode-base/src/jev.rs": 1863, "crates/jcode-base/src/memory.rs": 1572, - "crates/jcode-base/src/provider/catalog_routes.rs": 1596, - "crates/jcode-base/src/provider/mod.rs": 3241, - "crates/jcode-base/src/provider/models.rs": 1363, + "crates/jcode-base/src/provider/mod.rs": 3130, + "crates/jcode-base/src/provider/models.rs": 1297, "crates/jcode-base/src/provider_catalog.rs": 1339, - "crates/jcode-base/src/session.rs": 1823, - "crates/jcode-base/src/sidecar.rs": 1750, - "crates/jcode-base/src/skill.rs": 1593, - "crates/jcode-base/src/subscription_api.rs": 1206, - "crates/jcode-compaction-core/src/lib.rs": 1423, - "crates/jcode-config-types/src/lib.rs": 1623, - "crates/jcode-harness-api-server/src/translate.rs": 3620, + "crates/jcode-base/src/session.rs": 1802, + "crates/jcode-base/src/sidecar.rs": 1754, + "crates/jcode-config-types/src/lib.rs": 1576, + "crates/jcode-harness-api-server/src/translate.rs": 3518, "crates/jcode-import-core/src/lib.rs": 1645, - "crates/jcode-provider-anthropic-runtime/src/lib.rs": 2788, - "crates/jcode-provider-anthropic/src/lib.rs": 1246, - "crates/jcode-provider-bedrock/src/lib.rs": 1612, - "crates/jcode-provider-copilot-runtime/src/lib.rs": 1201, - "crates/jcode-provider-core/src/lib.rs": 1625, + "crates/jcode-provider-anthropic-runtime/src/lib.rs": 2692, + "crates/jcode-provider-anthropic/src/lib.rs": 1270, + "crates/jcode-provider-bedrock/src/lib.rs": 1550, + "crates/jcode-provider-core/src/lib.rs": 1624, "crates/jcode-provider-cursor-runtime/src/agent_transport.rs": 1873, "crates/jcode-provider-cursor-runtime/src/wire.rs": 1431, "crates/jcode-provider-doctor/src/lifecycle_driver.rs": 1980, "crates/jcode-provider-doctor/src/live_provider_probes.rs": 1939, "crates/jcode-provider-doctor/src/provider_e2e.rs": 2727, - "crates/jcode-provider-gemini-runtime/src/lib.rs": 1266, "crates/jcode-provider-metadata/src/catalog.rs": 1351, - "crates/jcode-provider-openai-runtime/src/lib.rs": 1606, - "crates/jcode-provider-openai-runtime/src/openai_provider_impl.rs": 1328, - "crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs": 2109, - "crates/jcode-provider-openrouter-runtime/src/lib.rs": 2931, + "crates/jcode-provider-openai-runtime/src/lib.rs": 1598, + "crates/jcode-provider-openai-runtime/src/openai_provider_impl.rs": 1310, + "crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs": 2024, + "crates/jcode-provider-openrouter-runtime/src/lib.rs": 2890, "crates/jcode-render-core/src/math.rs": 1235, - "crates/jcode-sdk/src/client.rs": 1938, + "crates/jcode-sdk/src/client.rs": 1824, "crates/jcode-setup-hints/src/lib.rs": 2660, "crates/jcode-telemetry-core/src/lib.rs": 2775, "crates/jcode-terminal-launch/src/lib.rs": 1700, "crates/jcode-tui-core/src/keybind.rs": 1235, - "crates/jcode-tui-markdown/src/lib.rs": 1230, - "crates/jcode-tui-mermaid/src/lib.rs": 1673, + "crates/jcode-tui-markdown/src/lib.rs": 1226, + "crates/jcode-tui-mermaid/src/lib.rs": 1635, "crates/jcode-tui-mermaid/src/mermaid_cache_render.rs": 1561, "crates/jcode-tui-mermaid/src/mermaid_viewport.rs": 2031, - "crates/jcode-tui-render/src/swarm_gallery.rs": 2057, - "crates/jcode-tui/src/tui/app.rs": 2923, + "crates/jcode-tui-render/src/swarm_gallery.rs": 2026, + "crates/jcode-tui/src/tui/app.rs": 2894, "crates/jcode-tui/src/tui/app/auth.rs": 3475, - "crates/jcode-tui/src/tui/app/auth_account_commands.rs": 1213, - "crates/jcode-tui/src/tui/app/auth_account_picker.rs": 1305, - "crates/jcode-tui/src/tui/app/commands.rs": 4042, - "crates/jcode-tui/src/tui/app/commands_cloud.rs": 1268, + "crates/jcode-tui/src/tui/app/auth_account_picker.rs": 1236, + "crates/jcode-tui/src/tui/app/commands.rs": 3838, + "crates/jcode-tui/src/tui/app/commands_cloud.rs": 1291, "crates/jcode-tui/src/tui/app/debug_bench.rs": 1286, - "crates/jcode-tui/src/tui/app/debug_cmds.rs": 1230, - "crates/jcode-tui/src/tui/app/helpers.rs": 1810, + "crates/jcode-tui/src/tui/app/helpers.rs": 1805, "crates/jcode-tui/src/tui/app/hotkey_feedback.rs": 1227, - "crates/jcode-tui/src/tui/app/inline_interactive.rs": 4208, - "crates/jcode-tui/src/tui/app/input.rs": 4324, - "crates/jcode-tui/src/tui/app/model_context.rs": 1986, - "crates/jcode-tui/src/tui/app/navigation.rs": 1955, + "crates/jcode-tui/src/tui/app/inline_interactive.rs": 4231, + "crates/jcode-tui/src/tui/app/input.rs": 4309, + "crates/jcode-tui/src/tui/app/model_context.rs": 2059, + "crates/jcode-tui/src/tui/app/navigation.rs": 1939, "crates/jcode-tui/src/tui/app/onboarding_flow_control.rs": 1821, - "crates/jcode-tui/src/tui/app/remote.rs": 2278, - "crates/jcode-tui/src/tui/app/remote/key_handling.rs": 2762, - "crates/jcode-tui/src/tui/app/remote/server_events.rs": 3421, - "crates/jcode-tui/src/tui/app/state_ui.rs": 2338, - "crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs": 1804, - "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1644, - "crates/jcode-tui/src/tui/app/tui_state.rs": 2909, - "crates/jcode-tui/src/tui/app/turn.rs": 1714, - "crates/jcode-tui/src/tui/backend.rs": 1618, - "crates/jcode-tui/src/tui/info_widget.rs": 2347, - "crates/jcode-tui/src/tui/mod.rs": 2523, - "crates/jcode-tui/src/tui/session_picker.rs": 2695, - "crates/jcode-tui/src/tui/session_picker/loading.rs": 3102, - "crates/jcode-tui/src/tui/ui.rs": 3795, - "crates/jcode-tui/src/tui/ui_frame_metrics.rs": 1434, - "crates/jcode-tui/src/tui/ui_header.rs": 1729, - "crates/jcode-tui/src/tui/ui_inline_image.rs": 1797, - "crates/jcode-tui/src/tui/ui_input.rs": 3026, - "crates/jcode-tui/src/tui/ui_messages.rs": 4633, - "crates/jcode-tui/src/tui/ui_pinned.rs": 1269, - "crates/jcode-tui/src/tui/ui_prepare.rs": 2736, - "crates/jcode-tui/src/tui/ui_tools.rs": 1687, - "crates/jcode-tui/src/tui/ui_viewport.rs": 1664, + "crates/jcode-tui/src/tui/app/remote.rs": 2283, + "crates/jcode-tui/src/tui/app/remote/key_handling.rs": 2080, + "crates/jcode-tui/src/tui/app/remote/server_events.rs": 3435, + "crates/jcode-tui/src/tui/app/state_ui.rs": 2313, + "crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs": 1788, + "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1650, + "crates/jcode-tui/src/tui/app/tui_state.rs": 2797, + "crates/jcode-tui/src/tui/app/turn.rs": 1702, + "crates/jcode-tui/src/tui/backend.rs": 1611, + "crates/jcode-tui/src/tui/info_widget.rs": 2236, + "crates/jcode-tui/src/tui/mod.rs": 2478, + "crates/jcode-tui/src/tui/session_picker.rs": 2591, + "crates/jcode-tui/src/tui/session_picker/loading.rs": 3013, + "crates/jcode-tui/src/tui/ui.rs": 3866, + "crates/jcode-tui/src/tui/ui_frame_metrics.rs": 1479, + "crates/jcode-tui/src/tui/ui_input.rs": 2494, + "crates/jcode-tui/src/tui/ui_messages.rs": 4627, + "crates/jcode-tui/src/tui/ui_pinned.rs": 1233, + "crates/jcode-tui/src/tui/ui_prepare.rs": 2764, + "crates/jcode-tui/src/tui/ui_tools.rs": 1643, + "crates/jcode-tui/src/tui/ui_viewport.rs": 1517, "src/bin/memory_recall_bench.rs": 2667, "src/bin/tui_bench.rs": 1765, - "src/cli/acp.rs": 2319, - "src/cli/cloud_move.rs": 2839, - "src/cli/commands.rs": 3632, - "src/cli/dispatch.rs": 1552, - "src/cli/provider_init.rs": 1955 + "src/cli/acp.rs": 2053, + "src/cli/args.rs": 1301, + "src/cli/cloud_move.rs": 2788, + "src/cli/commands.rs": 3584, + "src/cli/dispatch.rs": 1533, + "src/cli/provider_init.rs": 1955, + "src/cli/ssh_transport.rs": 1295 }, "version": 1 } diff --git a/scripts/panic_budget.json b/scripts/panic_budget.json index b047640f735..856035a09a4 100644 --- a/scripts/panic_budget.json +++ b/scripts/panic_budget.json @@ -1,9 +1,7 @@ { - "total": 174, + "total": 147, "tracked_files": { - "crates/jcode-app-core/build.rs": 4, "crates/jcode-app-core/src/agent/provider.rs": 1, - "crates/jcode-app-core/src/agent/tool_markup_recovery.rs": 2, "crates/jcode-app-core/src/session_launch.rs": 1, "crates/jcode-app-core/src/tool/bash.rs": 2, "crates/jcode-app-core/src/tool/browser.rs": 2, @@ -11,26 +9,20 @@ "crates/jcode-app-core/src/tool/browser_jev.rs": 1, "crates/jcode-app-core/src/tool/calendar.rs": 1, "crates/jcode-app-core/src/tool/compile_remote.rs": 2, - "crates/jcode-app-core/src/tool/computer/win.rs": 3, "crates/jcode-app-core/src/tool/discover.rs": 1, "crates/jcode-app-core/src/tool/mod.rs": 1, "crates/jcode-app-core/src/tool/sdk.rs": 1, - "crates/jcode-app-core/src/tool/todo.rs": 1, "crates/jcode-base/src/auth/account_store.rs": 1, - "crates/jcode-base/src/auth/oauth.rs": 3, "crates/jcode-base/src/hooks.rs": 1, "crates/jcode-base/src/mcp/manager.rs": 2, "crates/jcode-base/src/memory_jev.rs": 1, - "crates/jcode-base/src/provider_activity_oauth.rs": 3, "crates/jcode-base/src/subscription_notice.rs": 1, "crates/jcode-base/src/voice_intent.rs": 5, "crates/jcode-harness-api-server/src/translate.rs": 2, "crates/jcode-harness-api-server/src/translate/session_files.rs": 1, "crates/jcode-harness-api/examples/harness_repl.rs": 15, - "crates/jcode-provider-anthropic/src/wedge_fixture_check.rs": 3, "crates/jcode-provider-core/src/model_names.rs": 1, "crates/jcode-provider-cursor-runtime/src/wire.rs": 1, - "crates/jcode-provider-doctor/src/lifecycle_driver.rs": 2, "crates/jcode-provider-openai/src/stream.rs": 3, "crates/jcode-render-core/src/math.rs": 4, "crates/jcode-render-core/src/preprocess.rs": 2, @@ -53,14 +45,12 @@ "crates/jcode-tui/src/tui/app/auth_remote/picker.rs": 1, "crates/jcode-tui/src/tui/app/commands_cloud.rs": 2, "crates/jcode-tui/src/tui/app/usage_reset.rs": 1, - "crates/jcode-tui/src/tui/session_picker.rs": 2, "crates/jcode-tui/src/tui/ui_inline_interactive.rs": 1, "src/bin/memory_recall_bench.rs": 14, "src/cli/acp.rs": 2, "src/cli/cloud_managed.rs": 1, - "src/cli/cloud_move.rs": 1, "src/cli/commands/menubar.rs": 1, - "src/cli/login/device_login.rs": 9, + "src/cli/login/device_login.rs": 6, "src/cli/ssh.rs": 1, "src/cli/tui_launch.rs": 1 }, diff --git a/scripts/swallowed_error_budget.json b/scripts/swallowed_error_budget.json index ad832cfc8e1..faf47d1299a 100644 --- a/scripts/swallowed_error_budget.json +++ b/scripts/swallowed_error_budget.json @@ -1,9 +1,9 @@ { - "total": 3961, + "total": 3878, "totals_by_pattern": { - "dot_ok": 1553, - "let_underscore": 1428, - "unwrap_or_default": 980 + "dot_ok": 1529, + "let_underscore": 1409, + "unwrap_or_default": 940 }, "tracked_files": { "crates/jcode-app-core/src/agent.rs": { @@ -46,11 +46,6 @@ "let_underscore": 1, "unwrap_or_default": 0 }, - "crates/jcode-app-core/src/agent/tool_markup_recovery.rs": { - "dot_ok": 0, - "let_underscore": 0, - "unwrap_or_default": 1 - }, "crates/jcode-app-core/src/agent/tools.rs": { "dot_ok": 0, "let_underscore": 0, @@ -68,8 +63,8 @@ }, "crates/jcode-app-core/src/agent/turn_streaming_mpsc.rs": { "dot_ok": 0, - "let_underscore": 64, - "unwrap_or_default": 3 + "let_underscore": 59, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/agent/utils.rs": { "dot_ok": 1, @@ -88,7 +83,7 @@ }, "crates/jcode-app-core/src/ambient/persistence.rs": { "dot_ok": 0, - "let_underscore": 6, + "let_underscore": 3, "unwrap_or_default": 1 }, "crates/jcode-app-core/src/ambient/runner.rs": { @@ -173,8 +168,8 @@ }, "crates/jcode-app-core/src/server/client_actions.rs": { "dot_ok": 2, - "let_underscore": 48, - "unwrap_or_default": 1 + "let_underscore": 49, + "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/client_comm_channels.rs": { "dot_ok": 0, @@ -198,8 +193,8 @@ }, "crates/jcode-app-core/src/server/client_lifecycle.rs": { "dot_ok": 0, - "let_underscore": 32, - "unwrap_or_default": 1 + "let_underscore": 31, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/server/client_lifecycle_logging.rs": { "dot_ok": 1, @@ -213,22 +208,22 @@ }, "crates/jcode-app-core/src/server/client_processing.rs": { "dot_ok": 0, - "let_underscore": 21, - "unwrap_or_default": 0 + "let_underscore": 19, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/server/client_session.rs": { - "dot_ok": 8, - "let_underscore": 16, + "dot_ok": 5, + "let_underscore": 18, "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/client_state.rs": { - "dot_ok": 2, - "let_underscore": 1, - "unwrap_or_default": 4 + "dot_ok": 4, + "let_underscore": 0, + "unwrap_or_default": 6 }, "crates/jcode-app-core/src/server/comm_await.rs": { "dot_ok": 0, - "let_underscore": 9, + "let_underscore": 8, "unwrap_or_default": 5 }, "crates/jcode-app-core/src/server/comm_control.rs": { @@ -243,8 +238,8 @@ }, "crates/jcode-app-core/src/server/comm_session.rs": { "dot_ok": 4, - "let_underscore": 12, - "unwrap_or_default": 9 + "let_underscore": 10, + "unwrap_or_default": 10 }, "crates/jcode-app-core/src/server/comm_sync.rs": { "dot_ok": 0, @@ -303,8 +298,8 @@ }, "crates/jcode-app-core/src/server/jade_relay.rs": { "dot_ok": 2, - "let_underscore": 8, - "unwrap_or_default": 3 + "let_underscore": 9, + "unwrap_or_default": 2 }, "crates/jcode-app-core/src/server/lifecycle.rs": { "dot_ok": 8, @@ -317,9 +312,9 @@ "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/provider_control.rs": { - "dot_ok": 1, - "let_underscore": 29, - "unwrap_or_default": 2 + "dot_ok": 0, + "let_underscore": 31, + "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/reload.rs": { "dot_ok": 1, @@ -328,7 +323,7 @@ }, "crates/jcode-app-core/src/server/reload_recovery.rs": { "dot_ok": 3, - "let_underscore": 1, + "let_underscore": 0, "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/reload_state.rs": { @@ -347,8 +342,8 @@ "unwrap_or_default": 1 }, "crates/jcode-app-core/src/server/state.rs": { - "dot_ok": 3, - "let_underscore": 2, + "dot_ok": 2, + "let_underscore": 3, "unwrap_or_default": 1 }, "crates/jcode-app-core/src/server/swarm.rs": { @@ -682,9 +677,9 @@ "unwrap_or_default": 1 }, "crates/jcode-base/src/auth/account_pool.rs": { - "dot_ok": 2, + "dot_ok": 4, "let_underscore": 0, - "unwrap_or_default": 3 + "unwrap_or_default": 1 }, "crates/jcode-base/src/auth/account_store.rs": { "dot_ok": 1, @@ -763,8 +758,8 @@ }, "crates/jcode-base/src/auth/oauth.rs": { "dot_ok": 4, - "let_underscore": 1, - "unwrap_or_default": 3 + "let_underscore": 2, + "unwrap_or_default": 2 }, "crates/jcode-base/src/auth/oauth_callback_io.rs": { "dot_ok": 0, @@ -802,7 +797,7 @@ "unwrap_or_default": 1 }, "crates/jcode-base/src/background.rs": { - "dot_ok": 19, + "dot_ok": 18, "let_underscore": 18, "unwrap_or_default": 12 }, @@ -827,8 +822,8 @@ "unwrap_or_default": 0 }, "crates/jcode-base/src/bus.rs": { - "dot_ok": 2, - "let_underscore": 2, + "dot_ok": 3, + "let_underscore": 1, "unwrap_or_default": 0 }, "crates/jcode-base/src/claude_live.rs": { @@ -1016,6 +1011,11 @@ "let_underscore": 0, "unwrap_or_default": 0 }, + "crates/jcode-base/src/mobile_sync.rs": { + "dot_ok": 2, + "let_underscore": 0, + "unwrap_or_default": 0 + }, "crates/jcode-base/src/model_pricing.rs": { "dot_ok": 2, "let_underscore": 1, @@ -1088,7 +1088,7 @@ }, "crates/jcode-base/src/provider/models.rs": { "dot_ok": 3, - "let_underscore": 2, + "let_underscore": 0, "unwrap_or_default": 8 }, "crates/jcode-base/src/provider/openai.rs": { @@ -1136,21 +1136,11 @@ "let_underscore": 0, "unwrap_or_default": 0 }, - "crates/jcode-base/src/recent_session_index.rs": { - "dot_ok": 0, - "let_underscore": 2, - "unwrap_or_default": 0 - }, "crates/jcode-base/src/registry.rs": { "dot_ok": 3, "let_underscore": 0, "unwrap_or_default": 0 }, - "crates/jcode-base/src/runtime_activity.rs": { - "dot_ok": 2, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-base/src/runtime_memory_log.rs": { "dot_ok": 1, "let_underscore": 1, @@ -1262,7 +1252,7 @@ "unwrap_or_default": 0 }, "crates/jcode-base/src/todo.rs": { - "dot_ok": 3, + "dot_ok": 1, "let_underscore": 0, "unwrap_or_default": 3 }, @@ -1331,23 +1321,18 @@ "let_underscore": 0, "unwrap_or_default": 6 }, - "crates/jcode-base/src/usage/remote_accounts.rs": { - "dot_ok": 2, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-base/src/usage/startup_snapshot.rs": { "dot_ok": 11, "let_underscore": 0, "unwrap_or_default": 0 }, "crates/jcode-build-meta/build.rs": { - "dot_ok": 12, + "dot_ok": 9, "let_underscore": 0, "unwrap_or_default": 2 }, "crates/jcode-build-meta/src/lib.rs": { - "dot_ok": 5, + "dot_ok": 2, "let_underscore": 0, "unwrap_or_default": 0 }, @@ -1386,11 +1371,6 @@ "let_underscore": 1, "unwrap_or_default": 2 }, - "crates/jcode-config-types/src/lib.rs": { - "dot_ok": 0, - "let_underscore": 0, - "unwrap_or_default": 1 - }, "crates/jcode-config-types/src/serde_lenient.rs": { "dot_ok": 1, "let_underscore": 0, @@ -1427,9 +1407,9 @@ "unwrap_or_default": 3 }, "crates/jcode-harness-api-server/src/translate.rs": { - "dot_ok": 20, + "dot_ok": 24, "let_underscore": 5, - "unwrap_or_default": 46 + "unwrap_or_default": 23 }, "crates/jcode-harness-api-server/src/translate/session_files.rs": { "dot_ok": 4, @@ -1523,7 +1503,7 @@ }, "crates/jcode-provider-anthropic-runtime/src/lib.rs": { "dot_ok": 9, - "let_underscore": 15, + "let_underscore": 14, "unwrap_or_default": 1 }, "crates/jcode-provider-anthropic-runtime/src/native_web_search.rs": { @@ -1532,8 +1512,8 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-antigravity-runtime/src/lib.rs": { - "dot_ok": 2, - "let_underscore": 21, + "dot_ok": 3, + "let_underscore": 20, "unwrap_or_default": 3 }, "crates/jcode-provider-antigravity/src/lib.rs": { @@ -1542,12 +1522,12 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-bedrock/src/lib.rs": { - "dot_ok": 20, + "dot_ok": 18, "let_underscore": 9, "unwrap_or_default": 5 }, "crates/jcode-provider-copilot-runtime/src/lib.rs": { - "dot_ok": 5, + "dot_ok": 3, "let_underscore": 20, "unwrap_or_default": 2 }, @@ -1626,11 +1606,6 @@ "let_underscore": 0, "unwrap_or_default": 1 }, - "crates/jcode-provider-doctor/src/lifecycle_driver.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-provider-doctor/src/live_provider_probes.rs": { "dot_ok": 2, "let_underscore": 0, @@ -1647,9 +1622,9 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-gemini-runtime/src/lib.rs": { - "dot_ok": 4, + "dot_ok": 2, "let_underscore": 24, - "unwrap_or_default": 6 + "unwrap_or_default": 5 }, "crates/jcode-provider-gemini/src/lib.rs": { "dot_ok": 2, @@ -1667,7 +1642,7 @@ "unwrap_or_default": 8 }, "crates/jcode-provider-openai-runtime/src/lib.rs": { - "dot_ok": 4, + "dot_ok": 2, "let_underscore": 2, "unwrap_or_default": 4 }, @@ -1687,7 +1662,7 @@ "unwrap_or_default": 0 }, "crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs": { - "dot_ok": 3, + "dot_ok": 2, "let_underscore": 7, "unwrap_or_default": 1 }, @@ -1707,7 +1682,7 @@ "unwrap_or_default": 1 }, "crates/jcode-provider-openai/src/stream.rs": { - "dot_ok": 3, + "dot_ok": 2, "let_underscore": 0, "unwrap_or_default": 6 }, @@ -1946,11 +1921,6 @@ "let_underscore": 0, "unwrap_or_default": 0 }, - "crates/jcode-todo-policy/src/followup.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-todo-policy/src/lib.rs": { "dot_ok": 0, "let_underscore": 0, @@ -1962,9 +1932,9 @@ "unwrap_or_default": 1 }, "crates/jcode-todo-policy/src/store.rs": { - "dot_ok": 6, - "let_underscore": 0, - "unwrap_or_default": 1 + "dot_ok": 0, + "let_underscore": 1, + "unwrap_or_default": 0 }, "crates/jcode-transport/src/unix.rs": { "dot_ok": 0, @@ -2072,9 +2042,9 @@ "unwrap_or_default": 2 }, "crates/jcode-tui-mermaid/src/mermaid_runtime.rs": { - "dot_ok": 22, + "dot_ok": 23, "let_underscore": 2, - "unwrap_or_default": 5 + "unwrap_or_default": 3 }, "crates/jcode-tui-mermaid/src/mermaid_svg.rs": { "dot_ok": 19, @@ -2178,19 +2148,14 @@ }, "crates/jcode-tui/src/tui/app/commands_cloud.rs": { "dot_ok": 0, - "let_underscore": 6, - "unwrap_or_default": 5 + "let_underscore": 5, + "unwrap_or_default": 6 }, "crates/jcode-tui/src/tui/app/commands_desktop.rs": { "dot_ok": 1, "let_underscore": 2, "unwrap_or_default": 0 }, - "crates/jcode-tui/src/tui/app/commands_dispatch.rs": { - "dot_ok": 0, - "let_underscore": 0, - "unwrap_or_default": 1 - }, "crates/jcode-tui/src/tui/app/commands_improve.rs": { "dot_ok": 0, "let_underscore": 2, @@ -2259,7 +2224,7 @@ "crates/jcode-tui/src/tui/app/helpers.rs": { "dot_ok": 17, "let_underscore": 4, - "unwrap_or_default": 6 + "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/app/helpers/clipboard_helper.rs": { "dot_ok": 0, @@ -2273,7 +2238,7 @@ }, "crates/jcode-tui/src/tui/app/inline_interactive.rs": { "dot_ok": 6, - "let_underscore": 12, + "let_underscore": 11, "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/app/inline_interactive/helpers.rs": { @@ -2288,8 +2253,8 @@ }, "crates/jcode-tui/src/tui/app/input.rs": { "dot_ok": 12, - "let_underscore": 2, - "unwrap_or_default": 5 + "let_underscore": 3, + "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/app/local.rs": { "dot_ok": 0, @@ -2328,7 +2293,7 @@ }, "crates/jcode-tui/src/tui/app/remote.rs": { "dot_ok": 0, - "let_underscore": 11, + "let_underscore": 9, "unwrap_or_default": 0 }, "crates/jcode-tui/src/tui/app/remote/input_dispatch.rs": { @@ -2354,7 +2319,7 @@ "crates/jcode-tui/src/tui/app/remote/server_events.rs": { "dot_ok": 7, "let_underscore": 1, - "unwrap_or_default": 4 + "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/app/remote/server_events/history_fingerprint.rs": { "dot_ok": 1, @@ -2371,11 +2336,6 @@ "let_underscore": 0, "unwrap_or_default": 1 }, - "crates/jcode-tui/src/tui/app/run_shell.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-tui/src/tui/app/runtime_memory.rs": { "dot_ok": 0, "let_underscore": 1, @@ -2457,9 +2417,9 @@ "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/app/tui_state.rs": { - "dot_ok": 5, + "dot_ok": 7, "let_underscore": 0, - "unwrap_or_default": 9 + "unwrap_or_default": 7 }, "crates/jcode-tui/src/tui/app/turn.rs": { "dot_ok": 0, @@ -2561,11 +2521,6 @@ "let_underscore": 1, "unwrap_or_default": 1 }, - "crates/jcode-tui/src/tui/redraw_schedule.rs": { - "dot_ok": 1, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-tui/src/tui/remote_diff.rs": { "dot_ok": 1, "let_underscore": 0, @@ -2578,8 +2533,13 @@ }, "crates/jcode-tui/src/tui/session_picker.rs": { "dot_ok": 6, - "let_underscore": 5, - "unwrap_or_default": 6 + "let_underscore": 4, + "unwrap_or_default": 7 + }, + "crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs": { + "dot_ok": 0, + "let_underscore": 0, + "unwrap_or_default": 1 }, "crates/jcode-tui/src/tui/session_picker/filter.rs": { "dot_ok": 0, @@ -2587,9 +2547,14 @@ "unwrap_or_default": 4 }, "crates/jcode-tui/src/tui/session_picker/loading.rs": { - "dot_ok": 28, - "let_underscore": 1, - "unwrap_or_default": 18 + "dot_ok": 27, + "let_underscore": 4, + "unwrap_or_default": 19 + }, + "crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs": { + "dot_ok": 3, + "let_underscore": 0, + "unwrap_or_default": 0 }, "crates/jcode-tui/src/tui/session_picker/loading/pi_sessions.rs": { "dot_ok": 5, @@ -2601,6 +2566,11 @@ "let_underscore": 0, "unwrap_or_default": 1 }, + "crates/jcode-tui/src/tui/stable_status.rs": { + "dot_ok": 1, + "let_underscore": 0, + "unwrap_or_default": 0 + }, "crates/jcode-tui/src/tui/terminal_setup.rs": { "dot_ok": 0, "let_underscore": 0, @@ -2652,9 +2622,9 @@ "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/ui_header/account_router.rs": { - "dot_ok": 1, + "dot_ok": 2, "let_underscore": 1, - "unwrap_or_default": 2 + "unwrap_or_default": 1 }, "crates/jcode-tui/src/tui/ui_inline_image.rs": { "dot_ok": 6, @@ -2667,7 +2637,7 @@ "unwrap_or_default": 2 }, "crates/jcode-tui/src/tui/ui_input.rs": { - "dot_ok": 3, + "dot_ok": 2, "let_underscore": 0, "unwrap_or_default": 2 }, @@ -2707,9 +2677,9 @@ "unwrap_or_default": 3 }, "crates/jcode-tui/src/tui/ui_tools.rs": { - "dot_ok": 1, + "dot_ok": 0, "let_underscore": 0, - "unwrap_or_default": 12 + "unwrap_or_default": 13 }, "crates/jcode-tui/src/tui/ui_tools/batch.rs": { "dot_ok": 1, @@ -2731,11 +2701,6 @@ "let_underscore": 2, "unwrap_or_default": 1 }, - "crates/jcode-update-core/src/lib.rs": { - "dot_ok": 3, - "let_underscore": 0, - "unwrap_or_default": 0 - }, "crates/jcode-usage-types/src/lib.rs": { "dot_ok": 0, "let_underscore": 1, @@ -2762,14 +2727,19 @@ "unwrap_or_default": 0 }, "src/cli/cloud_move.rs": { - "dot_ok": 27, - "let_underscore": 18, - "unwrap_or_default": 8 + "dot_ok": 26, + "let_underscore": 14, + "unwrap_or_default": 7 + }, + "src/cli/cloud_move/transport.rs": { + "dot_ok": 1, + "let_underscore": 1, + "unwrap_or_default": 0 }, "src/cli/commands.rs": { "dot_ok": 13, - "let_underscore": 3, - "unwrap_or_default": 9 + "let_underscore": 2, + "unwrap_or_default": 5 }, "src/cli/commands/menubar.rs": { "dot_ok": 3, diff --git a/scripts/test_size_budget.json b/scripts/test_size_budget.json index 601e8f6bc33..8fc129c283e 100644 --- a/scripts/test_size_budget.json +++ b/scripts/test_size_budget.json @@ -1,55 +1,55 @@ { "threshold_loc": 1200, "tracked_files": { - "crates/jcode-app-core/src/agent_tests.rs": 3253, - "crates/jcode-app-core/src/server/client_lifecycle_tests.rs": 1939, - "crates/jcode-app-core/src/server/comm_session_tests.rs": 1332, - "crates/jcode-app-core/src/server/provider_control_tests.rs": 1352, - "crates/jcode-app-core/src/tool/bash_tests.rs": 1259, - "crates/jcode-app-core/src/tool/communicate_tests.rs": 1481, - "crates/jcode-app-core/src/tool/selfdev/tests.rs": 1460, + "crates/jcode-app-core/src/agent_tests.rs": 3195, + "crates/jcode-app-core/src/server/client_lifecycle_tests.rs": 1658, + "crates/jcode-app-core/src/server/comm_session_tests.rs": 1306, + "crates/jcode-app-core/src/server/provider_control_tests.rs": 1310, + "crates/jcode-app-core/src/tool/bash_tests.rs": 1245, + "crates/jcode-app-core/src/tool/browser_fast_tests.rs": 1286, + "crates/jcode-app-core/src/tool/selfdev/tests.rs": 1439, "crates/jcode-app-core/src/tool/tests.rs": 1976, - "crates/jcode-base/src/compaction_tests.rs": 1271, - "crates/jcode-base/src/config_tests.rs": 1795, + "crates/jcode-base/src/auth/tests.rs": 1206, + "crates/jcode-base/src/compaction_tests.rs": 1269, + "crates/jcode-base/src/config_tests.rs": 1760, "crates/jcode-base/src/live_tests.rs": 3087, - "crates/jcode-base/src/provider/tests.rs": 1335, + "crates/jcode-base/src/provider/tests.rs": 1276, "crates/jcode-base/src/provider/tests/model_resolution.rs": 2248, "crates/jcode-base/src/provider_catalog_tests.rs": 1409, - "crates/jcode-base/src/session_tests/cases.rs": 3267, - "crates/jcode-base/src/usage/tests.rs": 1317, - "crates/jcode-harness-api-server/src/translate_tests.rs": 4199, - "crates/jcode-provider-anthropic-runtime/src/anthropic_tests.rs": 2029, - "crates/jcode-provider-openrouter-runtime/src/openrouter_tests.rs": 3568, - "crates/jcode-sdk/tests/client_behavior.rs": 1432, + "crates/jcode-base/src/session_tests/cases.rs": 3298, + "crates/jcode-base/src/usage/tests.rs": 1289, + "crates/jcode-harness-api-server/src/translate_tests.rs": 4073, + "crates/jcode-provider-anthropic-runtime/src/anthropic_tests.rs": 2072, + "crates/jcode-provider-openrouter-runtime/src/openrouter_tests.rs": 3544, + "crates/jcode-sdk/tests/client_behavior.rs": 1244, "crates/jcode-telemetry-core/src/tests.rs": 1836, - "crates/jcode-tui/src/tui/app/remote_tests.rs": 1441, - "crates/jcode-tui/src/tui/app/tests.rs": 2030, - "crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs": 1604, - "crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs": 1326, + "crates/jcode-tui/src/tui/app/commands_cloud_tests.rs": 1315, + "crates/jcode-tui/src/tui/app/remote_tests.rs": 1329, + "crates/jcode-tui/src/tui/app/tests.rs": 1943, + "crates/jcode-tui/src/tui/app/tests/commands_accounts_01/part_01.rs": 1589, + "crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01.rs": 1245, "crates/jcode-tui/src/tui/app/tests/onboarding_eval.rs": 3305, "crates/jcode-tui/src/tui/app/tests/onboarding_flow.rs": 1672, - "crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs": 1712, - "crates/jcode-tui/src/tui/app/tests/remote_events_reload_04.rs": 2676, - "crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs": 1251, - "crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs": 2413, + "crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs": 1700, + "crates/jcode-tui/src/tui/app/tests/remote_events_reload_04.rs": 2622, + "crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs": 2334, "crates/jcode-tui/src/tui/app/tests/remote_startup_input_03/part_01.rs": 1340, - "crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_01.rs": 1583, + "crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_01.rs": 1404, "crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_01.rs": 1580, - "crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_02.rs": 1726, + "crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_02.rs": 1683, "crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs": 1716, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_01/part_01.rs": 1382, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs": 1683, - "crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs": 3121, - "crates/jcode-tui/src/tui/app/tests/swarm_plan_graph_inline.rs": 1883, - "crates/jcode-tui/src/tui/info_widget_tests.rs": 1961, - "crates/jcode-tui/src/tui/session_picker/loading_tests.rs": 1568, - "crates/jcode-tui/src/tui/session_picker_tests.rs": 2806, - "crates/jcode-tui/src/tui/ui_messages/tests.rs": 3653, - "crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs": 1225, - "crates/jcode-tui/src/tui/ui_tests/prepare.rs": 1374, - "crates/jcode-tui/src/tui/ui_tests/tools.rs": 1530, - "src/cli/commands_tests.rs": 1805, - "tests/e2e/test_support/mod.rs": 1417 + "crates/jcode-tui/src/tui/app/tests/state_model_poke_01/part_01.rs": 1379, + "crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs": 1564, + "crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs": 3119, + "crates/jcode-tui/src/tui/app/tests/swarm_plan_graph_inline.rs": 1857, + "crates/jcode-tui/src/tui/info_widget_tests.rs": 1938, + "crates/jcode-tui/src/tui/session_picker/loading_tests.rs": 1596, + "crates/jcode-tui/src/tui/session_picker_tests.rs": 2655, + "crates/jcode-tui/src/tui/ui_messages/tests.rs": 3737, + "crates/jcode-tui/src/tui/ui_tests/prepare.rs": 1344, + "crates/jcode-tui/src/tui/ui_tests/tools.rs": 1476, + "src/cli/args/tests.rs": 1225, + "src/cli/commands_tests.rs": 1764 }, "version": 1 } diff --git a/src/cli/cloud_managed.rs b/src/cli/cloud_managed.rs index f2be4717d51..1e0198365b7 100644 --- a/src/cli/cloud_managed.rs +++ b/src/cli/cloud_managed.rs @@ -24,22 +24,6 @@ const DEFAULT_API_BASE: &str = "https://api.jcode.sh/v1"; const PROVISION_DEADLINE: Duration = Duration::from_secs(10 * 60); const POLL_INTERVAL: Duration = Duration::from_secs(4); -/// A live multiplexed SSH master means the machine is awake and this client is -/// already authorized, so the Jcode Cloud wake/authorize call can be skipped. -pub(crate) fn master_is_live() -> bool { - let Ok(args) = ssh_args() else { - return false; - }; - Command::new("ssh") - .args(args) - .args(["-O", "check", HOST]) - .stdin(std::process::Stdio::null()) - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::null()) - .status() - .is_ok_and(|status| status.success()) -} - pub(crate) fn is_managed(host: &str) -> bool { host == HOST } @@ -133,6 +117,11 @@ pub(super) fn proxy_executable(exe: PathBuf) -> PathBuf { } } +pub(crate) fn preparation_namespace() -> Result { + let account = account()?; + control_namespace(&account.key, &account.base).context("missing cloud account namespace") +} + fn control_namespace(key: &str, base: &str) -> Option { use sha2::{Digest, Sha256}; @@ -338,18 +327,31 @@ pub(crate) fn stop() -> Result { request("POST", "cloud/host/stop", Some(serde_json::json!({}))) } +fn timed_managed_stage(stage: &str, operation: impl FnOnce() -> Result) -> Result { + let start = Instant::now(); + let result = operation(); + crate::logging::info(&format!( + "cloud prepare: {stage} took {}ms (ok={})", + start.elapsed().as_millis(), + result.is_ok() + )); + result +} + /// Create or wake the machine and wait until it accepts this client's key. /// `progress` receives each distinct status message. pub(crate) fn connect(progress: &mut dyn FnMut(&str)) -> Result { - let key = public_key()?; + let key = timed_managed_stage("cloud client key preparation", public_key)?; let deadline = Instant::now() + PROVISION_DEADLINE; let mut last = String::new(); loop { - let reply = request( - "POST", - "cloud/host/connect", - Some(serde_json::json!({ "public_key": key })), - )?; + let reply = timed_managed_stage("cloud connect API request", || { + request( + "POST", + "cloud/host/connect", + Some(serde_json::json!({ "public_key": key })), + ) + })?; if reply.ready { return Ok(reply); } @@ -364,7 +366,10 @@ pub(crate) fn connect(progress: &mut dyn FnMut(&str)) -> Result { if Instant::now() >= deadline { bail!("Jcode Cloud machine did not become ready in time. Retry to keep waiting."); } - std::thread::sleep(POLL_INTERVAL); + timed_managed_stage("cloud wake poll wait", || { + std::thread::sleep(POLL_INTERVAL); + Ok(()) + })?; } } @@ -534,6 +539,11 @@ fn prepare_script_for_paths_with_candidate( let privilege = if elevate { "sudo -n " } else { "" }; let home = shell_quote(home); let want = shell_quote(target.unwrap_or("")); + let capability_schema = shell_quote(&format!( + "handoff-capabilities-v1:{}:{}", + jcode_build_meta::VERSION, + jcode_build_meta::GIT_HASH, + )); format!( r#"set -eu want={want} @@ -546,6 +556,12 @@ cli="$HOME/.local/bin/jcode" backend={backend} root="$HOME/.local/share/jcode/cloud-releases" mkdir -p "$HOME/.local/bin" "$root" +stage_started=$(date +%s%N) +stage_timing() {{ + stage_now=$(date +%s%N) + echo "[cloud prepare timing] $1 $(( (stage_now - stage_started) / 1000000 ))ms" >&2 + stage_started=$stage_now +}} # Serialize cooperating preflights without terminating any running process. # Wait briefly for a concurrent prepare instead of failing the move, and # reclaim a lock left behind by a prepare that was killed mid-run. @@ -565,6 +581,7 @@ until mkdir "$lock" 2>/dev/null; do sleep 2 waited=$((waited + 2)) done +stage_timing lock_wait stage= backup= next= @@ -574,6 +591,7 @@ committed=0 cleanup() {{ rc=$? trap - EXIT HUP INT TERM + if [ "$rc" != 0 ]; then rm -f "$root/.capabilities-ok"; fi if [ "$switched" = 1 ] && [ "$committed" = 0 ]; then rollback_ok=1 if [ "$had_previous" = 1 ]; then @@ -603,6 +621,34 @@ cleanup() {{ }} trap cleanup EXIT trap 'exit 1' HUP INT TERM +# Cache only successful protocol probes, not release or managed runtime policy. +# Include wrapper, backend, symlink targets and adjacent loader/library files. +# A changed install (including an atomic replacement) must be probed again. +capability_snapshot() {{ + [ -f "$cli" ] && [ -f "$backend" ] || return 1 + resolved=$(readlink -f "$backend") || return 1 + printf '%s\n' {capability_schema} + stat -c '%n %d %i %s %y %z %a' "$cli" "$backend" || return 1 + stat -Lc '%n %d %i %s %y %z %a' "$cli" "$backend" || return 1 + files=$(find -L "$(dirname "$resolved")" -maxdepth 1 -type f \ + -exec stat -Lc '%n %d %i %s %y %z %a' {{}} +) || return 1 + printf '%s\n' "$files" | LC_ALL=C sort +}} +capability_cache="$root/.capabilities-ok" +snapshot=$(capability_snapshot) || snapshot= +warm=0 +now=$(date +%s) +if [ -n "$snapshot" ] && [ -z "$development_candidate" ] && [ "${{JCODE_CLOUD_FORCE_UPGRADE:-}}" != 1 ] && [ -f "$capability_cache" ]; then + stamp=$(head -n 1 "$capability_cache") + case "$stamp" in ''|*[!0-9]*) stamp=0 ;; esac + # Longer than the local 120s handoff marker: every reuse verifies this + # installation snapshot and still evaluates version/runtime policy below. + if [ "$stamp" -le "$now" ] && [ "$((now - stamp))" -le 600 ] && [ "$(tail -n +2 "$capability_cache")" = "$snapshot" ]; then + warm=1 + fi +fi +echo "[cloud prepare] capability cache warm=$warm" >&2 +stage_timing cache_verification probe() {{ # Do not let a fast-release parent identity mask the candidate's real version. env -u JCODE_RUNTIME_RELEASE_SEMVER -u JCODE_RUNTIME_RELEASE_GIT_HASH \ @@ -610,6 +656,7 @@ probe() {{ "$1" --no-update --no-selfdev "$2" "${{3:-}}" "${{4:-}}" }} capable() {{ + if [ "$warm" = 1 ] && {{ [ "$1" = "$cli" ] || [ "$1" = "$backend" ]; }}; then return 0; fi probe "$1" cloud receive --help >/dev/null 2>&1 && probe "$1" cloud credentials --help >/dev/null 2>&1 && probe "$1" cloud router-credentials --help >/dev/null 2>&1 && @@ -670,6 +717,8 @@ if [ -x "$managed_updater" ]; then exit 1 }} elif ! capable "$cli" || ! capable "$backend" || {{ [ -n "$want" ] && {{ ! version_at_least "$cli" || ! version_at_least "$backend"; }}; }} || [ "${{JCODE_CLOUD_FORCE_UPGRADE:-}}" = 1 ]; then + # Cached evidence describes the old install, never the candidate. + warm=0 if [ -z "$want" ] && [ -z "$development_candidate" ]; then # Development builds have no corresponding published tag. Resolve the # canonical latest redirect, then pin and validate that exact release. @@ -755,6 +804,7 @@ elif ! capable "$cli" || ! capable "$backend" || {{ [ -n "$want" ] && {{ ! versi {privilege}mv -Tf "$next" "$backend" capable "$backend" && version_matches "$backend" && capable "$cli" && version_matches "$cli" || exit 1 fi +stage_timing runtime_policy_and_preparation # All handoff protocols are mandatory even when credentials are not needed yet. capable "$cli" || {{ echo '[cloud prepare] cloud handoff, replacement and group protocols are required' >&2; exit 1; }} committed=1 @@ -762,6 +812,19 @@ if [ -n {home} ] && [ ! -d {home} ]; then sudo -n install -d -o "$(id -un)" -g "$(id -gn)" -m 755 {home} fi command -v git >/dev/null || sudo -n dnf install -y git >/dev/null +# Never renew the lifetime of cached evidence without actually probing again. +final_snapshot=$(capability_snapshot) || final_snapshot= +if [ "$warm" = 1 ] && [ "$final_snapshot" != "$snapshot" ]; then + echo '[cloud prepare] runtime changed during verification; retry preparation' >&2 + exit 1 +fi +if [ "$warm" = 0 ] && [ -z "$development_candidate" ]; then + rm -f "$capability_cache" + if [ -n "$snapshot" ] && [ "$final_snapshot" = "$snapshot" ]; then + (umask 077; printf '%s\n%s\n' "$now" "$snapshot" > "$capability_cache") + fi +fi +stage_timing final_verification "# ) } diff --git a/src/cli/cloud_managed_tests.rs b/src/cli/cloud_managed_tests.rs index f24dabceaa9..d04b8906aa0 100644 --- a/src/cli/cloud_managed_tests.rs +++ b/src/cli/cloud_managed_tests.rs @@ -55,6 +55,7 @@ mod shell { r#"#!/bin/sh set -eu [ -z "${{JCODE_RUNTIME_RELEASE_SEMVER:-}}" ] || exit 97 +printf '%s\n' "$*" >> "$HOME/probes" if [ {fail_after_switch} = true ] && [ "$0" = "$HOME/opt/jcode" ]; then exit 96; fi case "$*" in '--no-update --no-selfdev --version') echo 'jcode {version} (test)' ;; @@ -187,17 +188,21 @@ cp "$HOME/fixtures/${url##*/}" "$dest" ); let path = self.dir.path().join("prepare.sh"); std::fs::write(&path, script).unwrap(); - Command::new("sh") - .arg(path) + self.prepared_command().output().unwrap() + } + + fn prepared_command(&self) -> Command { + let mut command = Command::new("sh"); + command + .arg(self.dir.path().join("prepare.sh")) .env("HOME", self.dir.path()) .env( "PATH", format!("{}:/usr/bin:/bin", self.dir.path().join("mocks").display()), ) .env("JCODE_RUNTIME_RELEASE_SEMVER", "9.9.9") - .env_remove("JCODE_CLOUD_FORCE_UPGRADE") - .output() - .unwrap() + .env_remove("JCODE_CLOUD_FORCE_UPGRADE"); + command } fn assert_old(&self) { @@ -222,6 +227,161 @@ cp "$HOME/fixtures/${url##*/}" "$dest" } } + #[test] + fn warm_preparation_reuses_only_verified_unchanged_capabilities() { + let f = Fixture::new(); + f.installed(true); + let start = Instant::now(); + let cold = f.run(Some("v0.99.1")); + let cold_elapsed = start.elapsed(); + assert!( + cold.status.success(), + "{}", + String::from_utf8_lossy(&cold.stderr) + ); + let probes = f.dir.path().join("probes"); + let cold_probes = std::fs::read_to_string(&probes).unwrap(); + let cache = f + .dir + .path() + .join(".local/share/jcode/cloud-releases/.capabilities-ok"); + let cold_evidence = std::fs::read(&cache).unwrap(); + assert_eq!( + cold_probes.lines().filter(|l| l.contains("cloud ")).count(), + 18 + ); + std::fs::write(&probes, "").unwrap(); + let start = Instant::now(); + let warm = f.run(Some("v0.99.1")); + let warm_elapsed = start.elapsed(); + assert!( + warm.status.success(), + "{}", + String::from_utf8_lossy(&warm.stderr) + ); + let warm_probes = std::fs::read_to_string(&probes).unwrap(); + assert_eq!( + warm_probes.lines().filter(|l| l.contains("cloud ")).count(), + 0 + ); + assert_eq!( + warm_probes + .lines() + .filter(|l| l.contains("--version")) + .count(), + 2 + ); + let stderr = String::from_utf8_lossy(&warm.stderr); + assert!(stderr.contains("capability cache warm=1")); + assert_eq!(std::fs::read(&cache).unwrap(), cold_evidence); + for stage in [ + "lock_wait", + "cache_verification", + "runtime_policy_and_preparation", + "final_verification", + ] { + assert!(stderr.contains(&format!("[cloud prepare timing] {stage} "))); + } + eprintln!( + "representative generated-shell fixture: cold={cold_elapsed:?} (18 capability executions), warm={warm_elapsed:?} (0 capability executions, 2 version checks)" + ); + + // Adjacent library changes invalidate wrapper evidence as well. + std::fs::write(f.dir.path().join("opt/libfixture.so"), "changed").unwrap(); + let changed = f.run(Some("v0.99.1")); + assert!(changed.status.success()); + assert!(String::from_utf8_lossy(&changed.stderr).contains("capability cache warm=0")); + + // The release policy is still evaluated on a warm host. + f.artifact(true, false, None); + let upgraded = f.run(Some("v0.99.123")); + assert!( + upgraded.status.success(), + "{}", + String::from_utf8_lossy(&upgraded.stderr) + ); + assert!(f.backend().is_symlink()); + } + + #[test] + fn concurrent_remote_preparation_probes_once_then_reuses_verified_install() { + let f = Fixture::new(); + f.installed(true); + assert!(f.run(Some("v0.99.1")).status.success()); + std::fs::remove_file( + f.dir + .path() + .join(".local/share/jcode/cloud-releases/.capabilities-ok"), + ) + .unwrap(); + std::fs::write(f.dir.path().join("probes"), "").unwrap(); + let outputs = std::thread::scope(|scope| { + let first = scope.spawn(|| f.prepared_command().output().unwrap()); + let second = scope.spawn(|| f.prepared_command().output().unwrap()); + [first.join().unwrap(), second.join().unwrap()] + }); + for output in &outputs { + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + } + assert_eq!( + outputs + .iter() + .filter(|o| String::from_utf8_lossy(&o.stderr).contains("capability cache warm=1")) + .count(), + 1 + ); + let probes = std::fs::read_to_string(f.dir.path().join("probes")).unwrap(); + assert_eq!(probes.lines().filter(|l| l.contains("cloud ")).count(), 18); + } + + #[test] + fn warm_preparation_expiry_corruption_and_failure_invalidate_evidence() { + let f = Fixture::new(); + f.installed(true); + assert!(f.run(Some("v0.99.1")).status.success()); + let cache = f + .dir + .path() + .join(".local/share/jcode/cloud-releases/.capabilities-ok"); + let saved = std::fs::read_to_string(&cache).unwrap(); + let (_, snapshot) = saved.split_once('\n').unwrap(); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + std::fs::write(&cache, format!("{}\n{snapshot}", now - 121)).unwrap(); + let past_handoff_marker = f.run(Some("v0.99.1")); + assert!(past_handoff_marker.status.success()); + assert!( + String::from_utf8_lossy(&past_handoff_marker.stderr) + .contains("capability cache warm=1") + ); + std::fs::write(&cache, format!("{}\n{snapshot}", now - 601)).unwrap(); + let expired = f.run(Some("v0.99.1")); + assert!(expired.status.success()); + assert!(String::from_utf8_lossy(&expired.stderr).contains("capability cache warm=0")); + for stamp in ["0", "invalid", "9999999999"] { + let saved = std::fs::read_to_string(&cache).unwrap(); + let (_, snapshot) = saved.split_once('\n').unwrap(); + std::fs::write(&cache, format!("{stamp}\n{snapshot}")).unwrap(); + let output = f.run(Some("v0.99.1")); + assert!(output.status.success()); + assert!(String::from_utf8_lossy(&output.stderr).contains("capability cache warm=0")); + } + // A changed binary may not inherit prior capability evidence. + executable(&f.backend(), &binary("v0.99.1", false, false)); + executable( + &f.dir.path().join("opt/runtime-update"), + "#!/bin/sh\nexit 1\n", + ); + assert!(!f.run(Some("v0.99.1")).status.success()); + assert!(!cache.exists()); + } + #[test] fn managed_policy_keeps_capable_runtime_despite_newer_client_release() { let f = Fixture::new(); diff --git a/src/cli/cloud_move/transport.rs b/src/cli/cloud_move/transport.rs index d8109b0b5eb..a478147c5c5 100644 --- a/src/cli/cloud_move/transport.rs +++ b/src/cli/cloud_move/transport.rs @@ -137,38 +137,76 @@ impl Target { /// Fail fast, with an actionable message, when the cloud host's jcode is /// too old to speak the move protocol. pub(super) fn preflight(&self, host: &str) -> Result<()> { - if self.transport.is_none() && preflight_is_fresh(&self.preflight_key(host)) { + // Lock across the cache check and preparation, including other CLI + // processes. Waiters reuse only a fully successful preflight. + let _guard = if self.transport.is_none() { + Some(timed_move_stage("preflight singleflight wait", || { + let path = crate::storage::jcode_dir()?.join("cloud-preflight.lock"); + let file = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(path)?; + file.lock().context("lock cloud preflight")?; + Ok::<_, anyhow::Error>(file) + })?) + } else { + None + }; + let key = self.preflight_key(host)?; + if self.transport.is_none() + && std::env::var("JCODE_CLOUD_FORCE_UPGRADE").as_deref() != Ok("1") + && preflight_is_fresh(&key) + { crate::logging::info("cloud move: host preflight skipped, prepared moments ago"); return Ok(()); } let result = self.preflight_uncached(host); if result.is_ok() && self.transport.is_none() { - record_preflight(&self.preflight_key(host)); + record_preflight(&key); + } else if result.is_err() && self.transport.is_none() { + // An old marker must not survive a failed verification. + if let Some(path) = super::preflight_marker() { + let _ = std::fs::remove_file(path); + } } result } - fn preflight_key(&self, host: &str) -> String { + fn preflight_key(&self, host: &str) -> Result { // Capability changes invalidate markers written by older clients. - format!( - "{host}\n{}\n{}", + let namespace = if super::super::cloud_managed::is_managed(host) && self.transport.is_none() + { + super::super::cloud_managed::preparation_namespace()? + } else { + String::new() + }; + let exe = std::env::current_exe().context("locate preflight client")?; + // A running daemon's executable can have been replaced by an update. + // Its compiled identity still scopes evidence even when that old path + // no longer has metadata. + let metadata = exe.metadata().ok().map(|m| (m.len(), m.modified().ok())); + Ok(format!( + "{host}\n{}\n{}\n{namespace}\n{:?}\n{:?}\n{}\n{:?}\n{}\n{}\n{}", self.remote_binary(), - REQUIRED_REMOTE_COMMANDS.join(",") - ) + REQUIRED_REMOTE_COMMANDS.join(","), + std::env::var_os("JCODE_CLOUD_RELEASE"), + std::env::var_os("JCODE_CLOUD_FORCE_UPGRADE"), + exe.display(), + metadata, + jcode_build_meta::VERSION, + jcode_build_meta::GIT_HASH, + jcode_build_meta::semver(), + )) } fn preflight_uncached(&self, host: &str) -> Result<()> { timed_move_stage("host preflight", || { if super::super::cloud_managed::is_managed(host) && self.transport.is_none() { - // Usually the machine is already running and this is one - // authorization request. `connect` reports real wake progress. + // The SSH ProxyCommand connects and reports wake progress. + // Calling connect here too would authorize the same key twice + // whenever the multiplexed master has expired. step("prepare", "connecting to your Jcode Cloud machine"); - timed_move_stage("cloud api connect", || { - if super::super::cloud_managed::master_is_live() { - return Ok(()); - } - super::super::cloud_managed::connect(&mut |m| step("prepare", m)).map(|_| ()) - })?; let home = dirs::home_dir() .context("locate local home directory for cloud preparation")? .to_string_lossy() @@ -198,6 +236,12 @@ impl Target { } Ok(()) })?; + // The managed preparation already verified this exact wrapper + // and every mandatory protocol. Custom binary selections still + // need their own capability probes below. + if self.remote_binary() == "jcode" { + return Ok(()); + } } // Preserve custom transports that only support one remote invocation. if self.transport.is_none() { @@ -214,18 +258,20 @@ impl Target { "out=$({binary} --no-update --no-selfdev cloud {command} --help 2>&1 >/dev/null) || {{ printf '%s\\ncloud protocol probe failed: {command} (remote %s)\\n' \"$out\" \"$version\" >&2; exit 4; }}\n" )); } - let output = output_with_ssh_retry( - || { - let mut command = Self::ssh_command(host)?; - command - .arg(&script) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::piped()); - Ok(command) - }, - host, - )?; + let output = timed_move_stage("host capability verification", || { + output_with_ssh_retry( + || { + let mut command = Self::ssh_command(host)?; + command + .arg(&script) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()); + Ok(command) + }, + host, + ) + })?; let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); return match output.status.code() { Some(0) => Ok(()), @@ -389,6 +435,13 @@ impl Target { .context("ssh stdin")? .write_all(script.as_bytes()); let output = child.wait_with_output()?; + for line in String::from_utf8_lossy(&output.stderr).lines() { + if line.starts_with("[cloud prepare timing] ") + || line.starts_with("[cloud prepare] capability cache warm=") + { + crate::logging::info(line); + } + } if output.status.success() { written.context("send cloud preparation script")?; return Ok(()); diff --git a/src/cli/cloud_move/transport/tests.rs b/src/cli/cloud_move/transport/tests.rs index 490567057c1..55e50b47409 100644 --- a/src/cli/cloud_move/transport/tests.rs +++ b/src/cli/cloud_move/transport/tests.rs @@ -1,5 +1,162 @@ use super::*; +struct RestoreEnv(Vec<(&'static str, Option)>); + +impl RestoreEnv { + fn save(names: &[&'static str]) -> Self { + Self( + names + .iter() + .map(|&name| (name, std::env::var_os(name))) + .collect(), + ) + } +} + +impl Drop for RestoreEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + match value { + Some(value) => crate::env::set_var(name, value), + None => crate::env::remove_var(name), + } + } + } +} + +#[test] +fn preflight_cache_identity_tracks_account_endpoint_release_and_binary() -> Result<()> { + let _lock = crate::storage::lock_test_env(); + let _restore = RestoreEnv::save(&[ + "JCODE_API_KEY", + "JCODE_API_BASE", + "JCODE_CLOUD_RELEASE", + "JCODE_CLOUD_REMOTE_BINARY", + ]); + crate::env::set_var("JCODE_API_KEY", "offline-account-a"); + crate::env::set_var("JCODE_API_BASE", "https://offline-a.invalid/v1"); + crate::env::remove_var("JCODE_CLOUD_RELEASE"); + crate::env::remove_var("JCODE_CLOUD_REMOTE_BINARY"); + let target = Target::default(); + let host = super::super::super::cloud_managed::HOST; + let initial = target.preflight_key(host)?; + assert!(!initial.contains("offline-account-a")); + crate::env::set_var("JCODE_API_KEY", "offline-account-b"); + assert_ne!(initial, target.preflight_key(host)?); + crate::env::set_var("JCODE_API_KEY", "offline-account-a"); + crate::env::set_var("JCODE_API_BASE", "https://offline-b.invalid/v1"); + assert_ne!(initial, target.preflight_key(host)?); + crate::env::set_var("JCODE_API_BASE", "https://offline-a.invalid/v1"); + crate::env::set_var("JCODE_CLOUD_RELEASE", "v99.0.0"); + assert_ne!(initial, target.preflight_key(host)?); + crate::env::remove_var("JCODE_CLOUD_RELEASE"); + crate::env::set_var("JCODE_CLOUD_REMOTE_BINARY", "/other/jcode"); + assert_ne!(initial, target.preflight_key(host)?); + Ok(()) +} + +#[test] +fn managed_preparation_verifies_default_once_and_still_probes_custom_binary() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + let _lock = crate::storage::lock_test_env(); + let _restore = RestoreEnv::save(&[ + "JCODE_HOME", + "PATH", + "JCODE_API_KEY", + "JCODE_API_BASE", + "JCODE_CLOUD_REMOTE_BINARY", + "JCODE_CLOUD_FORCE_UPGRADE", + "JCODE_CLOUD_RELEASE", + ]); + let dir = tempfile::tempdir()?; + crate::env::set_var("JCODE_HOME", dir.path()); + crate::env::set_var("JCODE_API_KEY", "offline-account"); + // Reintroduced direct API calls must not reach any real cloud endpoint. + crate::env::set_var("JCODE_API_BASE", "https://127.0.0.1:9/v1"); + crate::env::remove_var("JCODE_CLOUD_REMOTE_BINARY"); + crate::env::remove_var("JCODE_CLOUD_FORCE_UPGRADE"); + crate::env::remove_var("JCODE_CLOUD_RELEASE"); + let ssh = dir.path().join("ssh"); + std::fs::write( + &ssh, + "#!/bin/sh\nprintf 'invocation\\n' >> \"$JCODE_HOME/calls\"\ncat >\"$JCODE_HOME/sent-script\"\n", + )?; + std::fs::set_permissions(&ssh, std::fs::Permissions::from_mode(0o755))?; + crate::env::set_var("PATH", format!("{}:/usr/bin:/bin", dir.path().display())); + let host = super::super::super::cloud_managed::HOST; + Target::default().preflight(host)?; + let preparation = std::fs::read_to_string(dir.path().join("sent-script"))?; + for command in REQUIRED_REMOTE_COMMANDS { + assert!(preparation.contains(&format!("cloud {command} --help"))); + } + assert_eq!( + std::fs::read_to_string(dir.path().join("calls"))? + .lines() + .count(), + 1 + ); + let custom = Target { + remote_binary: Some("/custom/jcode".into()), + ..Target::default() + }; + custom.preflight(host)?; + assert_eq!( + std::fs::read_to_string(dir.path().join("calls"))? + .lines() + .count(), + 3 + ); + Ok(()) +} + +#[test] +fn concurrent_preflights_share_success_and_failed_checks_clear_marker() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + let _lock = crate::storage::lock_test_env(); + let _restore = RestoreEnv::save(&[ + "JCODE_HOME", + "PATH", + "JCODE_CLOUD_REMOTE_BINARY", + "JCODE_CLOUD_FORCE_UPGRADE", + ]); + let dir = tempfile::tempdir()?; + crate::env::set_var("JCODE_HOME", dir.path()); + crate::env::remove_var("JCODE_CLOUD_REMOTE_BINARY"); + crate::env::remove_var("JCODE_CLOUD_FORCE_UPGRADE"); + let ssh = dir.path().join("ssh"); + std::fs::write( + &ssh, + "#!/bin/sh\nprintf 'probe\\n' >> \"$JCODE_HOME/calls\"\nsleep 0.1\n[ ! -f \"$JCODE_HOME/fail\" ]\n", + )?; + std::fs::set_permissions(&ssh, std::fs::Permissions::from_mode(0o755))?; + crate::env::set_var("PATH", format!("{}:/usr/bin:/bin", dir.path().display())); + std::thread::scope(|scope| { + let first = scope.spawn(|| Target::default().preflight("offline-fixture")); + let second = scope.spawn(|| Target::default().preflight("offline-fixture")); + first.join().unwrap().unwrap(); + second.join().unwrap().unwrap(); + }); + assert_eq!( + std::fs::read_to_string(dir.path().join("calls"))? + .lines() + .count(), + 1 + ); + crate::env::set_var("JCODE_CLOUD_FORCE_UPGRADE", "1"); + Target::default().preflight("offline-fixture")?; + std::fs::write(dir.path().join("fail"), "")?; + assert!(Target::default().preflight("offline-fixture").is_err()); + assert!(!super::super::preflight_marker().unwrap().exists()); + crate::env::remove_var("JCODE_CLOUD_FORCE_UPGRADE"); + let changed = Target { + remote_binary: Some("different-jcode".into()), + ..Target::default() + }; + assert!(changed.preflight("offline-fixture").is_err()); + assert!(!super::super::preflight_marker().unwrap().exists()); + Ok(()) +} + fn scripted_target(script: &str) -> Result<(tempfile::TempDir, Target)> { let dir = tempfile::tempdir()?; let path = dir.path().join("transport.sh"); From 7a3405a21c46017709d9a9bc4f7e7f7c9c5df18d Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:09:49 -0800 Subject: [PATCH 018/236] Fence recent history paging against live transcript changes --- crates/jcode-tui/src/tui/app/state_ui_messages.rs | 8 +++++++- .../jcode-tui/src/tui/app/tests/recent_history.rs | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/state_ui_messages.rs b/crates/jcode-tui/src/tui/app/state_ui_messages.rs index 53b54d68850..eb6c0f57fd3 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_messages.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_messages.rs @@ -903,6 +903,9 @@ impl App { } pub(super) fn maybe_queue_compacted_history_load_with_overshoot(&mut self, overshoot: usize) { + if self.recent_history_request.is_some() { + return; + } if !self.auto_scroll_paused { return; } @@ -950,7 +953,10 @@ impl App { } pub(super) fn take_pending_compacted_history_load(&mut self) -> Option { - if self.recent_history_retry_at.is_some_and(|deadline| Instant::now() < deadline) { + if self + .recent_history_retry_at + .is_some_and(|deadline| Instant::now() < deadline) + { return None; } self.recent_history_retry_at = None; diff --git a/crates/jcode-tui/src/tui/app/tests/recent_history.rs b/crates/jcode-tui/src/tui/app/tests/recent_history.rs index bd1789bc9ba..da60ca8a894 100644 --- a/crates/jcode-tui/src/tui/app/tests/recent_history.rs +++ b/crates/jcode-tui/src/tui/app/tests/recent_history.rs @@ -76,7 +76,10 @@ fn recent_history_page_retries_instead_of_discarding_live_append() { app.display_messages.last().unwrap().content, "live response after request" ); - assert_eq!(app.compacted_history_lazy.pending_request_visible, Some(192)); + assert_eq!( + app.compacted_history_lazy.pending_request_visible, + Some(192) + ); assert_eq!(app.take_pending_compacted_history_load(), None); app.recent_history_retry_at = Some(Instant::now()); assert_eq!(app.take_pending_compacted_history_load(), Some(192)); @@ -126,8 +129,14 @@ fn recent_history_page_rejects_snapshot_missing_already_visible_rows() { let mut app = recent_history_test_app(); let mut stale = vec![DisplayMessage::user("latest prompt")]; assert!(!app.recent_history_page_contains_visible_tail(&mut stale, 192)); - assert_eq!(app.compacted_history_lazy.pending_request_visible, Some(192)); - assert_eq!(app.display_messages.last().unwrap().content, "latest answer"); + assert_eq!( + app.compacted_history_lazy.pending_request_visible, + Some(192) + ); + assert_eq!( + app.display_messages.last().unwrap().content, + "latest answer" + ); let mut newer = vec![ DisplayMessage::assistant("older page content"), DisplayMessage::user("latest prompt"), From f360ab417173788bbfa66d38497bb5d152277f64 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:11:56 -0800 Subject: [PATCH 019/236] Prioritize fresh mobile saves over reconciliation backlog --- crates/jcode-base/src/mobile_sync.rs | 120 ++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 11 deletions(-) diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index f2769c2aa33..ef9c1ef709c 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -31,6 +31,9 @@ const MAX_TEXT_CHARS: usize = 100_000; /// Only sessions touched within this window are synced on enable. const BACKFILL_WINDOW: Duration = Duration::from_secs(14 * 24 * 3600); const SYNC_DEBOUNCE: Duration = Duration::from_millis(150); +// Also bounded when no background worker runs or mobile sync is disabled. +// Evicted hints are recovered by the periodic snapshot/journal reconciliation. +const MAX_DIRTY_SESSIONS: usize = 1024; static PERSISTED: tokio::sync::Notify = tokio::sync::Notify::const_new(); static DIRTY_SESSIONS: Mutex> = Mutex::new(BTreeSet::new()); static SYNC_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); @@ -39,13 +42,21 @@ static SYNC_PASS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); /// Notify retains a permit while an upload is running. The set also retains /// same-millisecond writes which the reconciliation mtime cannot distinguish. pub(crate) fn notify_session_persisted(id: &str) { - DIRTY_SESSIONS - .lock() - .unwrap_or_else(|e| e.into_inner()) - .insert(id.to_owned()); + mark_dirty(id); PERSISTED.notify_one(); } +fn mark_dirty(id: &str) { + if !valid_session_id(id) { + return; + } + let mut dirty = DIRTY_SESSIONS.lock().unwrap_or_else(|e| e.into_inner()); + if dirty.len() == MAX_DIRTY_SESSIONS && !dirty.contains(id) { + dirty.pop_first(); + } + dirty.insert(id.to_owned()); +} + fn state_lock() -> Result { let dir = storage::jcode_dir()?; std::fs::create_dir_all(&dir)?; @@ -527,7 +538,8 @@ async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result Result s, Err(_) => { - DIRTY_SESSIONS - .lock() - .unwrap_or_else(|e| e.into_inner()) - .insert(id); + mark_dirty(&id); continue; } }; @@ -610,10 +619,23 @@ async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result Date: Sat, 7 Nov 2026 22:12:15 -0800 Subject: [PATCH 020/236] Scope cloud preview cache by API endpoint and record live projection timings --- .../tui/session_picker/cloud_preview/cache.rs | 64 +++++++++++++++++-- .../cloud_preview/projection_tests.rs | 16 +++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs index 12091ffa693..7c077ff63dc 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs @@ -15,18 +15,36 @@ const TTL: Duration = Duration::from_secs(15); pub(super) fn path(session_id: &str, host: &str, epoch: u64) -> Option { let root = super::super::preview_result(crate::storage::jcode_dir(), "cache directory")?; let owner = crate::subscription_catalog::configured_api_key().unwrap_or_default(); - Some(scoped_key_path(&root, session_id, host, epoch, &owner)) + let endpoint = crate::subscription_api::configured_api_base(); + Some(scoped_key_path( + &root, session_id, host, epoch, &owner, &endpoint, + )) } #[cfg(test)] fn key_path(root: &Path, session_id: &str, host: &str, epoch: u64) -> PathBuf { - scoped_key_path(root, session_id, host, epoch, "test-owner") + scoped_key_path( + root, + session_id, + host, + epoch, + "test-owner", + "https://test.invalid", + ) } -fn scoped_key_path(root: &Path, session_id: &str, host: &str, epoch: u64, owner: &str) -> PathBuf { +fn scoped_key_path( + root: &Path, + session_id: &str, + host: &str, + epoch: u64, + owner: &str, + endpoint: &str, +) -> PathBuf { let mut digest = Sha256::new(); for field in [ owner.as_bytes(), + endpoint.as_bytes(), host.as_bytes(), session_id.as_bytes(), &epoch.to_be_bytes(), @@ -237,8 +255,44 @@ mod tests { assert_ne!(baseline, other); } assert_ne!( - scoped_key_path(root, "session", "host", 1, "owner-one"), - scoped_key_path(root, "session", "host", 1, "owner-two") + scoped_key_path( + root, + "session", + "host", + 1, + "owner-one", + "https://api-one.invalid" + ), + scoped_key_path( + root, + "session", + "host", + 1, + "owner-two", + "https://api-one.invalid" + ) + ); + assert_ne!( + scoped_key_path( + root, + "session", + "host", + 1, + "owner", + "https://api-one.invalid" + ), + scoped_key_path( + root, + "session", + "host", + 1, + "owner", + "https://api-two.invalid" + ) + ); + assert_ne!( + scoped_key_path(root, "session", "host", 1, "ab", "c"), + scoped_key_path(root, "session", "host", 1, "a", "bc") ); assert_ne!(key_path(root, "bc", "a", 0), key_path(root, "c", "ab", 0)); let hostile = key_path(root, "../../session", "secret-host/..", 1); diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs index 1a8dedd2106..73f7ce67a2a 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs @@ -1,3 +1,19 @@ +// Read-only production sample, 2026-11-08 UTC: Swan snapshot 97,722,380 bytes, +// journal 264,881 bytes. Ran this exact embedded projector over existing managed +// SSH (python3 stdin only), three samples per mode. Snapshot/journal size and +// mtime_ns were unchanged before/after each sample. No daemon RPC or host writes. +// Preview: 80 messages, 181,277 output bytes including framing/telemetry. +// Remote projection ms: 2465.50, 1862.45, 1646.69. +// End-to-end fresh SSH ms: 8923.53, 5664.24, 7751.36. +// Local Python json.loads median ms (20 parses/sample): 0.969, 1.121, 0.904. +// This measures JSON decoding only, NOT Rust parse_preview/hydration. The prior +// ~49 MB cache/~300 ms parse baseline is therefore not an apples-to-apples timing +// comparison, though the transferred projection is now bounded and much smaller. +// Inventory: 278 output bytes; remote ms: 912.64, 1453.10, 736.12. +// End-to-end fresh SSH ms: 4342.86, 4807.31, 4282.34. +// Prior 97 MB inventory baseline was 702 ms: no inventory speedup demonstrated. +// Reproduction script/results: ~/.jcode/scratch/bounded-preview-measure.py and +// ~/.jcode/scratch/bounded-preview-measure-results.jsonl on the measurement host. use super::*; use std::io::Write; From 8985eb2b56c25b02a7f088f4c3a25b0450ab153e Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:13:19 -0800 Subject: [PATCH 021/236] Preserve negotiated recent history across resume requests --- .../src/server/client_lifecycle.rs | 7 +- .../server/client_lifecycle_socket_tests.rs | 80 +++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/crates/jcode-app-core/src/server/client_lifecycle.rs b/crates/jcode-app-core/src/server/client_lifecycle.rs index 55127193e4f..d0a28a8c26f 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle.rs @@ -388,6 +388,8 @@ pub(super) async fn handle_client( let mut continue_on_disconnect = false; let mut model_usage_updates_enabled = false; let mut supports_pdf_panels = false; + // Keep only the window preference, never a session-specific prefix fingerprint. + let mut recent_history_messages = None; let mut supports_user_response = false; let mut supports_server_failover = false; // Client selfdev status is determined by Subscribe request, not server's env @@ -1574,6 +1576,7 @@ pub(super) async fn handle_client( // rather than retaining a prior pane's values. continue_on_disconnect = requested_continuation; supports_pdf_panels = requested_pdf_panels; + recent_history_messages = history_base.and_then(|base| base.recent_messages); supports_user_response = requested_user_response; supports_server_failover = requested_server_failover; active_terminal_env = terminal_env; @@ -1785,7 +1788,7 @@ pub(super) async fn handle_client( None, supports_pdf_panels, supports_turn_identity, - None, + recent_history_messages.map(crate::protocol::HistoryBase::recent), ) .await .is_err() @@ -1888,7 +1891,7 @@ pub(super) async fn handle_client( resume_working_dir.as_deref(), client_instance_id.as_deref(), client_has_local_history, - None, + recent_history_messages.map(crate::protocol::HistoryBase::recent), allow_session_takeover, &mut client_selfdev, &mut client_session_id, diff --git a/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs b/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs index 0924ee53c84..8d656e5d3dd 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle_socket_tests.rs @@ -710,6 +710,24 @@ async fn sdk_socket_cancel_workflow( // An existing session, not an empty panel: empty untitled sessions are // never written to disk and get a 30s reconnect grace on disconnect. other_session.title = Some("sdk other session".into()); + for index in 0..8 { + other_session.append_stored_message(crate::session::StoredMessage { + id: format!("history-{index}"), + role: if index % 2 == 0 { + crate::message::Role::User + } else { + crate::message::Role::Assistant + }, + content: vec![ContentBlock::Text { + text: format!("history {index}"), + cache_control: None, + }], + display_role: None, + timestamp: None, + tool_duration_ms: None, + token_usage: None, + }); + } other_session.save().unwrap(); assert!(crate::session::session_exists("sdk_other_session")); let other_registry = Registry::new(provider_template.clone()).await; @@ -722,6 +740,68 @@ async fn sdk_socket_cancel_workflow( None, ))), ); + if identity_capability && terminal_race { + // Exercise actual dispatch, not just the history handlers. A subsequent + // Subscribe is authoritative and clears an earlier opt-in for legacy clients. + let (history_task, history_stream) = connect(); + let (reader, mut writer) = history_stream.into_split(); + let mut reader = BufReader::new(reader); + for recent in [None, Some(2), None, Some(3)] { + let mut subscribe = + json!({"type":"subscribe","id":101,"working_dir":home._home.path()}); + if let Some(size) = recent { + // Deliberately stale prefix state must not leak into later requests. + subscribe["history_base"] = + json!({"count":4,"fingerprint":123,"recent_messages":size}); + } + send(&mut writer, subscribe).await; + until(&mut reader, |e| matches!(e, ServerEvent::Done { id: 101 })).await; + send( + &mut writer, + json!({"type":"resume_session","id":102,"session_id":"sdk_other_session"}), + ) + .await; + let event = until(&mut reader, |e| { + matches!(e, ServerEvent::History { id: 102, .. }) + }) + .await; + let expected = recent.map_or(8, |size| size + 1); + let ServerEvent::History { + messages, + messages_base_count, + .. + } = event + else { + unreachable!() + }; + assert_eq!(messages.len(), expected, "resume recent={recent:?}"); + assert_eq!(messages_base_count, None); + assert_eq!(messages.last().unwrap().content, "history 7"); + until(&mut reader, |e| matches!(e, ServerEvent::Done { id: 102 })).await; + send(&mut writer, json!({"type":"get_history","id":103})).await; + let event = until(&mut reader, |e| { + matches!(e, ServerEvent::History { id: 103, .. }) + }) + .await; + let ServerEvent::History { + messages, + messages_base_count, + .. + } = event + else { + unreachable!() + }; + assert_eq!(messages.len(), expected, "get_history recent={recent:?}"); + assert_eq!(messages_base_count, None); + } + drop(writer); + drop(reader); + tokio::time::timeout(Duration::from_secs(10), history_task) + .await + .expect("history client disconnect") + .unwrap() + .unwrap(); + } send( &mut observer_writer, json!({"type":"resume_session","id":23,"session_id":"sdk_other_session"}), From 6f16a8174e7c7d94f60e7bfc529288f932eafbae Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:15:36 -0800 Subject: [PATCH 022/236] Keep lazy history state and mobile sync tests in scoped modules --- .../src/server/client_lifecycle.rs | 5 +- crates/jcode-base/src/mobile_sync.rs | 580 +----------------- crates/jcode-base/src/mobile_sync_tests.rs | 571 +++++++++++++++++ crates/jcode-tui/src/tui/app.rs | 5 +- crates/jcode-tui/src/tui/app/remote.rs | 2 +- .../src/tui/app/state_ui_history_marker.rs | 65 ++ .../src/tui/app/state_ui_messages.rs | 82 +-- .../src/tui/app/tests/recent_history.rs | 14 +- crates/jcode-tui/src/tui/app/tui_lifecycle.rs | 6 +- 9 files changed, 661 insertions(+), 669 deletions(-) create mode 100644 crates/jcode-base/src/mobile_sync_tests.rs create mode 100644 crates/jcode-tui/src/tui/app/state_ui_history_marker.rs diff --git a/crates/jcode-app-core/src/server/client_lifecycle.rs b/crates/jcode-app-core/src/server/client_lifecycle.rs index d0a28a8c26f..16340719d2e 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle.rs @@ -82,10 +82,7 @@ const REQUEST_HANDLER_STALL_THRESHOLDS_MS: [u64; 3] = [2_000, 10_000, 60_000]; mod subscribe_context; use subscribe_context::*; -fn event_for_transport( - mut event: ServerEvent, - supports_turn_identity: bool, -) -> Option { +fn event_for_transport(mut event: ServerEvent, supports_turn_identity: bool) -> Option { if supports_turn_identity { return Some(event); } diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index ef9c1ef709c..ace900805e5 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -693,581 +693,5 @@ async fn background_sync() { } #[cfg(test)] -mod tests { - use super::*; - use jcode_session_types::StoredMessage; - - struct TestEnv(Vec<(&'static str, Option)>); - - impl TestEnv { - fn set(values: &[(&'static str, &std::ffi::OsStr)]) -> Self { - Self( - values - .iter() - .map(|(name, value)| { - let previous = std::env::var_os(name); - crate::env::set_var(name, value); - (*name, previous) - }) - .collect(), - ) - } - } - - impl Drop for TestEnv { - fn drop(&mut self) { - for (name, value) in &self.0 { - if let Some(value) = value { - crate::env::set_var(name, value); - } else { - crate::env::remove_var(name); - } - } - } - } - - #[test] - fn disabled_dirty_hints_are_bounded_without_reading_sync_state() { - let _lock = storage::lock_test_env(); - let home = tempfile::tempdir().unwrap(); - let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); - DIRTY_SESSIONS.lock().unwrap().clear(); - for index in 0..MAX_DIRTY_SESSIONS * 2 { - notify_session_persisted(&format!("disabled-{index}")); - } - notify_session_persisted("../invalid"); - let mut dirty = DIRTY_SESSIONS.lock().unwrap(); - assert_eq!(dirty.len(), MAX_DIRTY_SESSIONS); - assert!(dirty.contains(&format!("disabled-{}", MAX_DIRTY_SESSIONS * 2 - 1))); - assert!(!home.path().join("mobile_sync.json").exists()); - dirty.clear(); - } - - #[test] - fn state_commits_merge_concurrent_progress_and_never_reenable() { - let _lock = storage::lock_test_env(); - let home = tempfile::tempdir().unwrap(); - let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); - save_state(&MobileSyncState { - enabled: true, - ..Default::default() - }) - .unwrap(); - let generation = load_state().generation; - let progress = |uploaded, mtime_ms| SessionProgress { - uploaded, - mtime_ms, - ..Default::default() - }; - let stale = BTreeMap::from([("first".into(), progress(1, 1))]); - commit_progress( - generation, - &BTreeMap::from([("second".into(), progress(2, 2))]), - ) - .unwrap(); - commit_progress(generation, &stale).unwrap(); - assert_eq!(load_state().sessions.len(), 2); - commit_progress( - generation, - &BTreeMap::from([("first".into(), progress(3, 3))]), - ) - .unwrap(); - commit_progress(generation, &stale).unwrap(); - assert_eq!(load_state().sessions["first"].uploaded, 3); - let barrier = std::sync::Arc::new(std::sync::Barrier::new(8)); - let writers: Vec<_> = (0..8) - .map(|index| { - let barrier = barrier.clone(); - std::thread::spawn(move || { - barrier.wait(); - commit_progress( - generation, - &BTreeMap::from([(format!("parallel-{index}"), progress(index, 10))]), - ) - .unwrap(); - }) - }) - .collect(); - for writer in writers { - writer.join().unwrap(); - } - assert_eq!(load_state().sessions.len(), 10); - save_state(&MobileSyncState::default()).unwrap(); - commit_progress(generation, &stale).unwrap(); - assert!(!load_state().enabled); - assert!(load_state().sessions.is_empty()); - save_state(&MobileSyncState { - enabled: true, - ..Default::default() - }) - .unwrap(); - commit_progress(generation, &stale).unwrap(); - assert!( - load_state().sessions.is_empty(), - "old opt-in generation cannot restore wiped progress" - ); - } - - #[tokio::test] - async fn persistence_notifications_upload_under_one_second_and_retain_inflight_writes() { - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - let _lock = storage::lock_test_env(); - let home = tempfile::tempdir().unwrap(); - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let base = format!("http://{}", listener.local_addr().unwrap()); - let _env = TestEnv::set(&[ - ("JCODE_HOME", home.path().as_os_str()), - ("JCODE_API_KEY", std::ffi::OsStr::new("test-key")), - ("JCODE_API_BASE", std::ffi::OsStr::new(&base)), - ]); - let (requests_tx, mut requests) = tokio::sync::mpsc::channel(8); - let server = tokio::spawn(async move { - loop { - let (mut socket, _) = listener.accept().await.unwrap(); - let mut bytes = Vec::new(); - let mut buffer = [0; 4096]; - let body_start = loop { - let count = socket.read(&mut buffer).await.unwrap(); - assert_ne!(count, 0); - bytes.extend_from_slice(&buffer[..count]); - if let Some(index) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { - break index + 4; - } - }; - let header = String::from_utf8_lossy(&bytes[..body_start]); - let length: usize = header - .lines() - .find_map(|line| { - line.to_ascii_lowercase() - .strip_prefix("content-length:") - .map(|s| s.trim().parse().unwrap()) - }) - .unwrap(); - while bytes.len() < body_start + length { - let count = socket.read(&mut buffer).await.unwrap(); - assert_ne!(count, 0); - bytes.extend_from_slice(&buffer[..count]); - } - let body: serde_json::Value = - serde_json::from_slice(&bytes[body_start..body_start + length]).unwrap(); - let (release, wait) = tokio::sync::oneshot::channel::<()>(); - requests_tx.send((body, release)).await.unwrap(); - wait.await.unwrap(); - socket - .write_all( - b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}", - ) - .await - .unwrap(); - } - }); - let background = tokio::spawn(background_sync()); - // Consume the initial reconciliation while disabled. - tokio::time::sleep(Duration::from_millis(30)).await; - let mut session = Session::create_with_id("event-sync".into(), None, None); - session.add_message( - Role::User, - vec![ContentBlock::Text { - text: "first".into(), - cache_control: None, - }], - ); - session.save().unwrap(); - assert!(!is_enabled()); - assert!( - tokio::time::timeout(Duration::from_millis(220), requests.recv()) - .await - .is_err() - ); - save_state(&MobileSyncState { - enabled: true, - ..Default::default() - }) - .unwrap(); - let started = std::time::Instant::now(); - session.add_message( - Role::Assistant, - vec![ContentBlock::Text { - text: "second".into(), - cache_control: None, - }], - ); - session.save().unwrap(); - for title in ["coalesce one", "coalesce two", "coalesce final"] { - session.custom_title = Some(title.into()); - session.save().unwrap(); - } - let (first, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - eprintln!( - "mobile persistence-to-upload notification: {}ms", - started.elapsed().as_millis() - ); - assert!(started.elapsed() < Duration::from_secs(1)); - assert_eq!(first["messages"].as_array().unwrap().len(), 2); - assert_eq!(first["title"], "coalesce final"); - // Persist while the first upload is still blocked in the network. - session.add_message( - Role::User, - vec![ContentBlock::Text { - text: "during-upload".into(), - cache_control: None, - }], - ); - session.save().unwrap(); - release.send(()).unwrap(); - let (second, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - assert_eq!(second["messages"].as_array().unwrap().len(), 1); - assert_eq!(second["messages"][0]["seq"], 2); - assert_eq!(second["messages"][0]["text"], "during-upload"); - release.send(()).unwrap(); - // Progress persistence itself must not schedule another upload. - assert!( - tokio::time::timeout(Duration::from_millis(350), requests.recv()) - .await - .is_err() - ); - assert_eq!(load_state().sessions["event-sync"].uploaded, 3); - // Simulate a writer in another process: no notification hook in this - // process, so only the reconciliation path can discover the update. - session.add_message( - Role::User, - vec![ContentBlock::Text { - text: "external writer".into(), - cache_control: None, - }], - ); - let path = home.path().join("sessions/event-sync.json"); - storage::write_json_fast(&path, &session).unwrap(); - session = Session::load_from_path(&path).unwrap(); - let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); - let (external, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - assert_eq!(external["messages"].as_array().unwrap().len(), 1); - assert_eq!(external["messages"][0]["text"], "external writer"); - release.send(()).unwrap(); - assert_eq!(reconcile.await.unwrap().messages_uploaded, 1); - // Old snapshots have no local dirty hints, as with another process's - // backlog. Block the first old upload, then durably save a fresh session. - for id in ["old-a", "old-b", "old-c"] { - let mut old = Session::create_with_id(id.into(), None, None); - old.custom_title = Some(id.into()); - old.messages = vec![msg( - Role::User, - vec![ContentBlock::Text { - text: id.into(), - cache_control: None, - }], - )]; - storage::write_json_fast(&home.path().join(format!("sessions/{id}.json")), &old) - .unwrap(); - } - let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); - let (old, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - let old_id = old["title"].as_str().unwrap().to_owned(); - assert!(old_id.starts_with("old-")); - let mut fresh = Session::create_with_id("fresh-save".into(), None, None); - fresh.custom_title = Some("fresh-save".into()); - fresh.messages = vec![msg( - Role::User, - vec![ContentBlock::Text { - text: "fresh".into(), - cache_control: None, - }], - )]; - fresh.save().unwrap(); - release.send(()).unwrap(); - let (next, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - assert_eq!( - next["title"], "fresh-save", - "fresh save must bypass old backlog" - ); - assert_eq!(reconcile.await.unwrap().sessions_uploaded, 1); - assert_eq!(load_state().sessions[&old_id].uploaded, 1); - release.send(()).unwrap(); - tokio::time::sleep(Duration::from_millis(220)).await; - // The next periodic-style pass resumes the unvisited backlog, without - // uploading the completed old session again. - let resume = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); - for _ in 0..2 { - let (remaining, release) = - tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - assert!(remaining["title"].as_str().unwrap().starts_with("old-")); - assert_ne!(remaining["title"], old_id); - release.send(()).unwrap(); - } - assert_eq!(resume.await.unwrap().sessions_uploaded, 2); - session.custom_title = Some("metadata only".into()); - session.save().unwrap(); - let (metadata, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) - .await - .unwrap() - .unwrap(); - assert_eq!(metadata["title"], "metadata only"); - assert!(metadata["messages"].as_array().unwrap().is_empty()); - // A disable racing an upload must not be undone by its completion. - save_state(&MobileSyncState::default()).unwrap(); - release.send(()).unwrap(); - tokio::time::sleep(Duration::from_millis(220)).await; - assert!(!load_state().enabled); - assert!(load_state().sessions.is_empty()); - session.add_message( - Role::Assistant, - vec![ContentBlock::Text { - text: "disabled".into(), - cache_control: None, - }], - ); - session.save().unwrap(); - assert!( - tokio::time::timeout(Duration::from_millis(250), requests.recv()) - .await - .is_err() - ); - background.abort(); - server.abort(); - let _ = background.await; - let _ = server.await; - } - - #[test] - fn reconciliation_does_not_starve_old_candidates_or_miss_same_millisecond_writes() { - let _lock = storage::lock_test_env(); - let home = tempfile::tempdir().unwrap(); - let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); - let sessions = home.path().join("sessions"); - std::fs::create_dir_all(&sessions).unwrap(); - for index in 0..205 { - std::fs::write(sessions.join(format!("session-{index}.json")), b"{}").unwrap(); - } - assert_eq!(candidate_sessions(0).unwrap().len(), 205); - let path = sessions.join("session-0.json"); - let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); - let original = file.metadata().unwrap().modified().unwrap(); - let before = session_revision(&path); - let before_ms = session_mtime_ms(&path); - std::fs::write(&path, b"{\"changed\":true}").unwrap(); - file.set_times(std::fs::FileTimes::new().set_modified(original)) - .unwrap(); - assert_eq!(session_mtime_ms(&path), before_ms); - assert_ne!(session_revision(&path), before); - let journal = crate::session::session_journal_path_from_snapshot(&path); - std::fs::write(&journal, b"journal update\n").unwrap(); - let with_journal = session_revision(&path); - std::fs::remove_file(journal).unwrap(); - assert_ne!(session_revision(&path), with_journal); - assert_eq!(SYNC_INTERVAL, Duration::from_secs(20)); - } - - async fn mock_listing(status: &str, body: &str) -> (String, tokio::task::JoinHandle) { - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let base = format!("http://{}/", listener.local_addr().unwrap()); - let response = format!( - "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", - body.len() - ); - let task = tokio::spawn(async move { - let (mut socket, _) = listener.accept().await.unwrap(); - let mut request = Vec::new(); - let mut buffer = [0; 1024]; - while !request.windows(4).any(|window| window == b"\r\n\r\n") { - let count = socket.read(&mut buffer).await.unwrap(); - assert_ne!(count, 0); - request.extend_from_slice(&buffer[..count]); - } - socket.write_all(response.as_bytes()).await.unwrap(); - String::from_utf8(request).unwrap() - }); - (base, task) - } - - #[tokio::test] - async fn cloud_session_listing_filters_and_preserves_metadata() { - let body = serde_json::json!({"sessions": [ - {"session_id": "cloud-1", "source": "cloud", "title": "Title", - "host": "worker", "cwd": "/repo", "updated_at": "updated", "created_at": "created"}, - {"session_id": "cloud-2", "source": "cloud"}, - {"session_id": "local-1", "source": "local"}, - {"session_id": "../unsafe", "source": "cloud"}, - {"session_id": "", "source": "cloud"}, - {"session_id": "x".repeat(129), "source": "cloud"} - ]}) - .to_string(); - let (base, request) = mock_listing("200 OK", &body).await; - let sessions = - list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "test-key") - .await - .unwrap(); - assert_eq!(sessions.len(), 2); - assert_eq!(sessions[0].session_id, "cloud-1"); - assert_eq!(sessions[0].title.as_deref(), Some("Title")); - assert_eq!(sessions[0].host.as_deref(), Some("worker")); - assert_eq!(sessions[0].cwd.as_deref(), Some("/repo")); - assert_eq!(sessions[0].updated_at.as_deref(), Some("updated")); - assert_eq!(sessions[0].created_at.as_deref(), Some("created")); - assert_eq!(sessions[1].title, None); - assert_eq!(sessions[1].created_at, None); - let request = request.await.unwrap().to_ascii_lowercase(); - assert!(request.starts_with("get /mobile/sessions?limit=500 http/1.1\r\n")); - assert!(request.contains("\r\nauthorization: bearer test-key\r\n")); - } - - #[tokio::test] - async fn cloud_session_listing_rejects_http_and_schema_errors() { - for (status, body) in [ - ("401 Unauthorized", "{\"sessions\":[]}"), - ("200 OK", "not json"), - ("200 OK", "{}"), - ("200 OK", "{\"sessions\":[{\"source\":\"cloud\"}]}"), - ("200 OK", "{\"sessions\":[{\"session_id\":\"s1\"}]}"), - ] { - let (base, request) = mock_listing(status, body).await; - assert!( - list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") - .await - .is_err() - ); - request.await.unwrap(); - } - let (base, request) = mock_listing("200 OK", "{\"sessions\":[]}").await; - assert!( - list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") - .await - .unwrap() - .is_empty() - ); - request.await.unwrap(); - } - - fn msg(role: Role, content: Vec) -> StoredMessage { - StoredMessage { - id: "m".into(), - role, - content, - display_role: None, - timestamp: None, - tool_duration_ms: None, - token_usage: None, - } - } - - #[test] - fn wire_messages_keep_visible_text_and_skip_hidden() { - let mut session = Session::create_with_id("s1".into(), None, None); - session.messages = vec![ - msg( - Role::User, - vec![ContentBlock::Text { - text: "hi".into(), - cache_control: None, - }], - ), - msg( - Role::Assistant, - vec![ContentBlock::Reasoning { - text: "secret".into(), - }], - ), - msg( - Role::Assistant, - vec![ - ContentBlock::Text { - text: "running".into(), - cache_control: None, - }, - ContentBlock::ToolUse { - id: "t".into(), - name: "bash".into(), - input: serde_json::json!({"command": "ls"}), - thought_signature: None, - }, - ], - ), - msg( - Role::User, - vec![ContentBlock::ToolResult { - tool_use_id: "t".into(), - content: "x".repeat(MAX_TOOL_RESULT_CHARS + 10), - is_error: None, - }], - ), - ]; - let wire = wire_messages(&session, 0); - assert_eq!( - wire.iter().map(|m| m.seq).collect::>(), - vec![0, 2, 3] - ); - assert_eq!(wire[0].role, "user"); - assert_eq!(wire[1].text, "running\n\n🔧 bash: ls"); - assert_eq!(wire[2].role, "tool"); - assert!(wire[2].text.ends_with("(truncated)")); - assert!(!wire.iter().any(|m| m.text.contains("secret"))); - assert_eq!(wire_messages(&session, 3).len(), 1); - } - - #[test] - fn session_ids_are_validated() { - assert!(valid_session_id("session_abc-123.x")); - assert!(!valid_session_id("bad id")); - assert!(!valid_session_id("")); - } - - #[test] - fn incremental_updates_replay_rewritten_and_shrunk_prefixes() { - let mut session = Session::create_with_id("rewrite".into(), None, None); - for text in ["first", "second"] { - session.add_message( - Role::User, - vec![ContentBlock::Text { - text: text.into(), - cache_control: None, - }], - ); - } - let (initial, fingerprint) = incremental_messages(&session, &SessionProgress::default()); - assert_eq!(initial.len(), 2); - let progress = SessionProgress { - uploaded: 2, - fingerprint: Some(fingerprint), - ..Default::default() - }; - assert!(incremental_messages(&session, &progress).0.is_empty()); - session.add_message( - Role::Assistant, - vec![ContentBlock::Text { - text: "appended".into(), - cache_control: None, - }], - ); - let appended = incremental_messages(&session, &progress).0; - assert_eq!(appended.len(), 1); - assert_eq!(appended[0].seq, 2); - session.messages[0].content = vec![ContentBlock::Text { - text: "rewritten".into(), - cache_control: None, - }]; - let rewritten = incremental_messages(&session, &progress).0; - assert_eq!(rewritten.len(), 3); - assert_eq!(rewritten[0].text, "rewritten"); - session.truncate_messages(1); - assert_eq!(incremental_messages(&session, &progress).0.len(), 1); - } -} +#[path = "mobile_sync_tests.rs"] +mod tests; diff --git a/crates/jcode-base/src/mobile_sync_tests.rs b/crates/jcode-base/src/mobile_sync_tests.rs new file mode 100644 index 00000000000..dddf8b8ec4a --- /dev/null +++ b/crates/jcode-base/src/mobile_sync_tests.rs @@ -0,0 +1,571 @@ +use super::*; +use jcode_session_types::StoredMessage; + +struct TestEnv(Vec<(&'static str, Option)>); + +impl TestEnv { + fn set(values: &[(&'static str, &std::ffi::OsStr)]) -> Self { + Self( + values + .iter() + .map(|(name, value)| { + let previous = std::env::var_os(name); + crate::env::set_var(name, value); + (*name, previous) + }) + .collect(), + ) + } +} + +impl Drop for TestEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + if let Some(value) = value { + crate::env::set_var(name, value); + } else { + crate::env::remove_var(name); + } + } + } +} + +#[test] +fn disabled_dirty_hints_are_bounded_without_reading_sync_state() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + DIRTY_SESSIONS.lock().unwrap().clear(); + for index in 0..MAX_DIRTY_SESSIONS * 2 { + notify_session_persisted(&format!("disabled-{index}")); + } + notify_session_persisted("../invalid"); + let mut dirty = DIRTY_SESSIONS.lock().unwrap(); + assert_eq!(dirty.len(), MAX_DIRTY_SESSIONS); + assert!(dirty.contains(&format!("disabled-{}", MAX_DIRTY_SESSIONS * 2 - 1))); + assert!(!home.path().join("mobile_sync.json").exists()); + dirty.clear(); +} + +#[test] +fn state_commits_merge_concurrent_progress_and_never_reenable() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let generation = load_state().generation; + let progress = |uploaded, mtime_ms| SessionProgress { + uploaded, + mtime_ms, + ..Default::default() + }; + let stale = BTreeMap::from([("first".into(), progress(1, 1))]); + commit_progress( + generation, + &BTreeMap::from([("second".into(), progress(2, 2))]), + ) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert_eq!(load_state().sessions.len(), 2); + commit_progress( + generation, + &BTreeMap::from([("first".into(), progress(3, 3))]), + ) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert_eq!(load_state().sessions["first"].uploaded, 3); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(8)); + let writers: Vec<_> = (0..8) + .map(|index| { + let barrier = barrier.clone(); + std::thread::spawn(move || { + barrier.wait(); + commit_progress( + generation, + &BTreeMap::from([(format!("parallel-{index}"), progress(index, 10))]), + ) + .unwrap(); + }) + }) + .collect(); + for writer in writers { + writer.join().unwrap(); + } + assert_eq!(load_state().sessions.len(), 10); + save_state(&MobileSyncState::default()).unwrap(); + commit_progress(generation, &stale).unwrap(); + assert!(!load_state().enabled); + assert!(load_state().sessions.is_empty()); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + commit_progress(generation, &stale).unwrap(); + assert!( + load_state().sessions.is_empty(), + "old opt-in generation cannot restore wiped progress" + ); +} + +#[tokio::test] +async fn persistence_notifications_upload_under_one_second_and_retain_inflight_writes() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("test-key")), + ("JCODE_API_BASE", std::ffi::OsStr::new(&base)), + ]); + let (requests_tx, mut requests) = tokio::sync::mpsc::channel(8); + let server = tokio::spawn(async move { + loop { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0; 4096]; + let body_start = loop { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + if let Some(index) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { + break index + 4; + } + }; + let header = String::from_utf8_lossy(&bytes[..body_start]); + let length: usize = header + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length:") + .map(|s| s.trim().parse().unwrap()) + }) + .unwrap(); + while bytes.len() < body_start + length { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + } + let body: serde_json::Value = + serde_json::from_slice(&bytes[body_start..body_start + length]).unwrap(); + let (release, wait) = tokio::sync::oneshot::channel::<()>(); + requests_tx.send((body, release)).await.unwrap(); + wait.await.unwrap(); + socket + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}") + .await + .unwrap(); + } + }); + let background = tokio::spawn(background_sync()); + // Consume the initial reconciliation while disabled. + tokio::time::sleep(Duration::from_millis(30)).await; + let mut session = Session::create_with_id("event-sync".into(), None, None); + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "first".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + assert!(!is_enabled()); + assert!( + tokio::time::timeout(Duration::from_millis(220), requests.recv()) + .await + .is_err() + ); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let started = std::time::Instant::now(); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "second".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + for title in ["coalesce one", "coalesce two", "coalesce final"] { + session.custom_title = Some(title.into()); + session.save().unwrap(); + } + let (first, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + eprintln!( + "mobile persistence-to-upload notification: {}ms", + started.elapsed().as_millis() + ); + assert!(started.elapsed() < Duration::from_secs(1)); + assert_eq!(first["messages"].as_array().unwrap().len(), 2); + assert_eq!(first["title"], "coalesce final"); + // Persist while the first upload is still blocked in the network. + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "during-upload".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + release.send(()).unwrap(); + let (second, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(second["messages"].as_array().unwrap().len(), 1); + assert_eq!(second["messages"][0]["seq"], 2); + assert_eq!(second["messages"][0]["text"], "during-upload"); + release.send(()).unwrap(); + // Progress persistence itself must not schedule another upload. + assert!( + tokio::time::timeout(Duration::from_millis(350), requests.recv()) + .await + .is_err() + ); + assert_eq!(load_state().sessions["event-sync"].uploaded, 3); + // Simulate a writer in another process: no notification hook in this + // process, so only the reconciliation path can discover the update. + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: "external writer".into(), + cache_control: None, + }], + ); + let path = home.path().join("sessions/event-sync.json"); + storage::write_json_fast(&path, &session).unwrap(); + session = Session::load_from_path(&path).unwrap(); + let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + let (external, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(external["messages"].as_array().unwrap().len(), 1); + assert_eq!(external["messages"][0]["text"], "external writer"); + release.send(()).unwrap(); + assert_eq!(reconcile.await.unwrap().messages_uploaded, 1); + // Old snapshots have no local dirty hints, as with another process's + // backlog. Block the first old upload, then durably save a fresh session. + for id in ["old-a", "old-b", "old-c"] { + let mut old = Session::create_with_id(id.into(), None, None); + old.custom_title = Some(id.into()); + old.messages = vec![msg( + Role::User, + vec![ContentBlock::Text { + text: id.into(), + cache_control: None, + }], + )]; + storage::write_json_fast(&home.path().join(format!("sessions/{id}.json")), &old).unwrap(); + } + let reconcile = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + let (old, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + let old_id = old["title"].as_str().unwrap().to_owned(); + assert!(old_id.starts_with("old-")); + let mut fresh = Session::create_with_id("fresh-save".into(), None, None); + fresh.custom_title = Some("fresh-save".into()); + fresh.messages = vec![msg( + Role::User, + vec![ContentBlock::Text { + text: "fresh".into(), + cache_control: None, + }], + )]; + fresh.save().unwrap(); + release.send(()).unwrap(); + let (next, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!( + next["title"], "fresh-save", + "fresh save must bypass old backlog" + ); + assert_eq!(reconcile.await.unwrap().sessions_uploaded, 1); + assert_eq!(load_state().sessions[&old_id].uploaded, 1); + release.send(()).unwrap(); + tokio::time::sleep(Duration::from_millis(220)).await; + // The next periodic-style pass resumes the unvisited backlog, without + // uploading the completed old session again. + let resume = tokio::spawn(async { sync_once(&reqwest::Client::new()).await.unwrap() }); + for _ in 0..2 { + let (remaining, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert!(remaining["title"].as_str().unwrap().starts_with("old-")); + assert_ne!(remaining["title"], old_id); + release.send(()).unwrap(); + } + assert_eq!(resume.await.unwrap().sessions_uploaded, 2); + session.custom_title = Some("metadata only".into()); + session.save().unwrap(); + let (metadata, release) = tokio::time::timeout(Duration::from_secs(1), requests.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(metadata["title"], "metadata only"); + assert!(metadata["messages"].as_array().unwrap().is_empty()); + // A disable racing an upload must not be undone by its completion. + save_state(&MobileSyncState::default()).unwrap(); + release.send(()).unwrap(); + tokio::time::sleep(Duration::from_millis(220)).await; + assert!(!load_state().enabled); + assert!(load_state().sessions.is_empty()); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "disabled".into(), + cache_control: None, + }], + ); + session.save().unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(250), requests.recv()) + .await + .is_err() + ); + background.abort(); + server.abort(); + let _ = background.await; + let _ = server.await; +} + +#[test] +fn reconciliation_does_not_starve_old_candidates_or_miss_same_millisecond_writes() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[("JCODE_HOME", home.path().as_os_str())]); + let sessions = home.path().join("sessions"); + std::fs::create_dir_all(&sessions).unwrap(); + for index in 0..205 { + std::fs::write(sessions.join(format!("session-{index}.json")), b"{}").unwrap(); + } + assert_eq!(candidate_sessions(0).unwrap().len(), 205); + let path = sessions.join("session-0.json"); + let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); + let original = file.metadata().unwrap().modified().unwrap(); + let before = session_revision(&path); + let before_ms = session_mtime_ms(&path); + std::fs::write(&path, b"{\"changed\":true}").unwrap(); + file.set_times(std::fs::FileTimes::new().set_modified(original)) + .unwrap(); + assert_eq!(session_mtime_ms(&path), before_ms); + assert_ne!(session_revision(&path), before); + let journal = crate::session::session_journal_path_from_snapshot(&path); + std::fs::write(&journal, b"journal update\n").unwrap(); + let with_journal = session_revision(&path); + std::fs::remove_file(journal).unwrap(); + assert_ne!(session_revision(&path), with_journal); + assert_eq!(SYNC_INTERVAL, Duration::from_secs(20)); +} + +async fn mock_listing(status: &str, body: &str) -> (String, tokio::task::JoinHandle) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}/", listener.local_addr().unwrap()); + let response = format!( + "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + let task = tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut request = Vec::new(); + let mut buffer = [0; 1024]; + while !request.windows(4).any(|window| window == b"\r\n\r\n") { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&buffer[..count]); + } + socket.write_all(response.as_bytes()).await.unwrap(); + String::from_utf8(request).unwrap() + }); + (base, task) +} + +#[tokio::test] +async fn cloud_session_listing_filters_and_preserves_metadata() { + let body = serde_json::json!({"sessions": [ + {"session_id": "cloud-1", "source": "cloud", "title": "Title", + "host": "worker", "cwd": "/repo", "updated_at": "updated", "created_at": "created"}, + {"session_id": "cloud-2", "source": "cloud"}, + {"session_id": "local-1", "source": "local"}, + {"session_id": "../unsafe", "source": "cloud"}, + {"session_id": "", "source": "cloud"}, + {"session_id": "x".repeat(129), "source": "cloud"} + ]}) + .to_string(); + let (base, request) = mock_listing("200 OK", &body).await; + let sessions = list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "test-key") + .await + .unwrap(); + assert_eq!(sessions.len(), 2); + assert_eq!(sessions[0].session_id, "cloud-1"); + assert_eq!(sessions[0].title.as_deref(), Some("Title")); + assert_eq!(sessions[0].host.as_deref(), Some("worker")); + assert_eq!(sessions[0].cwd.as_deref(), Some("/repo")); + assert_eq!(sessions[0].updated_at.as_deref(), Some("updated")); + assert_eq!(sessions[0].created_at.as_deref(), Some("created")); + assert_eq!(sessions[1].title, None); + assert_eq!(sessions[1].created_at, None); + let request = request.await.unwrap().to_ascii_lowercase(); + assert!(request.starts_with("get /mobile/sessions?limit=500 http/1.1\r\n")); + assert!(request.contains("\r\nauthorization: bearer test-key\r\n")); +} + +#[tokio::test] +async fn cloud_session_listing_rejects_http_and_schema_errors() { + for (status, body) in [ + ("401 Unauthorized", "{\"sessions\":[]}"), + ("200 OK", "not json"), + ("200 OK", "{}"), + ("200 OK", "{\"sessions\":[{\"source\":\"cloud\"}]}"), + ("200 OK", "{\"sessions\":[{\"session_id\":\"s1\"}]}"), + ] { + let (base, request) = mock_listing(status, body).await; + assert!( + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") + .await + .is_err() + ); + request.await.unwrap(); + } + let (base, request) = mock_listing("200 OK", "{\"sessions\":[]}").await; + assert!( + list_cloud_sessions_with_endpoint(&reqwest::Client::new(), &base, "key") + .await + .unwrap() + .is_empty() + ); + request.await.unwrap(); +} + +fn msg(role: Role, content: Vec) -> StoredMessage { + StoredMessage { + id: "m".into(), + role, + content, + display_role: None, + timestamp: None, + tool_duration_ms: None, + token_usage: None, + } +} + +#[test] +fn wire_messages_keep_visible_text_and_skip_hidden() { + let mut session = Session::create_with_id("s1".into(), None, None); + session.messages = vec![ + msg( + Role::User, + vec![ContentBlock::Text { + text: "hi".into(), + cache_control: None, + }], + ), + msg( + Role::Assistant, + vec![ContentBlock::Reasoning { + text: "secret".into(), + }], + ), + msg( + Role::Assistant, + vec![ + ContentBlock::Text { + text: "running".into(), + cache_control: None, + }, + ContentBlock::ToolUse { + id: "t".into(), + name: "bash".into(), + input: serde_json::json!({"command": "ls"}), + thought_signature: None, + }, + ], + ), + msg( + Role::User, + vec![ContentBlock::ToolResult { + tool_use_id: "t".into(), + content: "x".repeat(MAX_TOOL_RESULT_CHARS + 10), + is_error: None, + }], + ), + ]; + let wire = wire_messages(&session, 0); + assert_eq!( + wire.iter().map(|m| m.seq).collect::>(), + vec![0, 2, 3] + ); + assert_eq!(wire[0].role, "user"); + assert_eq!(wire[1].text, "running\n\n🔧 bash: ls"); + assert_eq!(wire[2].role, "tool"); + assert!(wire[2].text.ends_with("(truncated)")); + assert!(!wire.iter().any(|m| m.text.contains("secret"))); + assert_eq!(wire_messages(&session, 3).len(), 1); +} + +#[test] +fn session_ids_are_validated() { + assert!(valid_session_id("session_abc-123.x")); + assert!(!valid_session_id("bad id")); + assert!(!valid_session_id("")); +} + +#[test] +fn incremental_updates_replay_rewritten_and_shrunk_prefixes() { + let mut session = Session::create_with_id("rewrite".into(), None, None); + for text in ["first", "second"] { + session.add_message( + Role::User, + vec![ContentBlock::Text { + text: text.into(), + cache_control: None, + }], + ); + } + let (initial, fingerprint) = incremental_messages(&session, &SessionProgress::default()); + assert_eq!(initial.len(), 2); + let progress = SessionProgress { + uploaded: 2, + fingerprint: Some(fingerprint), + ..Default::default() + }; + assert!(incremental_messages(&session, &progress).0.is_empty()); + session.add_message( + Role::Assistant, + vec![ContentBlock::Text { + text: "appended".into(), + cache_control: None, + }], + ); + let appended = incremental_messages(&session, &progress).0; + assert_eq!(appended.len(), 1); + assert_eq!(appended[0].seq, 2); + session.messages[0].content = vec![ContentBlock::Text { + text: "rewritten".into(), + cache_control: None, + }]; + let rewritten = incremental_messages(&session, &progress).0; + assert_eq!(rewritten.len(), 3); + assert_eq!(rewritten[0].text, "rewritten"); + session.truncate_messages(1); + assert_eq!(incremental_messages(&session, &progress).0.len(), 1); +} diff --git a/crates/jcode-tui/src/tui/app.rs b/crates/jcode-tui/src/tui/app.rs index 1f437657772..d0eb708715c 100644 --- a/crates/jcode-tui/src/tui/app.rs +++ b/crates/jcode-tui/src/tui/app.rs @@ -118,6 +118,7 @@ mod usage_reset; mod voice_input; pub(crate) use state_ui_input_helpers::{registered_command_entries, registered_command_names}; mod justrust_status; +mod state_ui_history_marker; mod state_ui_maintenance; mod state_ui_messages; mod state_ui_runtime; @@ -941,9 +942,7 @@ pub struct App { display_edit_line_counts: (usize, usize), terminal_title: RefCell, compacted_history_lazy: CompactedHistoryLazyState, - /// Request id, display generation and target size for a normal transcript page. - recent_history_request: Option<(u64, u64, usize)>, - recent_history_retry_at: Option, + recent_history_paging: state_ui_history_marker::RecentHistoryPagingState, /// When older compacted history has just been loaded, this anchors the /// viewport to the content the reader was looking at so the prepend does not /// visibly jump. Resolved into `scroll_offset` by the next render frame. diff --git a/crates/jcode-tui/src/tui/app/remote.rs b/crates/jcode-tui/src/tui/app/remote.rs index 528be595627..737280042ee 100644 --- a/crates/jcode-tui/src/tui/app/remote.rs +++ b/crates/jcode-tui/src/tui/app/remote.rs @@ -697,7 +697,7 @@ async fn dispatch_compacted_history_load(app: &mut App, remote: &mut RemoteConne { Ok(id) => { if recent_history { - app.recent_history_request = + app.recent_history_paging.request = Some((id, app.display_messages_version, visible_messages)); } true diff --git a/crates/jcode-tui/src/tui/app/state_ui_history_marker.rs b/crates/jcode-tui/src/tui/app/state_ui_history_marker.rs new file mode 100644 index 00000000000..5304984b445 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/state_ui_history_marker.rs @@ -0,0 +1,65 @@ +//! Marker parsing and in-flight fencing for lazily loaded history windows. +use super::{CompactedHistoryLazyState, DisplayMessage}; +use std::time::Instant; + +#[derive(Default)] +pub(super) struct RecentHistoryPagingState { + /// Request id, display generation and target size of the in-flight page. + pub request: Option<(u64, u64, usize)>, + pub retry_at: Option, +} + +pub(super) fn parse_compacted_history_marker( + message: &DisplayMessage, +) -> Option { + if message.role != "system" { + return None; + } + let rest = message + .content + .strip_prefix("Earlier conversation compacted - ") + .or_else(|| { + message + .content + .strip_prefix(crate::protocol::RECENT_HISTORY_MARKER_PREFIX) + })?; + if let Some(rest) = rest.strip_prefix("showing all ") { + let (total, _) = parse_leading_usize(rest)?; + return Some(CompactedHistoryLazyState { + total_messages: total, + visible_messages: total, + ..Default::default() + }); + } + let (first, after_first) = parse_leading_usize(rest)?; + if let Some(showing) = after_first.strip_prefix(" older historical messages hidden. Showing ") { + let (visible, after_visible) = parse_leading_usize(showing)?; + let after_visible = after_visible.strip_prefix(" of ")?; + let (total, _) = parse_leading_usize(after_visible)?; + return Some(CompactedHistoryLazyState { + total_messages: total, + visible_messages: visible, + remaining_messages: first, + ..Default::default() + }); + } + after_first + .starts_with(" historical messages hidden") + .then_some(CompactedHistoryLazyState { + total_messages: first, + remaining_messages: first, + ..Default::default() + }) +} + +fn parse_leading_usize(text: &str) -> Option<(usize, &str)> { + let end = text + .char_indices() + .take_while(|(_, ch)| ch.is_ascii_digit()) + .map(|(idx, ch)| idx + ch.len_utf8()) + .last()?; + let Ok(value) = text[..end].parse() else { + return None; + }; + Some((value, &text[end..])) +} diff --git a/crates/jcode-tui/src/tui/app/state_ui_messages.rs b/crates/jcode-tui/src/tui/app/state_ui_messages.rs index eb6c0f57fd3..2c111e0260d 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_messages.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_messages.rs @@ -1,3 +1,4 @@ +use super::state_ui_history_marker::parse_compacted_history_marker; use super::state_ui_storage::{ compact_display_message_tool_data, compact_display_messages_for_storage, }; @@ -7,7 +8,6 @@ use std::time::{Duration, Instant}; const COMPACTED_HISTORY_CHUNK_MESSAGES: usize = 64; const COMPACTED_HISTORY_LOAD_SCROLL_THRESHOLD: usize = 2; -const COMPACTED_HISTORY_MARKER_PREFIX: &str = "Earlier conversation compacted - "; const OVERNIGHT_CARD_REFRESH_INTERVAL: Duration = Duration::from_secs(5); #[cfg(test)] @@ -903,7 +903,7 @@ impl App { } pub(super) fn maybe_queue_compacted_history_load_with_overshoot(&mut self, overshoot: usize) { - if self.recent_history_request.is_some() { + if self.recent_history_paging.request.is_some() { return; } if !self.auto_scroll_paused { @@ -954,12 +954,13 @@ impl App { pub(super) fn take_pending_compacted_history_load(&mut self) -> Option { if self - .recent_history_retry_at + .recent_history_paging + .retry_at .is_some_and(|deadline| Instant::now() < deadline) { return None; } - self.recent_history_retry_at = None; + self.recent_history_paging.retry_at = None; self.compacted_history_lazy.pending_request_visible.take() } @@ -975,17 +976,18 @@ impl App { /// A response computed before a live append or rewind must not discard it. /// Retry against the new generation while retaining the viewport anchor. pub(super) fn accept_recent_history_response(&mut self, id: u64) -> bool { - let Some((expected, version, visible)) = self.recent_history_request else { + let Some((expected, version, visible)) = self.recent_history_paging.request else { return !self.recent_history_window_active(); }; if id != expected { return false; } - self.recent_history_request = None; + self.recent_history_paging.request = None; if version != self.display_messages_version { if self.recent_history_window_active() { self.restore_pending_compacted_history_load(visible); - self.recent_history_retry_at = Some(Instant::now() + Duration::from_millis(250)); + self.recent_history_paging.retry_at = + Some(Instant::now() + Duration::from_millis(250)); } return false; } @@ -1021,7 +1023,7 @@ impl App { }); if !matches { self.restore_pending_compacted_history_load(visible); - self.recent_history_retry_at = Some(Instant::now() + Duration::from_millis(250)); + self.recent_history_paging.retry_at = Some(Instant::now() + Duration::from_millis(250)); } matches } @@ -1144,67 +1146,3 @@ fn carry_over_tool_durations(previous: &[DisplayMessage], incoming: &mut [Displa } } } - -fn parse_compacted_history_marker(message: &DisplayMessage) -> Option { - if message.role != "system" { - return None; - } - let rest = message - .content - .strip_prefix(COMPACTED_HISTORY_MARKER_PREFIX) - .or_else(|| { - message - .content - .strip_prefix(crate::protocol::RECENT_HISTORY_MARKER_PREFIX) - })?; - - if let Some(rest) = rest.strip_prefix("showing all ") { - let (total, _) = parse_leading_usize(rest)?; - return Some(CompactedHistoryLazyState { - total_messages: total, - visible_messages: total, - remaining_messages: 0, - hidden_user_prompts: 0, - pending_request_visible: None, - }); - } - - let (first, after_first) = parse_leading_usize(rest)?; - if after_first.starts_with(" older historical messages hidden. Showing ") { - let showing = after_first.strip_prefix(" older historical messages hidden. Showing ")?; - let (visible, after_visible) = parse_leading_usize(showing)?; - let after_visible = after_visible.strip_prefix(" of ")?; - let (total, _) = parse_leading_usize(after_visible)?; - return Some(CompactedHistoryLazyState { - total_messages: total, - visible_messages: visible, - remaining_messages: first, - hidden_user_prompts: 0, - pending_request_visible: None, - }); - } - - if after_first.starts_with(" historical messages hidden") { - return Some(CompactedHistoryLazyState { - total_messages: first, - visible_messages: 0, - remaining_messages: first, - hidden_user_prompts: 0, - pending_request_visible: None, - }); - } - - None -} - -fn parse_leading_usize(text: &str) -> Option<(usize, &str)> { - let end = text - .char_indices() - .take_while(|(_, ch)| ch.is_ascii_digit()) - .map(|(idx, ch)| idx + ch.len_utf8()) - .last()?; - let Ok(value) = text[..end].parse() else { - return None; - }; - Some((value, &text[end..])) -} diff --git a/crates/jcode-tui/src/tui/app/tests/recent_history.rs b/crates/jcode-tui/src/tui/app/tests/recent_history.rs index da60ca8a894..486b0e89da3 100644 --- a/crates/jcode-tui/src/tui/app/tests/recent_history.rs +++ b/crates/jcode-tui/src/tui/app/tests/recent_history.rs @@ -67,7 +67,7 @@ fn recent_history_marker_preserves_absolute_prompt_numbers_and_queues_page() { #[test] fn recent_history_page_retries_instead_of_discarding_live_append() { let mut app = recent_history_test_app(); - app.recent_history_request = Some((42, app.display_messages_version, 192)); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); app.display_messages .push(DisplayMessage::assistant("live response after request")); app.bump_display_messages_version(); @@ -81,16 +81,16 @@ fn recent_history_page_retries_instead_of_discarding_live_append() { Some(192) ); assert_eq!(app.take_pending_compacted_history_load(), None); - app.recent_history_retry_at = Some(Instant::now()); + app.recent_history_paging.retry_at = Some(Instant::now()); assert_eq!(app.take_pending_compacted_history_load(), Some(192)); } #[test] fn recent_history_page_rejects_wrong_request_and_rewind() { let mut app = recent_history_test_app(); - app.recent_history_request = Some((42, app.display_messages_version, 192)); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); assert!(!app.accept_recent_history_response(41)); - assert!(app.recent_history_request.is_some()); + assert!(app.recent_history_paging.request.is_some()); app.replace_display_messages(vec![DisplayMessage::user("rewound transcript")]); assert!(!app.accept_recent_history_response(42)); assert_eq!(app.take_pending_compacted_history_load(), None); @@ -100,7 +100,7 @@ fn recent_history_page_rejects_wrong_request_and_rewind() { #[test] fn recent_history_full_window_restores_archived_compaction_loading() { let mut app = recent_history_test_app(); - app.recent_history_request = Some((42, app.display_messages_version, 1000)); + app.recent_history_paging.request = Some((42, app.display_messages_version, 1000)); assert!(app.accept_recent_history_response(42)); app.apply_compacted_history_window( vec![ @@ -117,9 +117,9 @@ fn recent_history_full_window_restores_archived_compaction_loading() { #[test] fn recent_history_page_accepts_unchanged_generation() { let mut app = recent_history_test_app(); - app.recent_history_request = Some((42, app.display_messages_version, 192)); + app.recent_history_paging.request = Some((42, app.display_messages_version, 192)); assert!(app.accept_recent_history_response(42)); - assert!(app.recent_history_request.is_none()); + assert!(app.recent_history_paging.request.is_none()); // Unsolicited page payloads cannot replace a bounded current-history view. assert!(!app.accept_recent_history_response(42)); } diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs index 1061359b86b..db4b95edc71 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs @@ -332,8 +332,7 @@ impl App { display_edit_line_counts: (0, 0), terminal_title: RefCell::new(terminal_title::TerminalTitleState::default()), compacted_history_lazy: CompactedHistoryLazyState::default(), - recent_history_request: None, - recent_history_retry_at: None, + recent_history_paging: Default::default(), pending_history_anchor: None, pending_resize_anchor: None, input: String::new(), @@ -838,8 +837,7 @@ impl App { display_edit_line_counts: (0, 0), terminal_title: RefCell::new(terminal_title::TerminalTitleState::default()), compacted_history_lazy: CompactedHistoryLazyState::default(), - recent_history_request: None, - recent_history_retry_at: None, + recent_history_paging: Default::default(), pending_history_anchor: None, pending_resize_anchor: None, input: String::new(), From ad0e5262df19a37bafbdb5f5fdffe1ae4378f15a Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:16:27 -0800 Subject: [PATCH 023/236] Keep polling todo snapshots after resume so cards don't freeze --- .../jcode-tui/src/tui/app/tests/todo_card.rs | 28 +++++++++++++++++++ crates/jcode-tui/src/tui/app/todos_view.rs | 6 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/jcode-tui/src/tui/app/tests/todo_card.rs b/crates/jcode-tui/src/tui/app/tests/todo_card.rs index b139cc6ef92..1d27e02f8b0 100644 --- a/crates/jcode-tui/src/tui/app/tests/todo_card.rs +++ b/crates/jcode-tui/src/tui/app/tests/todo_card.rs @@ -74,6 +74,34 @@ fn ssh_pinned_todos_arrive_before_history_and_survive_it() { crate::todo::save_todos("remote-early-todos", &[]).unwrap(); } +#[test] +fn resumed_local_snapshot_keeps_polling_so_card_does_not_go_stale() { + let mut app = create_test_app(); + app.is_remote = true; + app.remote_session_id = Some("resumed-local-snapshot".into()); + let payload = |status: &str| { + serde_json::json!({ + "todos": [{"id":"lane", "content":"Integrate lane", "priority":"high", "status":status}], + "plan": {}, "goals": [] + }) + .to_string() + }; + // Resume seeds an authoritative snapshot, after which disk is ignored. + app.receive_remote_todos("resumed-local-snapshot".into(), payload("in_progress")); + app.toggle_todo_card(); + assert!( + app.remote_todos_refresh_due(), + "a snapshot-backed card must request fresh todos, not freeze" + ); + app.receive_remote_todos("resumed-local-snapshot".into(), payload("completed")); + let card = app + .display_messages + .iter() + .find(|m| m.role == "todos") + .unwrap(); + assert!(card.content.contains("completed"), "{}", card.content); +} + #[test] fn ssh_todos_command_renders_remote_card() { if super::commands_dispatch::ssh_test_runs_in_child("ssh_todos_command_renders_remote_card") { diff --git a/crates/jcode-tui/src/tui/app/todos_view.rs b/crates/jcode-tui/src/tui/app/todos_view.rs index 513b73efdf1..eb9519d2a96 100644 --- a/crates/jcode-tui/src/tui/app/todos_view.rs +++ b/crates/jcode-tui/src/tui/app/todos_view.rs @@ -65,7 +65,11 @@ impl App { } pub(super) fn remote_todos_refresh_due(&mut self) -> bool { - if !crate::tui::is_ssh_remote() + // Any client that renders from a server snapshot must keep polling it. + // A local resume seeds `remote_todos_payload` before History, and from + // then on `current_todo_state` ignores disk. Without polling, the card + // froze at the resume snapshot while the stored plan kept advancing. + if !(crate::tui::is_ssh_remote() || self.remote_todos_payload.is_some()) || !(self.todos_view_enabled || self.latest_todo_card_index().is_some() || crate::config::config().display.pin_todos) From af3b5318bc5a4ba2695cfd357ac1dc0e7bc5460c Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:17:28 -0800 Subject: [PATCH 024/236] Reuse managed SSH connections for cloud previews and git probes --- crates/jcode-tui/src/tui/cloud_git.rs | 38 +++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/jcode-tui/src/tui/cloud_git.rs b/crates/jcode-tui/src/tui/cloud_git.rs index bea82951bd1..b3294a27424 100644 --- a/crates/jcode-tui/src/tui/cloud_git.rs +++ b/crates/jcode-tui/src/tui/cloud_git.rs @@ -84,6 +84,10 @@ fn script_command_inner( ) -> tokio::process::Command { let mut cmd = if let Some(host) = host { let mut cmd = tokio::process::Command::new("ssh"); + let managed_config = host == "jcode-cloud" + && config.is_some_and(|path| { + crate::storage::jcode_dir().is_ok_and(|home| path == home.join("cloud/ssh_config")) + }); if compressed { cmd.arg("-C"); } @@ -116,8 +120,6 @@ fn script_command_inner( "SessionType=default", "-o", "ControlMaster=no", - "-S", - "none", "-o", "ConnectTimeout=8", "-o", @@ -125,6 +127,11 @@ fn script_command_inner( "-o", "ServerAliveCountMax=1", ]); + // Borrow the account-scoped managed master without owning or stopping + // it. Custom SSH configurations remain isolated from arbitrary muxes. + if !managed_config { + cmd.args(["-S", "none"]); + } cmd.arg("--") .arg(host) .arg(format!("sh -c {} sh {}", quote(script), quote(dir))); @@ -489,6 +496,33 @@ mod tests { ); } + #[test] + fn managed_cloud_reads_borrow_mux_but_custom_transports_remain_isolated() { + let _lock = crate::storage::lock_test_env(); + let home = crate::storage::jcode_dir().unwrap(); + let managed = home.join("cloud/ssh_config"); + for (host, config, borrow) in [ + ("jcode-cloud", managed.as_path(), true), + ("custom-host", managed.as_path(), false), + ( + "jcode-cloud", + std::path::Path::new("/private/config"), + false, + ), + ] { + let cmd = compressed_script_command(Some(host), Some(config), "session", "true"); + let args: Vec<_> = cmd + .as_std() + .get_args() + .map(|a| a.to_string_lossy()) + .collect(); + assert_eq!(args.iter().any(|a| a == "-S"), !borrow); + assert!(args.iter().any(|a| a == "ControlMaster=no")); + assert!(!args.iter().any(|a| a == "ControlMaster=auto")); + assert!(args.iter().any(|a| a == "ClearAllForwardings=yes")); + } + } + #[test] fn ssh_command_quotes_workspace_and_reuses_private_config() { let cmd = command( From ab434ba94c575ea2472460aeb1bc3ba2565570bf Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:20:44 -0800 Subject: [PATCH 025/236] perf(cloud): avoid rescanning snapshot message bodies for previews --- .../cloud_preview/projection_tests.rs | 76 ++++++++++++++-- .../session_picker/cloud_preview/remote.py | 91 ++++++++++--------- 2 files changed, 120 insertions(+), 47 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs index 73f7ce67a2a..7e200d2f84a 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs @@ -14,9 +14,74 @@ // Prior 97 MB inventory baseline was 702 ms: no inventory speedup demonstrated. // Reproduction script/results: ~/.jcode/scratch/bounded-preview-measure.py and // ~/.jcode/scratch/bounded-preview-measure-results.jsonl on the measurement host. +// Scanner follow-up, 2026-11-08 UTC, same unchanged 97,722,380-byte snapshot: +// Whole-string regex alone did not improve latency. Coalescing scalar tokens +// plus retaining the last 80 message ranges during the first scan avoids the +// second full messages scan. Actual mmap preview remote ms: 768.34, 1381.58, +// 1376.54, fresh SSH ms: 4695.30, 5622.85, 5616.78. All retain 80 messages. +// Journal grew externally between samples (363,579 -> 384,539 bytes), but each +// individual read verified snapshot/journal size+mtime unchanged during projection. +// Paired comparison freezes both files in process RAM, no remote file writes: +// old preview ms 1419.64, 2489.48, 1444.72; new 1315.81, 1160.17, 772.47. +// Every pair is byte-identical (168,621 bytes, without framing/telemetry), SHA256 +// cc39e053c4a2e80dd767fee2a4142f784a44c2cd10d6685a97493d5a04cf75fc. +// Inventory actual mmap ms 1019.11, 1507.74, 742.82: no consistent speedup. +// Evidence: ~/.jcode/scratch/bounded-preview-final-scanner-results.jsonl and +// paired-projection-final-results.txt, reproduced by paired-projection.py. use super::*; use std::io::Write; +#[test] +fn remote_scanner_handles_escaped_nested_strings_and_rejects_truncation() { + let checks = r#" +import random +random.seed(37) +samples = ['', '"', '\\', '\\"', '\\\\"', '{}[]', '\n\t', '雪😀'] +for _ in range(200): + samples.append(''.join(random.choices('abc"\\{}[]\n雪', k=120))) +for text in samples: + value = {'nested': [{'text': text, 'braces': ']}{['}, [text]], 'tail': text} + encoded = json.dumps(value, ensure_ascii=False).encode() + assert value_end(encoded + b',trailer', 0) == len(encoded) + quoted = json.dumps(text, ensure_ascii=False).encode() + assert string_end(quoted + b',trailer', 0) == len(quoted) + assert json.loads(encoded) == value +messages = [{'id': str(i), 'content': [{'text': samples[i % len(samples)]}]} for i in range(300)] +encoded = json.dumps({'messages': messages}).encode() +ranges = collections.deque(maxlen=MAX_MESSAGES) +list(fields(encoded, message_ranges=ranges)) +assert len(ranges) == MAX_MESSAGES +assert [json.loads(encoded[a:b]) for a,b in ranges] == messages[-MAX_MESSAGES:] +encoded = b'{"messages":[{"id":"old"}],"messages":[]}' +for key,a,b in fields(encoded, message_ranges=ranges): + pass +assert not ranges +for malformed in (b'{', b'[', b'{"x":"unterminated}', b'["escaped\\"', b'{]', b'[{]}'): + try: + value_end(malformed, 0) + except (ValueError, IndexError): + pass + else: + raise AssertionError(repr(malformed)) +"#; + let mut child = std::process::Command::new("python3") + .arg("-") + .stdin(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let definitions = REMOTE_PROJECTOR.split("if __name__ ==").next().unwrap(); + let mut input = child.stdin.take().unwrap(); + write!(input, "{definitions}\n{checks}").unwrap(); + drop(input); + let output = child.wait_with_output().unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); +} + fn run_projection(home: &std::path::Path, mode: &str, id: &str) -> std::process::Output { let mut child = std::process::Command::new("python3") .args(["-", mode, id]) @@ -69,7 +134,7 @@ fn oversized_remote_snapshot_is_bounded_valid_and_keeps_late_journal_updates() { for i in 0..100 { writeln!(journal, "{}", serde_json::json!({ "meta": {"title": format!("Late journal title {i}"), "working_dir": "/latest/repository", "updated_at": "2026-11-08T05:00:00Z"}, - "append_messages": [{"id": format!("late-{i}"), "role": "user", "content": [{"type": "text", "text": format!("Late journal prompt {i}")}]}] + "append_messages": [{"id": format!("late-{i}"), "role": "user", "content": [{"type": "text", "text": format!("Late journal prompt {i} escaped quote \" slash \\ nested {{[}}] 雪")}]}] })).unwrap(); } drop(journal); @@ -81,11 +146,10 @@ fn oversized_remote_snapshot_is_bounded_valid_and_keeps_late_journal_updates() { assert_eq!(loaded.messages.len(), 80); assert_eq!(loaded.messages.last().unwrap().id, "late-99"); assert_eq!(loaded.migration_epoch, 19); - assert!( - build_messages_preview(&loaded) - .iter() - .any(|m| m.content.contains("Late journal prompt 99")) - ); + assert!(build_messages_preview(&loaded).iter().any(|m| { + m.content + .contains("Late journal prompt 99 escaped quote \" slash \\ nested {[}] 雪") + })); let inventory = run_projection(home.path(), "inventory", id); let inventory: serde_json::Value = serde_json::from_slice(&inventory.stdout).unwrap(); assert_eq!(inventory["sessions"][0]["title"], "Late journal title 99"); diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py index ab704cd182f..67261a9cb15 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py @@ -8,7 +8,11 @@ import socket import sys -STRUCT = re.compile(br'["{}\[\]]') +# Consume complete strings in C, including escaped quotes and backslashes. +STRING = re.compile(br'"[^"\\]*(?:\\.[^"\\]*)*"', re.DOTALL) +# Coalesce adjacent strings, keys, numbers and punctuation into one C match. +# Only container delimiters and malformed quotes need a Python iteration. +STRUCT = re.compile(br'(?P(?:"[^"\\]*(?:\\.[^"\\]*)*"|[^"{}\[\]]+){1,64})|(?P[{}\[\]])|(?P")', re.DOTALL) SPACE = b' \r\n\t' BULKY = {'env_snapshots', 'memory_injections', 'replay_events', 'compaction', 'system_prompt'} MAX_MESSAGES = 80 @@ -19,17 +23,10 @@ def string_end(data, start): - pos = start + 1 - while True: - end = data.find(b'"', pos) - if end < 0: - raise ValueError('unterminated string') - back = end - 1 - while back > start and data[back] == 92: - back -= 1 - if (end - back) % 2: - return end + 1 - pos = end + 1 + match = STRING.match(data, start) + if match is None: + raise ValueError('unterminated string') + return match.end() def skip_space(data, pos): @@ -38,7 +35,7 @@ def skip_space(data, pos): return pos -def value_end(data, pos): +def value_end(data, pos, ranges=None): if data[pos] == 34: return string_end(data, pos) if data[pos] not in (123, 91): @@ -46,23 +43,28 @@ def value_end(data, pos): while end < len(data) and data[end] not in b',}]\r\n \t': end += 1 return end - depth = 1 - pos += 1 - while depth: - match = STRUCT.search(data, pos) - if match is None: - raise ValueError('unterminated container') - pos = match.start() - char = data[pos] - if char == 34: - pos = string_end(data, pos) + stack = [125 if data[pos] == 123 else 93] + for match in STRUCT.finditer(data, pos + 1): + kind = match.lastgroup + if kind == 'skip': continue - depth += 1 if char in (123, 91) else -1 - pos += 1 - return pos - - -def fields(data, pos=0): + if kind == 'bad': + raise ValueError('unterminated string') + char = data[match.start()] + if char in (123, 91): + if ranges is not None and len(stack) == 1: + begin = match.start() + stack.append(125 if char == 123 else 93) + elif char != stack.pop(): + raise ValueError('mismatched container') + elif not stack: + return match.end() + elif ranges is not None and len(stack) == 1: + ranges.append((begin, match.end())) + raise ValueError('unterminated container') + + +def fields(data, pos=0, message_ranges=None): pos = skip_space(data, pos) if data[pos] != 123: raise ValueError('expected object') @@ -77,7 +79,12 @@ def fields(data, pos=0): if data[pos] != 58: raise ValueError('expected colon') pos = skip_space(data, pos + 1) - end = value_end(data, pos) + ranges = message_ranges if key in ('messages', 'append_messages') else None + if ranges is not None: + ranges.clear() + if data[pos] != 91: + raise ValueError('expected message array') + end = value_end(data, pos, ranges) yield key, pos, end pos = skip_space(data, end) if data[pos] == 125: @@ -87,17 +94,18 @@ def fields(data, pos=0): pos += 1 -def recent_messages(data, start, end, messages): +def recent_messages(data, start, end, messages, ranges=None): # Locate the tail without deserializing old message bodies. The range deque # is bounded even for snapshots with millions of messages. - ranges = collections.deque(maxlen=MAX_MESSAGES) - pos = skip_space(data, start + 1) - while pos < end and data[pos] != 93: - stop = value_end(data, pos) - ranges.append((pos, stop)) - pos = skip_space(data, stop) - if data[pos] == 44: - pos = skip_space(data, pos + 1) + if ranges is None: + ranges = collections.deque(maxlen=MAX_MESSAGES) + pos = skip_space(data, start + 1) + while pos < end and data[pos] != 93: + stop = value_end(data, pos) + ranges.append((pos, stop)) + pos = skip_space(data, stop) + if data[pos] == 44: + pos = skip_space(data, pos + 1) for pos, stop in ranges: # Oversized individual messages are projected without decoding their # potentially enormous tool payloads. Recent ordinary text is retained. @@ -129,10 +137,11 @@ def project(path, preview): session = {} messages = collections.deque(maxlen=MAX_MESSAGES) with path.open('rb') as source, mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) as data: - for key, start, end in fields(data): + ranges = collections.deque(maxlen=MAX_MESSAGES) if preview else None + for key, start, end in fields(data, message_ranges=ranges): if key == 'messages': if preview: - recent_messages(data, start, end, messages) + recent_messages(data, start, end, messages, ranges) elif key not in BULKY and (preview or key in INVENTORY_FIELDS) and end - start <= 256 * 1024: session[key] = json.loads(data[start:end]) journal = path.with_suffix('.journal.jsonl') From b8948c6a465b7e46f4f6e01474d82a0672b2ab25 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:30:16 -0800 Subject: [PATCH 026/236] Log safe mobile sync stage outcomes and timings --- crates/jcode-base/src/mobile_sync.rs | 99 ++++++++++++++++++---- crates/jcode-base/src/mobile_sync_tests.rs | 43 ++++++++++ 2 files changed, 127 insertions(+), 15 deletions(-) diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index ace900805e5..c2010af9beb 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -22,7 +22,41 @@ use std::collections::{BTreeMap, BTreeSet}; use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; use std::sync::Mutex; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +// Only compile-time labels enter timing records. Never include session IDs, +// endpoints, credentials, payloads, or error strings here. Drop also covers +// early returns, propagated errors and cancellation without changing budgets. +struct SyncTiming { + stage: &'static str, + outcome: &'static str, + started: Instant, +} + +impl SyncTiming { + fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "incomplete", + started: Instant::now(), + } + } + + fn record(&self) -> String { + format!( + "mobile_sync stage={} outcome={} duration_ms={}", + self.stage, + self.outcome, + self.started.elapsed().as_millis() + ) + } +} + +impl Drop for SyncTiming { + fn drop(&mut self) { + crate::logging::info(&self.record()); + } +} pub const SYNC_INTERVAL: Duration = Duration::from_secs(20); const MAX_MESSAGES_PER_UPLOAD: usize = 200; @@ -141,6 +175,8 @@ async fn list_cloud_sessions_with_endpoint( api_base: &str, api_key: &str, ) -> Result> { + let mut timing = SyncTiming::new("http_list"); + timing.outcome = "error"; #[derive(Deserialize)] struct SessionsResponse { sessions: Vec, @@ -163,6 +199,7 @@ async fn list_cloud_sessions_with_endpoint( .json() .await .context("invalid mobile session listing response")?; + timing.outcome = "success"; Ok(response .sessions .into_iter() @@ -466,6 +503,8 @@ async fn upload( session_id: &str, body: &UploadBody<'_>, ) -> Result<()> { + let mut timing = SyncTiming::new("http_upload"); + timing.outcome = "error"; let url = format!( "{}/mobile/sessions/{}", api_base.trim_end_matches('/'), @@ -481,14 +520,11 @@ async fn upload( .context("mobile sync upload failed")?; let status = response.status(); if !status.is_success() { - let text = response.text().await.with_context(|| { - format!("mobile sync upload returned {status}, response body unreadable") - })?; - anyhow::bail!( - "mobile sync upload returned {status}: {}", - text.chars().take(300).collect::() - ); + timing.outcome = "http_error"; + // Response bodies can echo private content or credentials. + anyhow::bail!("mobile sync upload returned {status}"); } + timing.outcome = "success"; Ok(()) } @@ -506,9 +542,19 @@ pub async fn sync_once(client: &reqwest::Client) -> Result { } async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result { + let mut queue = SyncTiming::new("queue_wait"); let _pass = SYNC_PASS.lock().await; + queue.outcome = "acquired"; + drop(queue); + let mut timing = SyncTiming::new(if reconcile { + "pass_reconcile" + } else { + "pass_incremental" + }); + timing.outcome = "error"; let mut state = load_state(); if !state.enabled { + timing.outcome = "skipped_disabled"; return Ok(SyncReport::default()); } let api_key = crate::subscription_catalog::configured_api_key() @@ -540,10 +586,13 @@ async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result Result s, Err(_) => { + upload_timing.outcome = "load_error"; mark_dirty(&id); continue; } }; if session.is_debug || session.messages.is_empty() { + upload_timing.outcome = "skipped_empty_or_debug"; state.sessions.insert( id, SessionProgress { @@ -586,6 +638,7 @@ async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result Result Result Result Result<()> { + let mut timing = SyncTiming::new("http_wipe"); + timing.outcome = "error"; let api_key = crate::subscription_catalog::configured_api_key() .context("no Jcode account credential is configured")?; let url = format!( @@ -659,8 +726,10 @@ pub async fn wipe_remote(client: &reqwest::Client) -> Result<()> { .send() .await?; if !response.status().is_success() { + timing.outcome = "http_error"; anyhow::bail!("server returned {}", response.status()); } + timing.outcome = "success"; Ok(()) } @@ -679,15 +748,15 @@ async fn background_sync() { _ = PERSISTED.notified() => { // A bounded coalescing window, not a trailing debounce: // continuous token saves cannot postpone sync forever. + let mut debounce = SyncTiming::new("debounce_wait"); tokio::time::sleep(SYNC_DEBOUNCE).await; + debounce.outcome = "success"; false } }; - if !is_enabled() { - continue; - } - if let Err(err) = sync_pass(&client, reconcile).await { - crate::logging::debug(&format!("mobile sync pass failed: {err:#}")); + // sync_pass records failures and disabled skips without error text. + if sync_pass(&client, reconcile).await.is_err() { + crate::logging::debug("mobile sync pass failed (see stage timing)"); } } } diff --git a/crates/jcode-base/src/mobile_sync_tests.rs b/crates/jcode-base/src/mobile_sync_tests.rs index dddf8b8ec4a..6a4088927e2 100644 --- a/crates/jcode-base/src/mobile_sync_tests.rs +++ b/crates/jcode-base/src/mobile_sync_tests.rs @@ -1,6 +1,49 @@ use super::*; use jcode_session_types::StoredMessage; +#[test] +fn sync_timing_records_only_static_fields_and_elapsed_milliseconds() { + let mut timing = SyncTiming::new("test_stage"); + timing.started = Instant::now() - Duration::from_millis(12); + for outcome in ["success", "error", "skipped_disabled", "incomplete"] { + timing.outcome = outcome; + let record = timing.record(); + let fields: Vec<_> = record.split_whitespace().collect(); + assert_eq!(fields.len(), 4); + assert_eq!(fields[0], "mobile_sync"); + assert_eq!(fields[1], "stage=test_stage"); + assert_eq!(fields[2], format!("outcome={outcome}")); + let elapsed: u128 = fields[3] + .strip_prefix("duration_ms=") + .unwrap() + .parse() + .unwrap(); + assert!(elapsed >= 12); + } +} + +#[tokio::test] +async fn upload_failure_does_not_retain_private_server_response() { + let secret = "private-transcript-and-auth-token"; + let (base, request) = mock_listing("500 Internal Server Error", secret).await; + let body = UploadBody { + title: Some(secret), + source: "local", + host: None, + cwd: None, + model: None, + created_at: String::new(), + updated_at: String::new(), + messages: &[], + }; + let error = upload(&reqwest::Client::new(), &base, secret, "test", &body) + .await + .unwrap_err(); + assert!(error.to_string().contains("500")); + assert!(!format!("{error:#}").contains(secret)); + request.await.unwrap(); +} + struct TestEnv(Vec<(&'static str, Option)>); impl TestEnv { From e193b9bd5efce7c198182750329209e2d158f04b Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:31:22 -0800 Subject: [PATCH 027/236] Instrument server history paging and bootstrap timing outcomes --- .../jcode-app-core/src/server/client_state.rs | 167 +++++------------ .../src/server/history_paging.rs | 101 ++++++++++ .../src/server/history_timing.rs | 176 ++++++++++++++++++ 3 files changed, 324 insertions(+), 120 deletions(-) create mode 100644 crates/jcode-app-core/src/server/history_paging.rs create mode 100644 crates/jcode-app-core/src/server/history_timing.rs diff --git a/crates/jcode-app-core/src/server/client_state.rs b/crates/jcode-app-core/src/server/client_state.rs index d1ed3c0656e..3b44d583f00 100644 --- a/crates/jcode-app-core/src/server/client_state.rs +++ b/crates/jcode-app-core/src/server/client_state.rs @@ -25,35 +25,10 @@ use tokio::sync::{Mutex, RwLock}; const ATTACH_MODEL_PREFETCH_DEBOUNCE_SECS: u64 = 15; const RELOAD_RESTORE_MARKER_MAX_AGE: Duration = Duration::from_secs(60); -/// Resume chrome must not queue behind a multi-megabyte History frame. Write -/// the small, session-bound authoritative snapshot before acquiring that frame's -/// writer lock. This does not require the agent lock, even during a live turn. -pub(super) async fn send_resume_todos( - session_id: &str, - writer: &Arc>, -) -> Result<()> { - let started = Instant::now(); - let payload = serde_json::json!({ - "todos": crate::todo::load_todos(session_id).unwrap_or_else(|error| { - crate::logging::warn(&format!("Failed to load resume todos for {session_id}: {error}")); - Vec::new() - }), - "plan": crate::todo::load_plan(session_id).unwrap_or_default(), - "goals": crate::todo::load_goals(session_id).unwrap_or_default(), - }); - let event = ServerEvent::Todos { - session_id: session_id.to_owned(), - payload: payload.to_string(), - }; - let json = encode_event(&event); - writer.lock().await.write_all(json.as_bytes()).await?; - crate::logging::info(&format!( - "[TIMING] resume todos: session={session_id}, bytes={}, total={}ms", - json.len(), - started.elapsed().as_millis(), - )); - Ok(()) -} +#[path = "history_timing.rs"] +mod history_timing; +use history_timing::Timing; +pub(super) use history_timing::send_resume_todos; /// Do not probe any identity until telemetry is enabled. Only the provider /// may identify its effective OAuth credential. An API-key route must not expose the email @@ -213,6 +188,7 @@ pub(super) async fn handle_get_history( supports_turn_identity: bool, history_base: Option, ) -> Result<()> { + let mut timing = Timing::new("history", Some(id), client_session_id); let history_start = Instant::now(); let activity = session_activity_snapshot(client_connections, client_session_id, client_is_processing) @@ -226,7 +202,8 @@ pub(super) async fn handle_get_history( "handle_get_history: session {} busy, falling back to persisted remote-startup snapshot", client_session_id )); - send_history_from_persisted_session( + timing.stage("persisted"); + let result = send_history_from_persisted_session( id, client_session_id, provider, @@ -241,7 +218,9 @@ pub(super) async fn handle_get_history( supports_turn_identity, history_base, ) - .await?; + .await; + timing.result(&result); + result?; crate::logging::info(&format!( "[TIMING] handle_get_history: session={}, persisted_fallback total={}ms", client_session_id, @@ -250,7 +229,8 @@ pub(super) async fn handle_get_history( return Ok(()); }; - send_history_with_guard( + timing.stage("live"); + let result = send_history_with_guard( id, client_session_id, agent_guard, @@ -267,7 +247,9 @@ pub(super) async fn handle_get_history( supports_turn_identity, history_base, ) - .await?; + .await; + timing.result(&result); + result?; let send_history_ms = history_start.elapsed().as_millis(); let prefetch_start = Instant::now(); @@ -484,86 +466,9 @@ pub(super) async fn handle_get_model_catalog( Ok(()) } -pub(super) async fn handle_get_compacted_history( - id: u64, - session_id: &str, - agent: &Arc>, - writer: &Arc>, - visible_messages: usize, - recent_history: bool, -) -> Result<()> { - let started = Instant::now(); - let (messages, images, compacted_info, source) = match agent.try_lock() { - Ok(agent_guard) => { - let (messages, images, info) = if recent_history { - let (messages, images) = agent_guard.get_history_and_rendered_images(); - let (messages, images, info) = - recent_history_window(messages, images, visible_messages); - (messages, images, Some(info)) - } else { - agent_guard.get_history_and_rendered_images_with_compacted_history(visible_messages) - }; - (messages, images, info, "live") - } - Err(_) => { - let session = crate::session::Session::load_for_remote_startup(session_id) - .or_else(|_| crate::session::Session::load_startup_stub(session_id))?; - let (rendered_messages, images, info) = if recent_history { - let (messages, images) = crate::session::render_messages_and_images(&session); - (messages, images, None) - } else { - crate::session::render_messages_and_images_with_compacted_history( - &session, - visible_messages, - ) - }; - let messages = rendered_messages - .into_iter() - .map(rendered_to_history_message) - .collect(); - let (messages, images, info) = if recent_history { - let (messages, images, info) = - recent_history_window(messages, images, visible_messages); - (messages, images, Some(info)) - } else { - (messages, images, info) - }; - (messages, images, info, "persisted") - } - }; - - let compacted_info = compacted_info.unwrap_or(crate::session::RenderedCompactedHistoryInfo { - total_messages: 0, - visible_messages: 0, - remaining_messages: 0, - hidden_user_prompts: 0, - }); - crate::logging::info(&format!( - "[TIMING] get_compacted_history: session={}, source={}, requested={}, visible={}, remaining={}, messages={}, total={}ms", - session_id, - source, - visible_messages, - compacted_info.visible_messages, - compacted_info.remaining_messages, - messages.len(), - started.elapsed().as_millis(), - )); - - write_event( - writer, - &ServerEvent::CompactedHistory { - id, - session_id: session_id.to_string(), - messages, - images, - compacted_total: compacted_info.total_messages, - compacted_visible: compacted_info.visible_messages, - compacted_remaining: compacted_info.remaining_messages, - compacted_hidden_prompts: compacted_info.hidden_user_prompts, - }, - ) - .await -} +#[path = "history_paging.rs"] +mod history_paging; +pub(super) use history_paging::handle_get_compacted_history; /// Window canonical rendered entries, not raw provider messages. Images follow /// their rendered entry and the synthetic marker occupies its own index. @@ -893,6 +798,7 @@ async fn send_history_from_persisted_session( supports_turn_identity: bool, history_base: Option, ) -> Result<()> { + let mut timing = Timing::new("persisted_history", Some(id), session_id); let loaded = super::run_blocking_io(|| { crate::session::Session::load_for_remote_startup(session_id) .or_else(|_| crate::session::Session::load_startup_stub(session_id)) @@ -913,8 +819,12 @@ async fn send_history_from_persisted_session( // Do not save this synthetic snapshot or mask other I/O errors. Session::create_with_id(session_id.to_string(), None, None) } - Err(error) => return Err(error), + Err(error) => { + timing.result(&Err::<(), _>(())); + return Err(error); + } }; + timing.stage("render"); let token_usage_totals = session.token_usage_totals(); let (rendered_messages, images) = crate::session::render_messages_and_images(&session); // Extract the small metadata fields we need, then drop the full Session @@ -1014,15 +924,23 @@ async fn send_history_from_persisted_session( applets: crate::applets::snapshot_for_session(session_id).unwrap_or_default(), }; + timing.stage("encode"); let json = encode_event(&history_event); + timing.stage("writer_lock"); let mut writer_guard = writer.lock().await; + timing.stage("write"); if supports_turn_identity && let Some(state) = history_turn_state(sessions, session_id) { - writer_guard + let sent = writer_guard .write_all(encode_event(&state).as_bytes()) - .await?; + .await; + if sent.is_err() { + timing.result(&sent); + } + sent?; } - writer_guard.write_all(json.as_bytes()).await?; - Ok(()) + let result = writer_guard.write_all(json.as_bytes()).await; + timing.result(&result); + result.map_err(Into::into) } /// How long the bootstrap History payload may wait on the model-name catalog. @@ -1076,6 +994,7 @@ async fn send_history_with_guard( supports_turn_identity: bool, history_base: Option, ) -> Result<()> { + let mut timing = Timing::new("live_history", Some(id), session_id); let history_start = Instant::now(); let catalog_provider = include_model_catalog.then(|| agent_guard.provider_handle()); let ( @@ -1326,6 +1245,7 @@ async fn send_history_with_guard( applets, }; let build_ms = build_start.elapsed().as_millis(); + timing.stage("encode"); let encode_start = Instant::now(); let json = encode_event(&history_event); // Free the structured event as soon as the wire bytes exist so only ~1x @@ -1333,16 +1253,23 @@ async fn send_history_with_guard( drop(history_event); let json_len = json.len(); let encode_ms = encode_start.elapsed().as_millis(); + timing.stage("writer_lock"); let writer_lock_start = Instant::now(); let mut writer_guard = writer.lock().await; let writer_lock_ms = writer_lock_start.elapsed().as_millis(); + timing.stage("write"); let write_start = Instant::now(); if supports_turn_identity && let Some(state) = history_turn_state(sessions, session_id) { - writer_guard + let sent = writer_guard .write_all(encode_event(&state).as_bytes()) - .await?; + .await; + if sent.is_err() { + timing.result(&sent); + } + sent?; } let result = writer_guard.write_all(json.as_bytes()).await; + timing.result(&result); drop(writer_guard); // Release the serialized payload before any further work (logging below // only needs the captured length). diff --git a/crates/jcode-app-core/src/server/history_paging.rs b/crates/jcode-app-core/src/server/history_paging.rs new file mode 100644 index 00000000000..8754d05fcc1 --- /dev/null +++ b/crates/jcode-app-core/src/server/history_paging.rs @@ -0,0 +1,101 @@ +use super::*; + +pub(in crate::server) async fn handle_get_compacted_history( + id: u64, + session_id: &str, + agent: &Arc>, + writer: &Arc>, + visible_messages: usize, + recent_history: bool, +) -> Result<()> { + let mut timing = Timing::new( + if recent_history { + "recent_history_page" + } else { + "compacted_history_page" + }, + Some(id), + session_id, + ); + let started = Instant::now(); + let (messages, images, compacted_info, source) = match agent.try_lock() { + Ok(agent_guard) => { + let (messages, images, info) = if recent_history { + let (messages, images) = agent_guard.get_history_and_rendered_images(); + let (messages, images, info) = + recent_history_window(messages, images, visible_messages); + (messages, images, Some(info)) + } else { + agent_guard.get_history_and_rendered_images_with_compacted_history(visible_messages) + }; + (messages, images, info, "live") + } + Err(_) => { + timing.stage("persisted_load"); + let loaded = crate::session::Session::load_for_remote_startup(session_id) + .or_else(|_| crate::session::Session::load_startup_stub(session_id)); + if loaded.is_err() { + timing.result(&loaded); + } + let session = loaded?; + timing.stage("render"); + let (rendered_messages, images, info) = if recent_history { + let (messages, images) = crate::session::render_messages_and_images(&session); + (messages, images, None) + } else { + crate::session::render_messages_and_images_with_compacted_history( + &session, + visible_messages, + ) + }; + let messages = rendered_messages + .into_iter() + .map(rendered_to_history_message) + .collect(); + let (messages, images, info) = if recent_history { + let (messages, images, info) = + recent_history_window(messages, images, visible_messages); + (messages, images, Some(info)) + } else { + (messages, images, info) + }; + (messages, images, info, "persisted") + } + }; + + let compacted_info = compacted_info.unwrap_or(crate::session::RenderedCompactedHistoryInfo { + total_messages: 0, + visible_messages: 0, + remaining_messages: 0, + hidden_user_prompts: 0, + }); + crate::logging::info(&format!( + "[TIMING] get_compacted_history: session={}, source={}, requested={}, visible={}, remaining={}, messages={}, total={}ms", + session_id, + source, + visible_messages, + compacted_info.visible_messages, + compacted_info.remaining_messages, + messages.len(), + started.elapsed().as_millis(), + )); + + timing.stage("encode"); + let event = ServerEvent::CompactedHistory { + id, + session_id: session_id.to_string(), + messages, + images, + compacted_total: compacted_info.total_messages, + compacted_visible: compacted_info.visible_messages, + compacted_remaining: compacted_info.remaining_messages, + compacted_hidden_prompts: compacted_info.hidden_user_prompts, + }; + let json = encode_event(&event); + timing.stage("writer_lock"); + let mut writer = writer.lock().await; + timing.stage("write"); + let result = writer.write_all(json.as_bytes()).await; + timing.result(&result); + result.map_err(Into::into) +} diff --git a/crates/jcode-app-core/src/server/history_timing.rs b/crates/jcode-app-core/src/server/history_timing.rs new file mode 100644 index 00000000000..44b5fe95f21 --- /dev/null +++ b/crates/jcode-app-core/src/server/history_timing.rs @@ -0,0 +1,176 @@ +use super::*; +use std::sync::atomic::{AtomicU64, Ordering}; + +static NEXT_OPERATION: AtomicU64 = AtomicU64::new(1); + +/// Only fixed stage labels and numeric metadata enter timing logs. Never errors or payloads. +pub(super) struct Timing { + operation_id: u64, + request_id: Option, + session_key: u64, + kind: &'static str, + stage: &'static str, + outcome: &'static str, + started: Instant, + stage_started: Instant, +} + +impl Timing { + pub(super) fn new(kind: &'static str, request_id: Option, session_id: &str) -> Self { + use std::hash::{Hash, Hasher}; + let mut session = std::collections::hash_map::DefaultHasher::new(); + session_id.hash(&mut session); + let started = Instant::now(); + Self { + operation_id: NEXT_OPERATION.fetch_add(1, Ordering::Relaxed), + request_id, + session_key: session.finish(), + kind, + stage: "prepare", + outcome: "dropped", + started, + stage_started: started, + } + } + + fn fields(&self, stage: &str, outcome: &str, duration_ms: u128) -> String { + format!( + "[CLOUD_TIMING] operation=server_history session_key={:016x} operation_id={} request_id={} kind={} stage={} outcome={} duration_ms={} total_duration_ms={}", + self.session_key, + self.operation_id, + self.request_id + .map_or_else(|| "none".into(), |id| id.to_string()), + self.kind, + stage, + outcome, + duration_ms, + self.started.elapsed().as_millis(), + ) + } + + pub(super) fn stage(&mut self, stage: &'static str) { + crate::logging::info(&self.fields( + self.stage, + "ok", + self.stage_started.elapsed().as_millis(), + )); + self.stage = stage; + self.stage_started = Instant::now(); + } + + fn fallback(&self, result: std::result::Result) -> T { + match result { + Ok(value) => value, + Err(_) => { + crate::logging::info(&self.fields( + "load", + "fallback", + self.started.elapsed().as_millis(), + )); + T::default() + } + } + } + + pub(super) fn result(&mut self, result: &std::result::Result) { + self.outcome = if result.is_ok() { "ok" } else { "failed" }; + } +} + +impl Drop for Timing { + fn drop(&mut self) { + crate::logging::info(&self.fields( + self.stage, + self.outcome, + self.stage_started.elapsed().as_millis(), + )); + } +} + +/// Send authoritative todo chrome before the potentially large history frame. +pub(in crate::server) async fn send_resume_todos( + session_id: &str, + writer: &Arc>, +) -> Result<()> { + let mut timing = Timing::new("resume_todos", None, session_id); + let todos = crate::todo::load_todos(session_id); + let plan = crate::todo::load_plan(session_id); + let goals = crate::todo::load_goals(session_id); + timing.stage("encode"); + let payload = serde_json::json!({ + "todos": timing.fallback(todos), + "plan": timing.fallback(plan), + "goals": timing.fallback(goals), + }); + let json = encode_event(&ServerEvent::Todos { + session_id: session_id.to_owned(), + payload: payload.to_string(), + }); + timing.stage("writer_lock"); + let mut writer = writer.lock().await; + timing.stage("write"); + let result = writer.write_all(json.as_bytes()).await; + timing.result(&result); + result.map_err(Into::into) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn resume_todos_reports_disconnected_writer() { + let (stream, peer) = crate::transport::stream_pair().unwrap(); + let (_, write) = stream.into_split(); + drop(peer); + let writer = Arc::new(Mutex::new(write)); + assert!( + send_resume_todos("timing_disconnected_writer", &writer) + .await + .is_err() + ); + } + + #[test] + fn stages_keep_correlation_and_do_not_mark_cancellation_successful() { + let mut timing = Timing::new("history", Some(9), "private-session"); + let operation_id = timing.operation_id; + timing.stage("writer_lock"); + assert_eq!(timing.operation_id, operation_id); + assert_eq!(timing.stage, "writer_lock"); + assert_eq!(timing.outcome, "dropped"); + let todos = Timing::new("resume_todos", None, "private-session"); + assert_eq!(timing.session_key, todos.session_key); + assert!( + !timing + .fields("write", "failed", 1) + .contains("private-session") + ); + assert_ne!( + timing.session_key, + Timing::new("history", Some(9), "other").session_key + ); + } + + #[test] + fn timing_fields_are_correlated_numeric_and_content_free() { + let mut timing = Timing::new("history", Some(42), "secret session"); + let fields = timing.fields("writer_lock", "ok", 7); + assert!( + fields + .contains("request_id=42 kind=history stage=writer_lock outcome=ok duration_ms=7") + ); + assert!(fields.contains("operation_id=")); + assert!(!fields.contains("session_id=")); + assert_eq!(timing.outcome, "dropped"); + timing.result(&Err::<(), _>("secret credential and content")); + assert_eq!(timing.outcome, "failed"); + assert!(!timing.fields("write", timing.outcome, 0).contains("secret")); + timing.result(&Ok::<_, ()>(())); + assert_eq!(timing.outcome, "ok"); + assert_ne!( + timing.operation_id, + Timing::new("history", Some(42), "secret session").operation_id + ); + } +} From bff51ea19eb1e96657d482a6f36ff494eb5a291a Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:32:07 -0800 Subject: [PATCH 028/236] Instrument bounded cloud previews with safe stage timings --- .../src/tui/session_picker/cloud_preview.rs | 176 +++++++++++------- .../tui/session_picker/cloud_preview/cache.rs | 46 +++-- .../cloud_preview/projection_tests.rs | 71 ++++++- .../session_picker/cloud_preview/remote.py | 36 +++- 4 files changed, 237 insertions(+), 92 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs index 565b9d906e8..b452f0fac93 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs @@ -8,6 +8,49 @@ mod projection_tests; type CloudSnapshot = (Session, Option); +/// Static labels only: never attach hostnames, session IDs, paths or errors. +pub(super) struct StageTimer { + stage: &'static str, + outcome: &'static str, + started: std::time::Instant, +} + +impl StageTimer { + pub(super) fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "error", + started: std::time::Instant::now(), + } + } + + pub(super) fn outcome(&mut self, outcome: &'static str) { + self.outcome = outcome; + } +} + +impl Drop for StageTimer { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage={} outcome={} duration_ms={}", + self.stage, + self.outcome, + self.started.elapsed().as_millis() + )); + } +} + +fn stage_result(result: Result, stage: &'static str) -> Option { + let mut timer = StageTimer::new(stage); + match result { + Ok(value) => { + timer.outcome("success"); + Some(value) + } + Err(_) => None, + } +} + pub(in crate::tui::session_picker) const REMOTE_PROJECTOR: &str = include_str!("cloud_preview/remote.py"); @@ -16,6 +59,7 @@ pub(in crate::tui::session_picker) fn projection_script(mode: &str) -> String { } pub(super) fn load(session_id: &str, host: &str) -> Option { + let mut timer = StageTimer::new("load"); if session_id.is_empty() || !session_id .bytes() @@ -23,6 +67,7 @@ pub(super) fn load(session_id: &str, host: &str) -> Option { || host.is_empty() || host.starts_with('-') { + timer.outcome("skipped_invalid_target"); return None; } let epoch = crate::storage::read_session_lease(session_id) @@ -31,7 +76,11 @@ pub(super) fn load(session_id: &str, host: &str) -> Option { let path = cache::path(session_id, host, epoch); let id = session_id.to_owned(); let host = host.to_owned(); - load_cached_or_fetch(session_id, path, move || fetch_bytes(&id, &host)) + let result = load_cached_or_fetch(session_id, path, move || fetch_bytes(&id, &host)); + if result.is_some() { + timer.outcome("success"); + } + result } /// A cloud cache is a preview replica, never a canonical local session. Even a @@ -42,16 +91,14 @@ fn load_cached_or_fetch( path: Option, fetch: impl FnOnce() -> Option> + Send + 'static, ) -> Option { - let started = std::time::Instant::now(); + let mut timer = StageTimer::new("cache_lookup"); if let Some(path) = path.as_deref() && let Some(bytes) = cache::read(path) && let Some(snapshot) = parse_for_session(&bytes, session_id) { - crate::logging::info(&format!( - "CLOUD_PREVIEW cache=hit session={session_id} bytes={} elapsed_ms={}", - bytes.len(), - started.elapsed().as_millis(), - )); + timer.outcome("hit"); + let mut refresh_timer = StageTimer::new("refresh_schedule"); + refresh_timer.outcome("skipped_fresh_or_in_flight"); if !cache::fresh(path) && let Some(guard) = cache::begin_refresh(path) { @@ -63,10 +110,12 @@ fn load_cached_or_fetch( let _guard = guard; fetch_and_cache(&id, Some(&path), fetch); }); - preview_result(refresh, "cloud refresh worker"); + refresh_timer.outcome(if refresh.is_ok() { "success" } else { "error" }); } return Some(snapshot); } + timer.outcome("miss"); + drop(timer); fetch_and_cache(session_id, path.as_deref(), fetch) } @@ -75,64 +124,62 @@ fn fetch_and_cache( path: Option<&std::path::Path>, fetch: impl FnOnce() -> Option>, ) -> Option { - let started = std::time::Instant::now(); + let mut timer = StageTimer::new("refresh"); + let mut network = StageTimer::new("fetch"); let bytes = fetch()?; - let fetched = started.elapsed(); + network.outcome("success"); + drop(network); // Validate the full snapshot + journal before replacing the last good copy. let snapshot = parse_for_session(&bytes, session_id)?; - if let Some(path) = path - && let Err(error) = cache::write(path, &bytes) - { - crate::logging::debug(&format!("Cloud preview cache write unavailable: {error}")); + if let Some(path) = path { + if cache::write(path, &bytes).is_err() { + timer.outcome("success_cache_unavailable"); + return Some(snapshot); + } + } else { + let mut skipped = StageTimer::new("cache_write"); + skipped.outcome("skipped_unavailable"); } - crate::logging::info(&format!( - "CLOUD_PREVIEW cache=refresh session={session_id} bytes={} fetch_ms={} total_ms={}", - bytes.len(), - fetched.as_millis(), - started.elapsed().as_millis(), - )); + timer.outcome("success"); Some(snapshot) } fn parse_for_session(bytes: &[u8], session_id: &str) -> Option { + let mut timer = StageTimer::new("parse"); let snapshot = parse_preview(bytes)?; - (snapshot.0.id == session_id).then_some(snapshot) + if snapshot.0.id != session_id { + timer.outcome("identity_mismatch"); + return None; + } + timer.outcome("success"); + Some(snapshot) } fn fetch_bytes(session_id: &str, host: &str) -> Option> { - let config = crate::storage::jcode_dir() - .map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - )? - .join("cloud/ssh_config"); + let mut timer = StageTimer::new("ssh"); + let config = stage_result(crate::storage::jcode_dir(), "ssh_config")?.join("cloud/ssh_config"); let command = crate::tui::cloud_git::compressed_script_command( Some(host), config.is_file().then_some(config.as_path()), session_id, &projection_script("preview"), ); - let runtime = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - )?; + let runtime = stage_result( + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build(), + "ssh_runtime", + )?; let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output( command, Duration::from_secs(15), cache::MAX_BYTES, ))?; if !status.success() { + timer.outcome("exit_failure"); return None; } + timer.outcome("success"); Some(bytes) } @@ -148,11 +195,17 @@ fn parse_preview( .map(|offset| first + 1 + offset); let session = parse_transcript(&bytes[..second.unwrap_or(bytes.len())])?; let telemetry = second.and_then(|split| { + let mut timer = StageTimer::new("telemetry_parse"); let bytes = &bytes[split + 1..]; if bytes.is_empty() { + timer.outcome("skipped_absent"); None } else { - preview_result(serde_json::from_slice(bytes), "remote telemetry decoding") + let result = stage_result(serde_json::from_slice(bytes), "telemetry_decode"); + if result.is_some() { + timer.outcome("success"); + } + result } }); Some((session, telemetry)) @@ -160,39 +213,20 @@ fn parse_preview( fn parse_transcript(bytes: &[u8]) -> Option { let split = bytes.iter().position(|b| *b == 0)?; - let dir = tempfile::tempdir().map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - )?; + let dir = stage_result(tempfile::tempdir(), "parse_tempdir")?; let path = dir.path().join("preview.json"); - std::fs::write(&path, &bytes[..split]).map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, + stage_result( + std::fs::write(&path, &bytes[..split]), + "parse_snapshot_write", )?; - std::fs::write( - crate::session::session_journal_path_from_snapshot(&path), - &bytes[split + 1..], - ) - .map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, + stage_result( + std::fs::write( + crate::session::session_journal_path_from_snapshot(&path), + &bytes[split + 1..], + ), + "parse_journal_write", )?; - Session::load_from_path(&path).map_or_else( - |error| { - crate::logging::debug(&format!("Cloud preview unavailable: {error}")); - None - }, - Some, - ) + stage_result(Session::load_from_path(&path), "parse_hydrate") } #[cfg(test)] diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs index 7c077ff63dc..2d2b3436a23 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs @@ -13,12 +13,13 @@ const CACHE_BUDGET: u64 = 24 * 1024 * 1024; const TTL: Duration = Duration::from_secs(15); pub(super) fn path(session_id: &str, host: &str, epoch: u64) -> Option { - let root = super::super::preview_result(crate::storage::jcode_dir(), "cache directory")?; + let mut timer = super::StageTimer::new("cache_scope"); + let root = super::stage_result(crate::storage::jcode_dir(), "cache_directory")?; let owner = crate::subscription_catalog::configured_api_key().unwrap_or_default(); let endpoint = crate::subscription_api::configured_api_base(); - Some(scoped_key_path( - &root, session_id, host, epoch, &owner, &endpoint, - )) + let path = scoped_key_path(&root, session_id, host, epoch, &owner, &endpoint); + timer.outcome("success"); + Some(path) } #[cfg(test)] @@ -57,33 +58,51 @@ fn scoped_key_path( } pub(super) fn read(path: &Path) -> Option> { + let mut timer = super::StageTimer::new("cache_read"); let file = match File::open(path) { Ok(file) => file, - Err(error) if error.kind() == io::ErrorKind::NotFound => return None, - Err(error) => return super::super::preview_result(Err::, _>(error), "cache open"), + Err(error) if error.kind() == io::ErrorKind::NotFound => { + timer.outcome("miss"); + return None; + } + Err(_) => return None, }; - if super::super::preview_result(file.metadata(), "cache metadata")?.len() > MAX_BYTES as u64 { + if super::stage_result(file.metadata(), "cache_metadata")?.len() > MAX_BYTES as u64 { + timer.outcome("oversized"); return None; } // Do not trust metadata for allocation or bounds: the file can grow concurrently. let mut bytes = Vec::new(); - super::super::preview_result( + super::stage_result( file.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes), - "cache read", + "cache_read_io", )?; + timer.outcome(if bytes.len() <= MAX_BYTES { + "hit" + } else { + "oversized" + }); (bytes.len() <= MAX_BYTES).then_some(bytes) } pub(super) fn fresh(path: &Path) -> bool { - super::super::preview_result( + let mut timer = super::StageTimer::new("cache_freshness"); + let fresh = super::stage_result( fs::metadata(path).and_then(|metadata| metadata.modified()), - "cache freshness", + "cache_timestamp", ) - .and_then(|modified| super::super::preview_result(modified.elapsed(), "cache timestamp")) - .is_some_and(|age| age < TTL) + .and_then(|modified| super::stage_result(modified.elapsed(), "cache_age")) + .is_some_and(|age| age < TTL); + timer.outcome(if fresh { + "fresh" + } else { + "stale_or_unavailable" + }); + fresh } pub(super) fn write(path: &Path, bytes: &[u8]) -> io::Result<()> { + let mut timer = super::StageTimer::new("cache_write"); if bytes.len() > MAX_BYTES { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -108,6 +127,7 @@ pub(super) fn write(path: &Path, bytes: &[u8]) -> io::Result<()> { temporary.as_file().sync_all()?; temporary.persist(path).map_err(|error| error.error)?; prune(parent, path, CACHE_BUDGET); + timer.outcome("success"); Ok(()) } diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs index 7e200d2f84a..7f987591234 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs @@ -31,6 +31,59 @@ use super::*; use std::io::Write; +#[test] +fn remote_timing_errors_and_skips_are_safe_and_do_not_change_wire_format() { + let home = tempfile::tempdir().unwrap(); + let private_id = "private_session_identifier"; + let preview = projection_output(home.path(), "preview", private_id); + assert!(!preview.status.success()); + assert!(preview.stdout.is_empty()); + let inventory = run_projection( + home.path(), + "inventory", + &format!("{private_id}\n../private"), + ); + assert!(inventory.status.success()); + let payload: serde_json::Value = serde_json::from_slice(&inventory.stdout).unwrap(); + assert_eq!(payload["sessions"], serde_json::json!([])); + for output in [&preview, &inventory] { + let logs = String::from_utf8_lossy(&output.stderr); + assert!(logs.contains("stage=projection outcome=error duration_ms=")); + assert!(!logs.contains(private_id)); + assert!(!logs.contains(home.path().to_str().unwrap())); + assert!(!logs.contains("Traceback")); + for line in logs.lines() { + let fields: Vec<_> = line.split_whitespace().collect(); + assert_eq!(fields.len(), 4, "{line}"); + assert_eq!(fields[0], "[CLOUD_TIMING]"); + assert!(fields[1].starts_with("stage=")); + assert!(fields[2].starts_with("outcome=")); + fields[3] + .strip_prefix("duration_ms=") + .unwrap() + .parse::() + .unwrap(); + } + } + assert!(String::from_utf8_lossy(&inventory.stderr).contains("outcome=skipped_invalid_target")); + std::fs::create_dir(home.path().join("sessions")).unwrap(); + let session = Session::create_with_id(private_id.into(), None, Some("private title".into())); + std::fs::write( + home.path() + .join("sessions") + .join(format!("{private_id}.json")), + serde_json::to_vec(&session).unwrap(), + ) + .unwrap(); + let success = run_projection(home.path(), "preview", private_id); + assert!(success.status.success()); + assert!(parse_for_session(&success.stdout, private_id).is_some()); + let logs = String::from_utf8_lossy(&success.stderr); + assert!(logs.contains("stage=projection outcome=success duration_ms=")); + assert!(!logs.contains(private_id)); + assert!(!logs.contains("private title")); +} + #[test] fn remote_scanner_handles_escaped_nested_strings_and_rejects_truncation() { let checks = r#" @@ -83,6 +136,16 @@ for malformed in (b'{', b'[', b'{"x":"unterminated}', b'["escaped\\"', b'{]', b' } fn run_projection(home: &std::path::Path, mode: &str, id: &str) -> std::process::Output { + let output = projection_output(home, mode, id); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + output +} + +fn projection_output(home: &std::path::Path, mode: &str, id: &str) -> std::process::Output { let mut child = std::process::Command::new("python3") .args(["-", mode, id]) .env("JCODE_HOME", home) @@ -98,13 +161,7 @@ fn run_projection(home: &std::path::Path, mode: &str, id: &str) -> std::process: .unwrap() .write_all(REMOTE_PROJECTOR.as_bytes()) .unwrap(); - let output = child.wait_with_output().unwrap(); - assert!( - output.status.success(), - "{}", - String::from_utf8_lossy(&output.stderr) - ); - output + child.wait_with_output().unwrap() } #[test] diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py index 67261a9cb15..244951988f7 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py @@ -7,6 +7,28 @@ import re import socket import sys +import time + + +def timing(stage, outcome, started): + # Fixed labels only. Never print exceptions, identifiers or projected data. + print('[CLOUD_TIMING] stage={} outcome={} duration_ms={}'.format( + stage, outcome, int((time.monotonic() - started) * 1000)), file=sys.stderr) + + +def timed(stage): + def decorate(function): + def run(*args, **kwargs): + started = time.monotonic() + outcome = 'error' + try: + result = function(*args, **kwargs) + outcome = 'success' + return result + finally: + timing(stage, outcome, started) + return run + return decorate # Consume complete strings in C, including escaped quotes and backslashes. STRING = re.compile(br'"[^"\\]*(?:\\.[^"\\]*)*"', re.DOTALL) @@ -133,6 +155,7 @@ def recent_messages(data, start, end, messages, ranges=None): messages.append(message) +@timed('projection') def project(path, preview): session = {} messages = collections.deque(maxlen=MAX_MESSAGES) @@ -175,12 +198,15 @@ def project(path, preview): return session +@timed('remote_request') def main(): mode, ids = sys.argv[1:3] base = Path(os.environ.get('JCODE_HOME', str(Path.home() / '.jcode'))) result = [] for sid in ids.splitlines(): + started = time.monotonic() if not sid or not all(c.isascii() and (c.isalnum() or c in '_-') for c in sid): + timing('inventory_entry', 'skipped_invalid_target', started) if mode == 'preview': raise ValueError('invalid session id') continue @@ -204,13 +230,21 @@ def main(): lease_path = base / 'session_leases' / (sid + '.json') lease = json.loads(lease_path.read_bytes()) if lease_path.exists() else {} if lease.get('away_host'): + timing('inventory_entry', 'skipped_away', started) continue result.append({'session_id': sid, 'epoch': max(session.get('migration_epoch', 0), lease.get('epoch', 0)), 'cwd': session.get('working_dir'), 'title': session.get('custom_title') if session.get('custom_title') is not None else session.get('title'), 'updated_at': session.get('updated_at')}) + timing('inventory_entry', 'success', started) except (OSError, ValueError, TypeError, IndexError): + timing('inventory_entry' if mode != 'preview' else 'remote_preview', 'error', started) if mode == 'preview': raise print(json.dumps({'host': socket.gethostname(), 'sessions': result})) if __name__ == '__main__': - main() + try: + main() + except Exception: + # Tracebacks include paths and may quote private input. The stage timer + # already records failure, and the exit status preserves the contract. + sys.exit(1) From ae9608777f2e7ee6999d7290355254936f646a5e Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:34:58 -0800 Subject: [PATCH 029/236] Accept notify_auth_changed from one-shot login clients so Desktop gets new models --- .../src/server/client_lightweight_control.rs | 13 ++++++ .../src/server/provider_control.rs | 46 +++++++++++++++++++ crates/jcode-protocol/src/lib.rs | 4 ++ crates/jcode-protocol/src/protocol_tests.rs | 11 +++++ 4 files changed, 74 insertions(+) diff --git a/crates/jcode-app-core/src/server/client_lightweight_control.rs b/crates/jcode-app-core/src/server/client_lightweight_control.rs index 647ff387e47..ee55ed3d3d4 100644 --- a/crates/jcode-app-core/src/server/client_lightweight_control.rs +++ b/crates/jcode-app-core/src/server/client_lightweight_control.rs @@ -337,6 +337,19 @@ pub(super) async fn handle_lightweight_control_request( ) .await; } + Request::NotifyAuthChanged { + id, provider, auth, .. + } => { + super::provider_control::handle_unbound_notify_auth_changed( + id, + provider, + auth, + provider_template, + sessions, + &client_event_tx, + ) + .await; + } Request::NotifySession { id, session_id, diff --git a/crates/jcode-app-core/src/server/provider_control.rs b/crates/jcode-app-core/src/server/provider_control.rs index 5252b286c49..5001d3407ff 100644 --- a/crates/jcode-app-core/src/server/provider_control.rs +++ b/crates/jcode-app-core/src/server/provider_control.rs @@ -1144,6 +1144,52 @@ pub(super) async fn handle_notify_auth_changed( let _ = client_event_tx.send(ServerEvent::Done { id }); } +/// A one-shot client (the SDK login flow, `jcode login` from another shell) +/// reports new credentials without subscribing to a session. Refresh every +/// live session's provider so subscribed clients, including Desktop chats and +/// drafts that never asked for a session-local switch, receive the new catalog +/// through `ModelsUpdated`. The post-login model switch stays session-local. +pub(super) async fn handle_unbound_notify_auth_changed( + id: u64, + provider_hint: Option, + auth: Option, + provider_template: &Arc, + sessions: &SessionAgents, + client_event_tx: &mpsc::UnboundedSender, +) { + crate::auth::AuthStatus::invalidate_cache(); + let activation = crate::auth::lifecycle::activate_auth_change(&AuthActivationRequest::new( + provider_hint, + auth, + )); + if let Some(provider_id) = activation.provider_id.as_deref() { + crate::provider::clear_provider_unavailable_for_account(provider_id); + } + provider_template.on_auth_changed(); + let agents: Vec>> = sessions.read().await.values().cloned().collect(); + let mut idle = Vec::new(); + let mut busy = Vec::new(); + for agent in agents { + let handle = agent.try_lock().map(|guard| guard.provider_handle()); + match handle { + Ok(provider) => idle.push(provider), + Err(_) => busy.push(agent), + } + } + let mut seen: Vec> = vec![Arc::clone(provider_template)]; + for provider in idle { + if !seen.iter().any(|existing| Arc::ptr_eq(existing, &provider)) { + provider.on_auth_changed_preserve_current_provider(); + seen.push(provider); + } + } + spawn_deferred_auth_refreshes(busy); + crate::bus::Bus::global().publish_models_updated(); + // Catalogs that refresh in the background announce themselves again with + // their own ModelsUpdated once the network fetch completes. + let _ = client_event_tx.send(ServerEvent::Done { id }); +} + #[cfg(test)] #[path = "provider_control_tests.rs"] mod provider_control_tests; diff --git a/crates/jcode-protocol/src/lib.rs b/crates/jcode-protocol/src/lib.rs index c6b0cf8b550..0e4ed761c8f 100644 --- a/crates/jcode-protocol/src/lib.rs +++ b/crates/jcode-protocol/src/lib.rs @@ -919,6 +919,10 @@ impl Request { | Request::AccountUsage { .. } | Request::InvalidateOpenAiUsage { .. } | Request::InvalidateAnthropicUsage { .. } + // One-shot login clients report new credentials without a + // session. Subscribed connections still take the + // session-local path that also switches their model. + | Request::NotifyAuthChanged { .. } | Request::NotifySession { .. } | Request::AppletAction { .. } | Request::CloseApplet { .. } diff --git a/crates/jcode-protocol/src/protocol_tests.rs b/crates/jcode-protocol/src/protocol_tests.rs index 25a55736551..f2d52ccc097 100644 --- a/crates/jcode-protocol/src/protocol_tests.rs +++ b/crates/jcode-protocol/src/protocol_tests.rs @@ -96,3 +96,14 @@ fn swarm_live_status_defaults_and_roundtrips() { runtime ); } + +#[test] +fn sdk_login_notify_is_accepted_without_subscribe() { + // The SDK login flow sends exactly this frame on a fresh connection. + // Requiring Subscribe first dropped it, so Desktop never saw new models. + let request: Request = serde_json::from_str( + r#"{"type":"notify_auth_changed","id":1,"provider":"claude"}"#, + ) + .unwrap(); + assert!(request.is_lightweight_control_request()); +} From a361d2b603a4d7c4df15401e736009198e541ae6 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:35:07 -0800 Subject: [PATCH 030/236] Instrument SDK cloud HTTP and host wait timings --- Cargo.lock | 1 + crates/jcode-sdk/Cargo.toml | 3 +- crates/jcode-sdk/src/cloud.rs | 143 +++++++++----- crates/jcode-sdk/src/cloud/timing.rs | 70 +++++++ crates/jcode-sdk/src/cloud/timing_tests.rs | 213 +++++++++++++++++++++ 5 files changed, 384 insertions(+), 46 deletions(-) create mode 100644 crates/jcode-sdk/src/cloud/timing.rs create mode 100644 crates/jcode-sdk/src/cloud/timing_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 711a95b54da..deb0c8c326a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4471,6 +4471,7 @@ dependencies = [ "anyhow", "jcode-applet-types", "jcode-harness-api", + "jcode-logging", "jcode-provider-metadata", "jcode-todo-policy", "jcode-transport", diff --git a/crates/jcode-sdk/Cargo.toml b/crates/jcode-sdk/Cargo.toml index 587a57d751a..6f6a5fb7926 100644 --- a/crates/jcode-sdk/Cargo.toml +++ b/crates/jcode-sdk/Cargo.toml @@ -22,6 +22,7 @@ url = "2" uuid = { version = "1", features = ["v4"] } # Jcode Cloud control plane (feature `cloud`). ureq = { version = "3", features = ["json"], optional = true } +jcode-logging = { path = "../jcode-logging", optional = true } # In-process SSH (feature `ssh-native`), for platforms without an `ssh` binary. # Keep RSA disabled: native SSH generates Ed25519 keys and uses pinned host keys. # russh 0.65 migrates the elliptic-curve stack to patched crypto-bigint 0.7. @@ -32,7 +33,7 @@ tokio = { version = "1", features = ["rt", "sync", "time", "net", "io-util", "ma [features] default = [] # Account sign-in (email code, browser/device flow) and host wake/provision. -cloud = ["dep:ureq"] +cloud = ["dep:ureq", "dep:jcode-logging"] # Pure-Rust SSH transport with pinned host keys; also enables `cloud::connect`. ssh-native = ["cloud", "dep:russh", "dep:rand", "dep:tokio"] diff --git a/crates/jcode-sdk/src/cloud.rs b/crates/jcode-sdk/src/cloud.rs index f4f5cd04e52..1fd5b22d555 100644 --- a/crates/jcode-sdk/src/cloud.rs +++ b/crates/jcode-sdk/src/cloud.rs @@ -23,6 +23,10 @@ use crate::{Error, ErrorKind, Result}; use serde::{Deserialize, Serialize}; use std::time::{Duration, Instant}; +mod timing; +#[cfg(test)] +mod timing_tests; + /// Production control plane. pub const DEFAULT_API_BASE: &str = "https://api.jcode.sh/v1"; /// How long [`CloudClient::wait_for_host`] waits for a host to provision. @@ -287,11 +291,13 @@ impl CloudClient { serde_json::json!({"device_code": login.device_code}), )?; if (200..300).contains(&status) { - let r: LoginReply = serde_json::from_value(body).map_err(|e| { - Error::new( - ErrorKind::UnexpectedReply, - format!("Unexpected reply from jcode.sh: {e}"), - ) + let r: LoginReply = timing::measured("reply_decode", "device_poll", || { + serde_json::from_value(body).map_err(|e| { + Error::new( + ErrorKind::UnexpectedReply, + format!("Unexpected reply from jcode.sh: {e}"), + ) + }) })?; return Ok(Some(self.account(r))); } @@ -367,29 +373,51 @@ impl CloudClient { progress: &dyn Fn(&str), cancelled: &dyn Fn() -> bool, ) -> Result { - let deadline = Instant::now() + PROVISION_DEADLINE; - let mut last = String::new(); - loop { - let reply = self.request_host_connect(account, public_key_openssh)?; - if reply.ready { - return CloudTarget::from_reply(&reply); - } - let message = reply - .message - .clone() - .unwrap_or_else(|| format!("Jcode Cloud machine is {}…", reply.state)); - if message != last { - progress(&message); - last = message; - } - if Instant::now() >= deadline { - return Err(Error::new( - ErrorKind::StartupTimeout, - "Jcode Cloud did not become ready in time. Retry to keep waiting.", - )); + self.wait_for_host_until( + account, + public_key_openssh, + progress, + cancelled, + Instant::now() + PROVISION_DEADLINE, + ) + } + + fn wait_for_host_until( + &self, + account: &Account, + public_key_openssh: &str, + progress: &dyn Fn(&str), + cancelled: &dyn Fn() -> bool, + deadline: Instant, + ) -> Result { + timing::measured("total", "wait_for_host", || { + let mut last = String::new(); + loop { + let reply = timing::measured("poll", "wait_for_host", || { + self.request_host_connect(account, public_key_openssh) + })?; + if reply.ready { + return CloudTarget::from_reply(&reply); + } + let message = reply + .message + .clone() + .unwrap_or_else(|| format!("Jcode Cloud machine is {}…", reply.state)); + if message != last { + progress(&message); + last = message; + } + if Instant::now() >= deadline { + return Err(Error::new( + ErrorKind::StartupTimeout, + "Jcode Cloud did not become ready in time. Retry to keep waiting.", + )); + } + timing::measured("poll_wait", "wait_for_host", || { + sleep_unless(self.poll_interval, cancelled) + })?; } - sleep_unless(self.poll_interval, cancelled)?; - } + }) } /// Register this device's APNs token so the account's jcode servers can @@ -488,6 +516,8 @@ impl CloudClient { api_key: Option<&str>, body: serde_json::Value, ) -> Result<(u16, serde_json::Value)> { + let operation = timing::operation(method, path); + let send_started = Instant::now(); let mut req = ureq::http::Request::builder() .method(method) .uri(self.url(path)) @@ -495,24 +525,43 @@ impl CloudClient { if let Some(k) = api_key { req = req.header("Authorization", format!("Bearer {k}")); } - let req = req - .body(body.to_string()) - .map_err(|e| Error::new(ErrorKind::Transport, e.to_string()))?; - let mut resp = self.agent().run(req).map_err(|e| { + let req = req.body(body.to_string()).map_err(|e| { + timing::record("http_send", operation, "error", send_started.elapsed()); + Error::new(ErrorKind::Transport, e.to_string()) + })?; + let response = self.agent().run(req); + let outcome = match &response { + Ok(resp) if resp.status().is_success() => "success", + Ok(_) => "error", + Err(ureq::Error::Timeout(_)) => "timeout", + Err(_) => "error", + }; + timing::record("http_send", operation, outcome, send_started.elapsed()); + let mut resp = response.map_err(|e| { Error::new( ErrorKind::ConnectFailed, format!("Could not reach jcode.sh: {e}"), ) })?; let status = resp.status().as_u16(); - let text = resp - .body_mut() - .read_to_string() - .map_err(|e| Error::new(ErrorKind::Transport, e.to_string()))?; - Ok(( - status, - serde_json::from_str(&text).unwrap_or(serde_json::Value::Null), - )) + let start = Instant::now(); + let text = resp.body_mut().read_to_string(); + timing::record( + "http_body", + operation, + timing::http_outcome(&text), + start.elapsed(), + ); + let text = text.map_err(|e| Error::new(ErrorKind::Transport, e.to_string()))?; + let start = Instant::now(); + let decoded = serde_json::from_str(&text); + timing::record( + "http_decode", + operation, + if decoded.is_ok() { "success" } else { "error" }, + start.elapsed(), + ); + Ok((status, decoded.unwrap_or(serde_json::Value::Null))) } fn post Deserialize<'de>>( @@ -523,11 +572,13 @@ impl CloudClient { ) -> Result { let (status, body) = self.post_raw(path, api_key, body)?; if (200..300).contains(&status) { - return serde_json::from_value(body).map_err(|e| { - Error::new( - ErrorKind::UnexpectedReply, - format!("Unexpected reply from jcode.sh: {e}"), - ) + return timing::measured("reply_decode", timing::operation("POST", path), || { + serde_json::from_value(body).map_err(|e| { + Error::new( + ErrorKind::UnexpectedReply, + format!("Unexpected reply from jcode.sh: {e}"), + ) + }) }); } let err = &body["error"]; @@ -643,7 +694,9 @@ mod tests { } /// Minimal one-shot HTTP server for flow tests. - fn serve(replies: Vec<(u16, &'static str)>) -> (String, std::sync::mpsc::Receiver) { + pub(super) fn serve( + replies: Vec<(u16, &'static str)>, + ) -> (String, std::sync::mpsc::Receiver) { use std::io::{Read, Write}; let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let base = format!("http://{}", listener.local_addr().unwrap()); diff --git a/crates/jcode-sdk/src/cloud/timing.rs b/crates/jcode-sdk/src/cloud/timing.rs new file mode 100644 index 00000000000..0a6b8c50b20 --- /dev/null +++ b/crates/jcode-sdk/src/cloud/timing.rs @@ -0,0 +1,70 @@ +//! Timing fields are closed labels, never request data or error messages. +use crate::{ErrorKind, Result}; +use std::time::{Duration, Instant}; + +pub(super) fn operation(method: &str, path: &str) -> &'static str { + match (method, path) { + ("POST", "/auth/email/start") => "email_start", + ("POST", "/auth/email/verify") => "email_verify", + ("POST", "/auth/device") => "device_start", + ("POST", "/auth/token") => "device_poll", + ("POST", "/cloud/host/connect") => "host_connect", + ("POST", "/push/devices") => "push_register", + ("DELETE", "/push/devices") => "push_unregister", + ("POST", "/push/badge") => "push_badge", + ("POST", "/push/send") => "push_send", + _ => "unknown", + } +} + +pub(super) fn record( + stage: &'static str, + operation: &'static str, + outcome: &'static str, + elapsed: Duration, +) { + let line = format!( + "[CLOUD_TIMING] stage={stage} operation={operation} outcome={outcome} duration_ms={}", + elapsed.as_millis() + ); + jcode_logging::info(&line); + #[cfg(test)] + EVENTS.with(|events| events.borrow_mut().push(line)); +} + +pub(super) fn http_outcome(result: &std::result::Result) -> &'static str { + match result { + Ok(_) => "success", + Err(ureq::Error::Timeout(_)) => "timeout", + Err(_) => "error", + } +} + +pub(super) fn measured( + stage: &'static str, + operation: &'static str, + run: impl FnOnce() -> Result, +) -> Result { + let start = Instant::now(); + let result = run(); + let outcome = match &result { + Ok(_) => "success", + Err(error) => match error.kind { + ErrorKind::Cancelled => "cancelled", + ErrorKind::Timeout | ErrorKind::StartupTimeout => "timeout", + _ => "error", + }, + }; + record(stage, operation, outcome, start.elapsed()); + result +} + +#[cfg(test)] +thread_local! { + static EVENTS: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; +} + +#[cfg(test)] +pub(super) fn take_events() -> Vec { + EVENTS.with(|events| std::mem::take(&mut *events.borrow_mut())) +} diff --git a/crates/jcode-sdk/src/cloud/timing_tests.rs b/crates/jcode-sdk/src/cloud/timing_tests.rs new file mode 100644 index 00000000000..abbc3dc5ac8 --- /dev/null +++ b/crates/jcode-sdk/src/cloud/timing_tests.rs @@ -0,0 +1,213 @@ +use super::*; + +fn account(base: String) -> Account { + Account { + api_key: "secret-account-key".into(), + email: Some("private@example.com".into()), + api_base: base, + } +} + +fn assert_event(events: &[String], stage: &str, operation: &str, outcome: &str) { + let prefix = format!( + "[CLOUD_TIMING] stage={stage} operation={operation} outcome={outcome} duration_ms=" + ); + assert!( + events.iter().any(|line| line + .strip_prefix(&prefix) + .is_some_and(|duration| duration.parse::().is_ok())), + "missing {prefix}: {events:?}" + ); + for line in events { + assert!(!line.contains("secret")); + assert!(!line.contains("private@example.com")); + assert!(!line.contains("ssh-ed25519")); + assert!(!line.contains("http://")); + } +} + +#[test] +fn successful_wait_records_http_poll_wait_and_total() { + let (base, _seen) = tests::serve(vec![ + (200, r#"{"ready":false,"message":"secret-progress"}"#), + ( + 200, + r#"{"ready":true,"address":"203.0.113.7","user":"u","host_keys":["ssh-ed25519 AAAA"]}"#, + ), + ]); + let mut cloud = CloudClient::new("secret-client"); + cloud.poll_interval = Duration::from_millis(1); + let target = cloud + .wait_for_host(&account(base), "ssh-ed25519 secret", &|_| {}, &|| false) + .unwrap(); + assert_eq!(target.user, "u"); + let events = timing::take_events(); + for stage in ["http_send", "http_body", "http_decode", "reply_decode"] { + assert_event(&events, stage, "host_connect", "success"); + } + for stage in ["poll", "poll_wait", "total"] { + assert_event(&events, stage, "wait_for_host", "success"); + } + assert_eq!( + events.iter().filter(|e| e.contains("stage=poll ")).count(), + 2 + ); +} + +#[test] +fn cancelled_wait_records_cancel_without_changing_error() { + let (base, _seen) = tests::serve(vec![(200, r#"{"ready":false}"#)]); + let error = CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| true) + .unwrap_err(); + assert_eq!(error.kind, ErrorKind::Cancelled); + let events = timing::take_events(); + assert_event(&events, "poll_wait", "wait_for_host", "cancelled"); + assert_event(&events, "total", "wait_for_host", "cancelled"); +} + +#[test] +fn expired_deadline_records_timeout_without_sleeping() { + let (base, _seen) = tests::serve(vec![(200, r#"{"ready":false}"#)]); + let error = CloudClient::new("t") + .wait_for_host_until(&account(base), "secret", &|_| {}, &|| false, Instant::now()) + .unwrap_err(); + assert_eq!(error.kind, ErrorKind::StartupTimeout); + let events = timing::take_events(); + assert_event(&events, "total", "wait_for_host", "timeout"); + assert!(!events.iter().any(|e| e.contains("stage=poll_wait "))); +} + +#[test] +fn http_error_and_decode_failure_are_safe() { + for (status, body, stage) in [ + (401, r#"{"error":{"message":"secret-error"}}"#, "http_send"), + (200, "secret-not-json", "http_decode"), + (200, r#"{"ready":"secret-wrong-type"}"#, "reply_decode"), + ] { + let (base, _seen) = tests::serve(vec![(status, body)]); + assert!( + CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| false) + .is_err() + ); + let events = timing::take_events(); + assert_event(&events, stage, "host_connect", "error"); + assert_event(&events, "poll", "wait_for_host", "error"); + assert_event(&events, "total", "wait_for_host", "error"); + } +} + +#[test] +fn invalid_ready_target_records_total_error() { + let (base, _seen) = tests::serve(vec![(200, r#"{"ready":true}"#)]); + assert!( + CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| false) + .is_err() + ); + assert_event(&timing::take_events(), "total", "wait_for_host", "error"); +} + +#[test] +fn unknown_paths_and_invalid_requests_never_leak_into_labels() { + assert_eq!( + timing::operation("POST", "/secret-account?api_key=secret"), + "unknown" + ); + let cloud = CloudClient::new("t").with_api_base("secret-invalid-uri"); + assert!( + cloud + .post_raw( + "/secret-account", + Some("secret"), + serde_json::json!({"secret":"secret"}) + ) + .is_err() + ); + assert_event(&timing::take_events(), "http_send", "unknown", "error"); +} + +#[test] +fn transport_failures_record_error() { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + drop(listener); + assert!( + CloudClient::new("t") + .wait_for_host(&account(base), "secret", &|_| {}, &|| false) + .is_err() + ); + let events = timing::take_events(); + assert_event(&events, "http_send", "host_connect", "error"); + assert_event(&events, "total", "wait_for_host", "error"); +} + +#[test] +fn http_timeout_classification_is_static() { + let result: std::result::Result<(), _> = Err(ureq::Error::Timeout(ureq::Timeout::Global)); + assert_eq!(timing::http_outcome(&result), "timeout"); +} + +#[test] +fn stalled_http_send_and_body_record_timeouts() { + use std::io::{Read, Write}; + for stage in ["http_send", "http_body"] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = [0; 4096]; + stream.read(&mut request).unwrap(); + if stage == "http_body" { + stream + .write_all( + b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\nConnection: close\r\n\r\n", + ) + .unwrap(); + } + std::thread::sleep(Duration::from_millis(150)); + }); + let mut cloud = CloudClient::new("t"); + cloud.timeout = Duration::from_millis(50); + let error = cloud + .request_host_connect(&account(base), "secret") + .unwrap_err(); + // Keep existing public error kinds, while the HTTP stage can identify + // the underlying ureq timeout without logging its error text. + assert_eq!( + error.kind, + if stage == "http_send" { + ErrorKind::ConnectFailed + } else { + ErrorKind::Transport + } + ); + assert_event(&timing::take_events(), stage, "host_connect", "timeout"); + server.join().unwrap(); + } +} + +#[test] +fn truncated_body_records_error_without_decoding() { + use std::io::{Read, Write}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = [0; 4096]; + stream.read(&mut request).unwrap(); + stream + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\nConnection: close\r\n\r\nsecret") + .unwrap(); + }); + assert!( + CloudClient::new("t") + .request_host_connect(&account(base), "secret") + .is_err() + ); + let events = timing::take_events(); + assert_event(&events, "http_body", "host_connect", "error"); + assert!(!events.iter().any(|e| e.contains("stage=http_decode "))); + server.join().unwrap(); +} From 029d280e99d28e52f029286b768b6c16ce12c3e2 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:39:17 -0800 Subject: [PATCH 031/236] Trace cloud resume latency stages and report slow operations --- crates/jcode-tui/src/tui/app/remote.rs | 6 +- .../jcode-tui/src/tui/app/remote/latency.rs | 16 ++ crates/jcode-tui/src/tui/backend.rs | 29 ++- crates/jcode-tui/src/tui/cloud_latency.rs | 173 ++++++++++++++++++ .../jcode-tui/src/tui/cloud_latency_tests.rs | 46 +++++ crates/jcode-tui/src/tui/mod.rs | 4 +- crates/jcode-tui/src/tui/ui.rs | 2 +- docs/CLOUD_LATENCY.md | 73 ++++++++ scripts/cloud_latency_report.py | 91 +++++++++ scripts/test_cloud_latency_report.py | 53 ++++++ src/cli/cloud_move.rs | 16 +- src/cli/cloud_move/timing.rs | 13 ++ src/cli/cloud_move/transport.rs | 4 +- src/cli/ssh_transport.rs | 14 +- src/cli/ssh_transport/timing.rs | 27 +++ 15 files changed, 524 insertions(+), 43 deletions(-) create mode 100644 crates/jcode-tui/src/tui/app/remote/latency.rs create mode 100644 crates/jcode-tui/src/tui/cloud_latency.rs create mode 100644 crates/jcode-tui/src/tui/cloud_latency_tests.rs create mode 100644 docs/CLOUD_LATENCY.md create mode 100644 scripts/cloud_latency_report.py create mode 100644 scripts/test_cloud_latency_report.py create mode 100644 src/cli/cloud_move/timing.rs create mode 100644 src/cli/ssh_transport/timing.rs diff --git a/crates/jcode-tui/src/tui/app/remote.rs b/crates/jcode-tui/src/tui/app/remote.rs index 737280042ee..4d33857856a 100644 --- a/crates/jcode-tui/src/tui/app/remote.rs +++ b/crates/jcode-tui/src/tui/app/remote.rs @@ -19,6 +19,7 @@ use std::time::{Duration, Instant}; mod input_dispatch; mod key_handling; +mod latency; mod queue_recovery; mod reconnect; mod server_event_handlers; @@ -52,8 +53,7 @@ use session_persistence::{ }; use workspace::{handle_workspace_command, handle_workspace_navigation_key}; -// Re-export the remote input dispatch helpers for sibling modules/tests that go -// through the `remote` facade instead of private submodule paths. +// Re-export remote input dispatch helpers for sibling modules/tests. #[allow(unused_imports)] pub(super) use input_dispatch::{ apply_remote_transcript_event, apply_transcript_event, begin_remote_send, @@ -65,7 +65,7 @@ pub(super) use input_dispatch::{ pub(super) use key_handling::{ handle_remote_char_input, handle_remote_key, handle_remote_key_event, send_interleave_now, }; -pub(super) use server_events::handle_server_event; +pub(super) use latency::handle_server_event; const CONNECTION_MESSAGE_TITLE: &str = "Connection"; const RELOAD_MARKER_MAX_AGE: Duration = Duration::from_secs(30); diff --git a/crates/jcode-tui/src/tui/app/remote/latency.rs b/crates/jcode-tui/src/tui/app/remote/latency.rs new file mode 100644 index 00000000000..6fdb1e0f759 --- /dev/null +++ b/crates/jcode-tui/src/tui/app/remote/latency.rs @@ -0,0 +1,16 @@ +use super::{App, ServerEvent}; +use crate::tui::backend::RemoteEventState; + +pub(in crate::tui::app) fn handle_server_event( + app: &mut App, + event: ServerEvent, + remote: &mut impl RemoteEventState, +) -> bool { + let kind = crate::tui::cloud_latency::event_key(&event); + let started = std::time::Instant::now(); + let redraw = super::server_events::handle_server_event(app, event, remote); + if let Some((kind, _)) = kind { + crate::tui::cloud_latency::applied(kind, started, redraw); + } + redraw +} diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index 5d0e4baab6d..2e1a7ba888a 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -390,6 +390,8 @@ impl RemoteConnection { allow_session_takeover: bool, remote_working_dir: Option<&str>, ) -> Result { + let mut timing = super::cloud_latency::Span::new("connect"); + timing.operation("socket_connect"); let connect_start = Instant::now(); let socket_connect_start = Instant::now(); let socket_path = server::socket_path(); @@ -436,6 +438,7 @@ impl RemoteConnection { }; let connection_setup_ms = connection_setup_start.elapsed().as_millis(); + timing.operation("ssh_ready"); let ssh_ready_start = Instant::now(); // The local SSH adapter accepts before OpenSSH authenticates. A Unix // connect/write alone must not reset reconnect backoff or show success. @@ -445,6 +448,7 @@ impl RemoteConnection { let ssh_ready_ms = ssh_ready_start.elapsed().as_millis(); // Subscribe to events + timing.operation("subscribe"); let subscribe_start = Instant::now(); let (working_dir, selfdev) = super::subscribe_metadata(remote_working_dir); let resume_target = resume_session @@ -481,6 +485,7 @@ impl RemoteConnection { // If resuming a session, the target-aware Subscribe attaches directly to // that session and returns History, so avoid a second bootstrap request. + timing.operation("bootstrap_request"); let bootstrap_request_start = Instant::now(); let mut bootstrap_request = "get_history"; if resume_target.is_none() { @@ -523,6 +528,8 @@ impl RemoteConnection { bootstrap_request, )); + timing.operation("complete"); + timing.finish("ok"); Ok(conn) } @@ -532,20 +539,7 @@ impl RemoteConnection { fn spawn_usage_hydration( hydrate: impl FnOnce() -> bool + Send + 'static, ) -> tokio::task::JoinHandle<()> { - let queued = Instant::now(); - tokio::task::spawn_blocking(move || { - let started = Instant::now(); - let imported = hydrate(); - crate::logging::info(&format!( - "[TIMING] remote quota hydration: queue={}ms, read={}ms, imported={}", - queued - .elapsed() - .saturating_sub(started.elapsed()) - .as_millis(), - started.elapsed().as_millis(), - imported, - )); - }) + super::cloud_latency::spawn_usage_hydration(hydrate) } fn interrupt_request_log_fields( @@ -615,6 +609,7 @@ impl RemoteConnection { )); } + let mut timing = super::cloud_latency::begin_request(&request); let total_start = Instant::now(); let writer_wait_start = Instant::now(); let mut w = self.writer.lock().await; @@ -640,7 +635,11 @@ impl RemoteConnection { )), } } + if let Some(timing) = &mut timing { + timing.finish(if result.is_ok() { "ok" } else { "error" }); + } result?; + super::cloud_latency::request_sent(&request); Ok(()) } @@ -1450,7 +1449,7 @@ impl RemoteConnection { } return LineOutcome::Skip; } - match serde_json::from_str(&text) { + match super::cloud_latency::parse_event(&text) { Ok(ServerEvent::Done { id }) if self .control_done_ids diff --git a/crates/jcode-tui/src/tui/cloud_latency.rs b/crates/jcode-tui/src/tui/cloud_latency.rs new file mode 100644 index 00000000000..aa0745a9643 --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_latency.rs @@ -0,0 +1,173 @@ +//! Content-free client-side cloud latency milestones. +use crate::protocol::{Request, ServerEvent}; +use std::cell::RefCell; +use std::collections::VecDeque; +use std::time::Instant; + +#[cfg(test)] +#[path = "cloud_latency_tests.rs"] +mod tests; + +const MAX_PENDING: usize = 64; +thread_local! { + static REQUESTS: RefCell> = const { RefCell::new(VecDeque::new()) }; + static PAINT: RefCell> = const { RefCell::new(None) }; +} + +pub(super) struct Span { + stage: &'static str, + started: Instant, + outcome: &'static str, + operation: &'static str, + request_id: Option, +} +impl Span { + pub(super) fn new(stage: &'static str) -> Self { + Self { + stage, + started: Instant::now(), + outcome: "error_or_cancelled", + operation: "none", + request_id: None, + } + } + pub(super) fn operation(&mut self, operation: &'static str) { + self.operation = operation; + } + pub(super) fn finish(&mut self, outcome: &'static str) { + self.outcome = outcome; + } +} +impl Drop for Span { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage={} operation={} request_id={:?} outcome={} duration_ms={:.3}", + self.stage, + self.operation, + self.request_id, + self.outcome, + self.started.elapsed().as_secs_f64() * 1000.0 + )); + } +} + +fn request_key(request: &Request) -> Option<(u64, &'static str)> { + match request { + Request::Subscribe { id, .. } => Some((*id, "subscribe")), + Request::ResumeSession { id, .. } => Some((*id, "resume")), + Request::GetHistory { id } => Some((*id, "history")), + Request::GetCompactedHistory { id, .. } => Some((*id, "history_page")), + Request::GetTodos { id } => Some((*id, "todos")), + _ => None, + } +} + +pub(super) fn begin_request(request: &Request) -> Option { + request_key(request).map(|(id, kind)| { + let mut span = Span::new("request_write"); + span.operation = kind; + span.request_id = Some(id); + span + }) +} + +pub(super) fn request_sent(request: &Request) { + let Some((id, kind)) = request_key(request) else { + return; + }; + REQUESTS.with_borrow_mut(|pending| { + pending.retain(|(previous, operation, at)| { + if at.elapsed().as_secs() >= 120 { + crate::logging::info(&format!("[CLOUD_TIMING] stage=response_wait operation={operation} request_id={previous} outcome=expired duration_ms={:.3}", at.elapsed().as_secs_f64() * 1000.0)); + false + } else { *previous != id } + }); + if pending.len() == MAX_PENDING { pending.pop_front(); } + pending.push_back((id, kind, Instant::now())); + }); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=request_sent operation={kind} request_id={id} outcome=ok duration_ms=0" + )); +} + +pub(super) fn event_key(event: &ServerEvent) -> Option<(&'static str, Option)> { + match event { + ServerEvent::History { id, .. } => Some(("history", Some(*id))), + ServerEvent::CompactedHistory { id, .. } => Some(("history_page", Some(*id))), + ServerEvent::Todos { .. } => Some(("todos", None)), + _ => None, + } +} + +pub(super) fn parse_event(text: &str) -> serde_json::Result { + let started = Instant::now(); + let result = serde_json::from_str(text); + let key = match &result { + Ok(event) => event_key(event), + Err(_) => None, + }; + if key.is_some() || result.is_err() || started.elapsed().as_millis() >= 100 { + let (kind, id) = key.unwrap_or(("other", None)); + let wait = REQUESTS.with_borrow_mut(|pending| { + let index = pending.iter().position(|(request, operation, _)| { + id == Some(*request) + || (kind == "todos" && matches!(*operation, "todos" | "subscribe" | "resume")) + })?; + // Early Todos must not consume the History correlation for Subscribe. + let elapsed = pending[index].2.elapsed(); + if kind != "todos" || pending[index].1 == "todos" { + pending.remove(index); + } + Some(elapsed.as_secs_f64() * 1000.0) + }); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=event_parse operation={kind} request_id={id:?} outcome={} duration_ms={:.3} bytes={} request_wait_ms={}", + if result.is_ok() { "ok" } else { "error" }, + started.elapsed().as_secs_f64() * 1000.0, + text.len(), + wait.map(|ms| format!("{ms:.3}")) + .unwrap_or_else(|| "none".into()) + )); + } + result +} + +pub(super) fn applied(kind: &'static str, started: Instant, redraw: bool) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=event_apply operation={kind} outcome=ok duration_ms={:.3} redraw={redraw}", + started.elapsed().as_secs_f64() * 1000.0 + )); + if redraw { + PAINT.with_borrow_mut(|pending| { + if pending.is_none() { + *pending = Some((kind, Instant::now())); + } + }); + } +} + +pub(super) fn rendered() { + PAINT.with_borrow_mut(|pending| { + if let Some((kind, at)) = pending.take() { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=frame_ready operation={kind} outcome=ok duration_ms={:.3}", + at.elapsed().as_secs_f64() * 1000.0 + )); + } + }); +} + +pub(super) fn spawn_usage_hydration( + hydrate: impl FnOnce() -> bool + Send + 'static, +) -> tokio::task::JoinHandle<()> { + let queued = Instant::now(); + tokio::task::spawn_blocking(move || { + let started = Instant::now(); + let imported = hydrate(); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=quota_hydration outcome=ok duration_ms={:.3} queue_ms={:.3} imported={imported}", + started.elapsed().as_secs_f64() * 1000.0, + started.duration_since(queued).as_secs_f64() * 1000.0, + )); + }) +} diff --git a/crates/jcode-tui/src/tui/cloud_latency_tests.rs b/crates/jcode-tui/src/tui/cloud_latency_tests.rs new file mode 100644 index 00000000000..4d5e1bc09be --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_latency_tests.rs @@ -0,0 +1,46 @@ +use super::*; + +#[test] +fn cloud_latency_pending_requests_are_bounded_and_responses_remove_them() { + REQUESTS.with_borrow_mut(VecDeque::clear); + for id in 0..100 { + request_sent(&Request::GetHistory { id }); + } + REQUESTS.with_borrow(|pending| { + assert_eq!(pending.len(), MAX_PENDING); + assert_eq!(pending.front().map(|entry| entry.0), Some(36)); + }); + let event = parse_event(r#"{"type":"compacted_history","id":99,"session_id":"fixture","messages":[],"images":[],"compacted_total":0,"compacted_visible":0,"compacted_remaining":0,"compacted_hidden_prompts":0}"#).unwrap(); + assert_eq!(event_key(&event), Some(("history_page", Some(99)))); + REQUESTS.with_borrow(|pending| assert!(!pending.iter().any(|entry| entry.0 == 99))); + REQUESTS.with_borrow_mut(VecDeque::clear); +} + +#[test] +fn cloud_latency_todos_preserve_attach_correlation_and_payload() { + REQUESTS.with_borrow_mut(|pending| { + pending.clear(); + pending.push_back((42, "subscribe", Instant::now())); + }); + let event = parse_event(r#"{"type":"todos","session_id":"fixture","payload":"private transcript must not be logged"}"#).unwrap(); + assert_eq!(event_key(&event), Some(("todos", None))); + REQUESTS.with_borrow(|pending| assert_eq!(pending.len(), 1)); + assert!( + matches!(event, ServerEvent::Todos { payload, .. } if payload == "private transcript must not be logged") + ); + REQUESTS.with_borrow_mut(VecDeque::clear); + assert!(parse_event("not json").is_err()); +} + +#[test] +fn cloud_latency_paint_milestone_is_consumed_once() { + PAINT.with_borrow_mut(|pending| *pending = None); + applied("history", Instant::now(), false); + PAINT.with_borrow(|pending| assert!(pending.is_none())); + applied("todos", Instant::now(), true); + applied("history", Instant::now(), true); + PAINT.with_borrow(|pending| assert_eq!(pending.as_ref().map(|entry| entry.0), Some("todos"))); + rendered(); + PAINT.with_borrow(|pending| assert!(pending.is_none())); + rendered(); +} diff --git a/crates/jcode-tui/src/tui/mod.rs b/crates/jcode-tui/src/tui/mod.rs index ac9c8154162..79f1e4e4698 100644 --- a/crates/jcode-tui/src/tui/mod.rs +++ b/crates/jcode-tui/src/tui/mod.rs @@ -23,13 +23,13 @@ pub struct BackgroundTaskRow { pub label: String, pub percent: Option, pub status: BackgroundTaskRowStatus, - /// When a successful task stopped being actionable. Running and failed - /// tasks remain visible until their state changes or the session closes. + /// When a successful task stopped being actionable, retained until session closure. pub completed_at: Option, } pub mod backend; mod cloud_git; +mod cloud_latency; mod cloud_status; pub(crate) mod color_support; mod core; diff --git a/crates/jcode-tui/src/tui/ui.rs b/crates/jcode-tui/src/tui/ui.rs index 1afe5ba91fe..2ea3ee845cd 100644 --- a/crates/jcode-tui/src/tui/ui.rs +++ b/crates/jcode-tui/src/tui/ui.rs @@ -2816,8 +2816,8 @@ pub fn draw(frame: &mut Frame, app: &dyn TuiState) { adapt_buffer_for_emoji_preference(frame.buffer_mut()); // Classic Kitty placements must follow all text writes. Unlike Unicode // placeholders they survive scrolling, so finalize even an image-free frame. - // This also drains deferred cache-eviction deletions when no image remains. crate::tui::mermaid::finish_terminal_image_frame(frame.buffer_mut()); + crate::tui::cloud_latency::rendered(); } fn draw_inner(frame: &mut Frame, app: &dyn TuiState) { panel_image_preview::clear_regions(); diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md new file mode 100644 index 00000000000..1c6f53e40ca --- /dev/null +++ b/docs/CLOUD_LATENCY.md @@ -0,0 +1,73 @@ +# Cloud latency diagnostics + +Cloud operations emit info-level, content-free timing records to the ordinary +Jcode logs in `$JCODE_HOME/logs/jcode-YYYY-MM-DD.log` (default `~/.jcode/logs`). +They do not require trace mode. Each process writes its own records, so a local +client and a cloud daemon must both be updated to see both sides of a resume. +A new local binary does not update an already-running cloud daemon. + +## Find expensive stages + +From the repository root: + +```bash +python3 scripts/cloud_latency_report.py --slow-ms 1000 +python3 scripts/cloud_latency_report.py ~/.jcode/logs/jcode-2026-11-07.log +python3 scripts/cloud_latency_report.py downloaded-cloud-daemon.log --slow-ms 250 +``` + +The report ranks stage/operation/metric groups by maximum observed duration and +shows sample count, error count, p50, p95 and maximum. It reads files incrementally, +retaining numeric samples for exact percentiles. Defaults include all retained +logs, so narrow the files when comparing before/after builds. Percentiles describe +only the selected observed samples, not a statistically controlled benchmark. +The report prints only allowlisted label syntax and numeric fields, never raw log +lines. Do not combine overlapping stage/total metrics by summing them. + +Most records use `[CLOUD_TIMING]`; mobile records use `mobile_sync stage=...`. +Common fields are `stage`, `operation`, `outcome`, and `duration_ms`. Additional +numeric `*_ms` fields appear as separate report rows. Request IDs correlate +client wire events and server work. Server records additionally have operation IDs, +a hashed session key, and history kind. Session keys are diagnostic correlation, +not an authorization mechanism. `none` means a metric was not available. + +## Coverage and interpretation + +| Path | Observable boundaries | +| --- | --- | +| Cloud control plane | HTTP send, body read/decode, host readiness polling/wait and terminal success/failure/cancellation | +| Native SSH attach | Process start through authenticated daemon handshake, including failures and cancelled attempts | +| TUI connect | Socket, readiness, Subscribe and bootstrap setup, with the failing phase retained on early return | +| History/todos transport | Request write, response wait, event JSON parse, payload byte count and request correlation | +| Server bootstrap and paging | Snapshot/build, encoding, writer-lock wait and socket write, plus fallback/failure/drop outcomes | +| Client application | History, page and todo event application, followed by one-shot frame composition readiness | +| Preview/inventory | Cache scope/read/freshness/write, SSH command, remote projection/inventory and local decode/load | +| Mobile transcript sync | Save queue/debounce, incremental/reconciliation pass, session upload/skip/error, HTTP and progress persistence | +| Handoff | Existing preparation/cache/probe timings plus machine-readable repository/snapshot/archive/upload/activation stages | + +`request_wait_ms` starts after the request write completes and ends after response +JSON parsing. It includes remote queue/work, transfer and client scheduling, not +just network RTT. Server writer-lock versus write versus encode timings help +separate those causes. This measurement survives a cancelled socket-read future +because it is attached to the outstanding request, not a particular read attempt. +The correlation queue is bounded; old entries expire when subsequent requests +arrive. Expiry is a diagnostic observation, not an application timeout. + +`frame_ready` measures from completed event application until a composed TUI frame +is ready. It is not a physical display/terminal flush timestamp. Updates may share +a frame. Existing rendering diagnostics give detailed draw costs when needed. +Fast streaming tokens are deliberately not logged individually. Key sync milestones +are logged consistently, while unrelated protocol events only receive a parse +record if slow or malformed, avoiding per-token logging overhead. + +Remote projector stderr must pass a bounded, strict timing-record filter before +entering local logs. Arbitrary SSH stderr is not forwarded. Timings contain fixed +stage/outcome labels, numeric sizes/durations and bounded correlation fields, not +credentials, keys, paths, transcript contents, payloads or raw server error bodies. +Keep ordinary diagnostic log files private nevertheless, since other existing +subsystems may include information beyond these timing records. + +A high total alone is not proof of the cause. Compare cache hits/misses, success +versus failure, cold versus warm connections and local versus daemon stage timing. +Do not claim a cold VM wake or a full end-to-end cloud resume is subsecond just +because one stage or a synthetic fixture is subsecond. diff --git a/scripts/cloud_latency_report.py b/scripts/cloud_latency_report.py new file mode 100644 index 00000000000..2220c23f5f8 --- /dev/null +++ b/scripts/cloud_latency_report.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +"""Summarize content-free cloud timing metrics from Jcode log files.""" +import argparse +from collections import defaultdict +import glob +import math +import os +from pathlib import Path +import re +import sys + +FIELD = re.compile(r"\b([a-z_]+)=([^\s,]+)") +NUMBER = re.compile(r"^\d+(?:\.\d+)?$") +SAFE_LABEL = re.compile(r"^[a-zA-Z0-9_.:-]{1,96}$") + + +def collect(lines): + groups = defaultdict(list) + failures = defaultdict(int) + for line in lines: + if not any(prefix in line for prefix in ("[CLOUD_TIMING]", "mobile_sync stage=", "cloud_preview stage=", "cloud_inventory stage=")): + continue + fields = dict(FIELD.findall(line)) + stage = fields.get("stage", "unknown") + operation = fields.get("operation", "") + if not SAFE_LABEL.fullmatch(stage) or (operation and not SAFE_LABEL.fullmatch(operation)): + continue + kind = fields.get("kind", "") + if kind and SAFE_LABEL.fullmatch(kind): + operation = operation + "." + kind if operation else kind + name = stage + (":" + operation if operation else "") + outcome = fields.get("outcome", "unknown") + for metric, value in fields.items(): + if not metric.endswith("_ms") or not NUMBER.fullmatch(value): + continue + number = float(value) + if not math.isfinite(number): + continue + key = (name, metric) + groups[key].append(number) + if outcome in {"error", "failed", "dropped", "timeout", "cancelled", "expired", "error_or_cancelled"}: + failures[key] += 1 + return groups, failures + + +def percentile(values, fraction): + values = sorted(values) + return values[max(0, math.ceil(len(values) * fraction) - 1)] + + +def report(groups, failures, threshold_ms=0): + rows = [ + (max(values), name, metric, values) + for (name, metric), values in groups.items() + if max(values) >= threshold_ms + ] + print(f"{'stage:operation':52} {'metric':20} {'count':>7} {'errors':>7} {'p50 ms':>10} {'p95 ms':>10} {'max ms':>10}") + for maximum, name, metric, values in sorted(rows, reverse=True): + print(f"{name:52} {metric:20} {len(values):7} {failures[(name, metric)]:7} {percentile(values, .5):10.2f} {percentile(values, .95):10.2f} {maximum:10.2f}") + if not rows: + print("No matching timing samples. Run an instrumented cloud operation first, or select other logs.") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("files", nargs="*", help="Log files or glob patterns. '-' reads stdin. Default: all retained Jcode logs.") + parser.add_argument("--slow-ms", type=float, default=0, help="Only show stages whose maximum reaches this duration.") + args = parser.parse_args() + files = args.files or [str(Path(os.environ.get("JCODE_HOME", Path.home() / ".jcode")) / "logs" / "jcode-*.log")] + + def lines(): + seen = set() + for pattern in files: + if pattern == "-": + yield from sys.stdin + continue + for filename in sorted(glob.glob(pattern)): + if filename in seen: + continue + seen.add(filename) + try: + with open(filename, encoding="utf-8", errors="replace") as handle: + yield from handle + except OSError as error: + print(f"Could not read log file: {error.strerror}", file=sys.stderr) + groups, failures = collect(lines()) + report(groups, failures, args.slow_ms) + + +if __name__ == "__main__": + main() diff --git a/scripts/test_cloud_latency_report.py b/scripts/test_cloud_latency_report.py new file mode 100644 index 00000000000..70e98e2e6ba --- /dev/null +++ b/scripts/test_cloud_latency_report.py @@ -0,0 +1,53 @@ +import contextlib +import importlib.util +import io +from pathlib import Path +import unittest + +SPEC = importlib.util.spec_from_file_location("cloud_latency_report", Path(__file__).with_name("cloud_latency_report.py")) +reporter = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(reporter) + + +class CloudLatencyReportTests(unittest.TestCase): + def test_collects_failure_and_secondary_wait_metrics(self): + groups, failures = reporter.collect([ + "[INFO] [CLOUD_TIMING] stage=event_parse operation=history outcome=ok duration_ms=12.5 request_wait_ms=1400 bytes=261000\n", + "[INFO] [CLOUD_TIMING] stage=event_parse operation=history outcome=error duration_ms=2\n", + "[INFO] mobile_sync stage=http_upload outcome=timeout duration_ms=6000\n", + ]) + self.assertEqual(groups[("event_parse:history", "duration_ms")], [12.5, 2]) + self.assertEqual(groups[("event_parse:history", "request_wait_ms")], [1400]) + self.assertEqual(failures[("event_parse:history", "duration_ms")], 1) + self.assertEqual(failures[("http_upload", "duration_ms")], 1) + + def test_ignores_unrelated_invalid_and_nonfinite_values(self): + groups, _ = reporter.collect([ + "private stage=anything outcome=ok duration_ms=9000", + "[CLOUD_TIMING] stage=https://secret outcome=ok duration_ms=9", + "[CLOUD_TIMING] stage=parse outcome=ok duration_ms=nan", + "[CLOUD_TIMING] stage=parse outcome=ok duration_ms=inf", + "[CLOUD_TIMING] stage=parse outcome=ok request_wait_ms=none", + "[CLOUD_TIMING] stage=parse outcome=ok duration_ms=" + "9" * 400, + ]) + self.assertFalse(groups) + + def test_report_is_ranked_filtered_and_content_free(self): + groups, failures = reporter.collect([ + "[CLOUD_TIMING] stage=fast outcome=ok duration_ms=20 payload=PRIVATE", + "[CLOUD_TIMING] stage=slow outcome=error duration_ms=1000 payload=PRIVATE", + "[CLOUD_TIMING] stage=slower outcome=ok duration_ms=3000 token=SECRET", + ]) + output = io.StringIO() + with contextlib.redirect_stdout(output): + reporter.report(groups, failures, threshold_ms=500) + text = output.getvalue() + self.assertLess(text.index("slower"), text.index("slow ")) + self.assertNotIn("fast", text) + self.assertNotIn("PRIVATE", text) + self.assertNotIn("SECRET", text) + self.assertEqual(reporter.percentile([1, 2, 3, 4], .95), 4) + + +if __name__ == "__main__": + unittest.main() diff --git a/src/cli/cloud_move.rs b/src/cli/cloud_move.rs index f4343c92091..52dd9128fb8 100644 --- a/src/cli/cloud_move.rs +++ b/src/cli/cloud_move.rs @@ -423,20 +423,8 @@ fn mark_memory_sent(host: &str, memory: &Path, bytes: &[u8]) { } } -fn timed_move_stage(stage: &str, operation: impl FnOnce() -> Result) -> Result { - let started = std::time::Instant::now(); - let result = operation(); - crate::logging::info(&format!( - "cloud move: {stage} {} in {:.3}s", - if result.is_ok() { - "completed" - } else { - "failed" - }, - started.elapsed().as_secs_f64() - )); - result -} +mod timing; +use timing::timed_move_stage; pub(crate) fn run_move( session_ref: Option<&str>, diff --git a/src/cli/cloud_move/timing.rs b/src/cli/cloud_move/timing.rs new file mode 100644 index 00000000000..12a7e9e646f --- /dev/null +++ b/src/cli/cloud_move/timing.rs @@ -0,0 +1,13 @@ +use anyhow::Result; + +pub(super) fn timed_move_stage(stage: &str, operation: impl FnOnce() -> Result) -> Result { + let started = std::time::Instant::now(); + let result = operation(); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=handoff operation={} outcome={} duration_ms={:.3}", + stage.replace(' ', "_"), + if result.is_ok() { "ok" } else { "error" }, + started.elapsed().as_secs_f64() * 1000.0 + )); + result +} diff --git a/src/cli/cloud_move/transport.rs b/src/cli/cloud_move/transport.rs index a478147c5c5..e36efe28a3a 100644 --- a/src/cli/cloud_move/transport.rs +++ b/src/cli/cloud_move/transport.rs @@ -141,7 +141,9 @@ impl Target { // processes. Waiters reuse only a fully successful preflight. let _guard = if self.transport.is_none() { Some(timed_move_stage("preflight singleflight wait", || { - let path = crate::storage::jcode_dir()?.join("cloud-preflight.lock"); + let home = crate::storage::jcode_dir()?; + std::fs::create_dir_all(&home).context("create cloud preflight directory")?; + let path = home.join("cloud-preflight.lock"); let file = std::fs::OpenOptions::new() .create(true) .truncate(false) diff --git a/src/cli/ssh_transport.rs b/src/cli/ssh_transport.rs index e0e2ac9bc43..b2de50d3f2a 100644 --- a/src/cli/ssh_transport.rs +++ b/src/cli/ssh_transport.rs @@ -19,6 +19,8 @@ use tokio::process::{Child, ChildStdin, ChildStdout, Command}; use tokio::sync::watch; use tokio::task::{JoinHandle, JoinSet}; +mod timing; + const PROTOCOL: u32 = 1; const HANDSHAKE_LIMIT: usize = 8192; const STDERR_LIMIT: usize = 16 * 1024; @@ -118,11 +120,9 @@ impl NativeSsh { ); } options.working_dir = remote_working_dir.map(str::to_owned); - // Fail before entering the TUI, with authentication/host-key diagnostics. let probe = SshConnection::connect(&options).await?; let handshake = probe.handshake.clone(); - // Remote --cwd already verified the directory and the header contains - // its resolved path. Keep reconnects on that same verified workspace. + // Keep reconnects on the workspace already resolved and verified by remote --cwd. options.working_dir = Some(handshake.working_dir.clone()); let directory = private_directory()?; @@ -333,8 +333,7 @@ struct SshConnection { impl SshConnection { async fn connect(options: &SshOptions) -> Result { - // Jcode Cloud hosts may be asleep: waking the VM and starting the - // remote daemon routinely exceeds the plain SSH startup deadline. + // Waking cloud hosts and starting their daemon exceeds the plain SSH deadline. let cloud = super::cloud_managed::is_managed(&options.host) || super::cloud_ephemeral::host_id(&options.host).is_some(); let (deadline, attempts) = if cloud { @@ -356,11 +355,13 @@ impl SshConnection { } async fn spawn(command: Command, deadline: Duration) -> Result { + let mut timing = timing::StartupTiming::new(); let mut connection = Self::spawn_process(command)?; if let Err(error) = connection.establish(deadline).await { connection.shutdown().await; return Err(error.context(connection.diagnostic())); } + timing.success(); Ok(connection) } @@ -369,8 +370,7 @@ impl SshConnection { #[cfg(target_os = "linux")] { let parent_pid = std::process::id(); - // SIGKILL/process::exit do not run guards. Do not leave SSH holding - // a remote bridge open if the local UI disappears abruptly. + // SIGKILL/process::exit skip guards, so bind SSH lifetime to this UI. unsafe { command.as_std_mut().pre_exec(move || { if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) != 0 { diff --git a/src/cli/ssh_transport/timing.rs b/src/cli/ssh_transport/timing.rs new file mode 100644 index 00000000000..aea93184f8c --- /dev/null +++ b/src/cli/ssh_transport/timing.rs @@ -0,0 +1,27 @@ +use std::time::Instant; + +/// Content-free timing even when an SSH future is cancelled or returns early. +pub(super) struct StartupTiming { + started: Instant, + outcome: &'static str, +} +impl StartupTiming { + pub(super) fn new() -> Self { + Self { + started: Instant::now(), + outcome: "error_or_cancelled", + } + } + pub(super) fn success(&mut self) { + self.outcome = "ok"; + } +} +impl Drop for StartupTiming { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=ssh_startup outcome={} duration_ms={:.3}", + self.outcome, + self.started.elapsed().as_secs_f64() * 1000.0 + )); + } +} From d86ab7a4cb7c8b58ba28208e56fad59a936ff2ff Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:39:38 -0800 Subject: [PATCH 032/236] Safely forward bounded remote projection timing records --- crates/jcode-tui/src/tui/cloud_git.rs | 50 ++++-- crates/jcode-tui/src/tui/cloud_git_stderr.rs | 92 +++++++++++ .../src/tui/cloud_git_stderr_tests.rs | 150 ++++++++++++++++++ .../loading/mobile_inventory.rs | 30 +++- .../loading/mobile_inventory_tests.rs | 43 +++++ 5 files changed, 349 insertions(+), 16 deletions(-) create mode 100644 crates/jcode-tui/src/tui/cloud_git_stderr.rs create mode 100644 crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs diff --git a/crates/jcode-tui/src/tui/cloud_git.rs b/crates/jcode-tui/src/tui/cloud_git.rs index b3294a27424..a09a78c97f0 100644 --- a/crates/jcode-tui/src/tui/cloud_git.rs +++ b/crates/jcode-tui/src/tui/cloud_git.rs @@ -8,6 +8,8 @@ use std::path::PathBuf; use std::sync::{Arc, Mutex, OnceLock}; use std::time::Duration; use tokio::io::AsyncReadExt; +#[path = "cloud_git_stderr.rs"] +mod stderr_timing; #[derive(Clone, Debug, Default, PartialEq)] pub enum RepoState { @@ -143,7 +145,7 @@ fn script_command_inner( }; cmd.stdin(std::process::Stdio::null()) .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) .kill_on_drop(true); cmd } @@ -180,6 +182,7 @@ pub(crate) async fn bounded_output( deadline: Duration, limit: usize, ) -> Option<(std::process::ExitStatus, Vec)> { + let mut timing = super::cloud_latency::Span::new("workspace_command"); #[cfg(unix)] { use std::os::unix::process::CommandExt; @@ -201,27 +204,48 @@ pub(crate) async fn bounded_output( } } } + command + .stderr(std::process::Stdio::piped()) + .kill_on_drop(true); let Ok(mut child) = command.spawn() else { return None; }; + let _process_group = stderr_timing::ProcessGroup(child.id()); let stdout = child.stdout.take()?; + let stderr = child.stderr.take()?; let result = tokio::time::timeout(deadline, async { - let mut bytes = Vec::new(); - stdout - .take((limit + 1) as u64) - .read_to_end(&mut bytes) - .await - .ok()?; - if bytes.len() > limit { - return None; + let output = async { + let mut bytes = Vec::new(); + stdout + .take(limit.saturating_add(1) as u64) + .read_to_end(&mut bytes) + .await + .ok()?; + if bytes.len() > limit { + return None; + } + let status = child.wait().await.ok()?; + Some((status, bytes)) + }; + let diagnostics = async { + stderr_timing::drain(stderr, |record| crate::logging::info(&record)).await; + Ok::<(), ()>(()) + }; + // No detached reader can outlive cancellation or this deadline. An + // oversized stdout cancels the stderr drain immediately. + match tokio::try_join!(async { output.await.ok_or(()) }, diagnostics) { + Ok((output, ())) => Some(output), + Err(()) => None, } - let status = child.wait().await.ok()?; - Some((status, bytes)) }) .await; match result { - Ok(Some(output)) => Some(output), - _ => { + Ok(Some(output)) => { + timing.finish(if output.0.success() { "ok" } else { "error" }); + Some(output) + } + result => { + timing.finish(if result.is_err() { "timeout" } else { "error" }); #[cfg(unix)] if let Some(pid) = child.id() { // Own process group only, including ProxyCommand descendants. diff --git a/crates/jcode-tui/src/tui/cloud_git_stderr.rs b/crates/jcode-tui/src/tui/cloud_git_stderr.rs new file mode 100644 index 00000000000..fdfa1518cc6 --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_git_stderr.rs @@ -0,0 +1,92 @@ +//! Remote stderr is untrusted. Drain it without retaining content or trusting labels. +use tokio::io::{AsyncRead, AsyncReadExt}; + +const MAX_LINE: usize = 192; +const SCAN_BUDGET: usize = 64 * 1024; +const RECORD_BUDGET: usize = 128; + +fn timing_record(line: &[u8]) -> Option { + let Ok(text) = std::str::from_utf8(line) else { + return None; + }; + let rest = text.strip_prefix("[CLOUD_TIMING] stage=")?; + let (stage, rest) = rest.split_once(" outcome=")?; + let stage = match stage { + "projection" => "projection", + "remote_request" => "remote_request", + "inventory_entry" => "inventory_entry", + "remote_preview" => "remote_preview", + _ => return None, + }; + let (outcome, duration) = rest.split_once(" duration_ms=")?; + let outcome = match outcome { + "success" => "success", + "error" => "error", + "skipped_invalid_target" => "skipped_invalid_target", + "skipped_away" => "skipped_away", + _ => return None, + }; + if duration.is_empty() || !duration.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let Ok(duration) = duration.parse::() else { + return None; + }; + Some(format!( + "[CLOUD_TIMING] stage={stage} outcome={outcome} duration_ms={duration}" + )) +} + +pub(super) async fn drain(reader: impl AsyncRead + Unpin, mut emit: impl FnMut(String)) { + let mut reader = reader; + let mut chunk = [0u8; 4096]; + let mut line = Vec::with_capacity(MAX_LINE); + let mut oversized = false; + let mut scanned = 0usize; + let mut emitted = 0usize; + loop { + let Ok(count) = reader.read(&mut chunk).await else { + return; + }; + if count == 0 { + return; + } + for &byte in &chunk[..count] { + if scanned >= SCAN_BUDGET || emitted >= RECORD_BUDGET { + // Still drain the pipe, otherwise a noisy child can deadlock stdout. + break; + } + scanned += 1; + if byte == b'\n' { + if !oversized && let Some(record) = timing_record(&line) { + emit(record); + emitted += 1; + } + line.clear(); + oversized = false; + } else if line.len() < MAX_LINE && !oversized { + line.push(byte); + } else { + line.clear(); + oversized = true; + } + } + } +} + +/// Unlike kill_on_drop, this also kills pipe-holding descendants on cancellation. +pub(super) struct ProcessGroup(pub(super) Option); +impl Drop for ProcessGroup { + fn drop(&mut self) { + #[cfg(unix)] + if let Some(pid) = self.0 { + unsafe { + libc::kill(-(pid as i32), libc::SIGKILL); + } + } + } +} + +#[cfg(test)] +#[path = "cloud_git_stderr_tests.rs"] +mod tests; diff --git a/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs b/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs new file mode 100644 index 00000000000..7676470d358 --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs @@ -0,0 +1,150 @@ +use super::*; +use crate::tui::cloud_git::{bounded_output, script_command}; +use std::time::Duration; + +const SAFE: &str = "[CLOUD_TIMING] stage=projection outcome=success duration_ms=17"; + +#[test] +fn exact_whitelist_rejects_auth_content_and_injection() { + assert_eq!(timing_record(SAFE.as_bytes()).as_deref(), Some(SAFE)); + for line in [ + "Authorization: Bearer SECRET".to_string(), + SAFE.replace("projection", "secret-session-id"), + SAFE.replace("success", "private-error"), + format!("{SAFE} token=SECRET"), + format!("{SAFE}\r"), + format!(" {SAFE}"), + SAFE.replace("17", "NaN"), + SAFE.replace("17", "-1"), + SAFE.replace("17", "1.2"), + SAFE.replace("17", "18446744073709551616"), + SAFE.replace("17", "17\u{1b}[31m"), + ] { + assert!(timing_record(line.as_bytes()).is_none()); + } + assert!(timing_record(b"\xff").is_none()); +} + +#[tokio::test] +async fn oversized_lines_and_stream_have_fixed_log_and_scan_budgets() { + let mut input = vec![b'x'; MAX_LINE + 1]; + input.extend_from_slice(SAFE.as_bytes()); // A suffix must not masquerade as a line. + input.push(b'\n'); + input.extend_from_slice(SAFE.as_bytes()); + input.push(b'\n'); + let mut records = Vec::new(); + drain(input.as_slice(), |record| records.push(record)).await; + assert_eq!(records, [SAFE]); + let input = format!("{SAFE}\n").repeat(RECORD_BUDGET * 100); + records.clear(); + drain(input.as_bytes(), |record| records.push(record)).await; + assert_eq!(records.len(), RECORD_BUDGET); + let mut input = vec![b'x'; SCAN_BUDGET]; + input.push(b'\n'); + input.extend_from_slice(format!("{SAFE}\n").as_bytes()); + records.clear(); + drain(input.as_slice(), |record| records.push(record)).await; + assert!(records.is_empty()); +} + +#[tokio::test] +async fn split_records_and_incomplete_eof_do_not_leak() { + use tokio::io::AsyncWriteExt; + let (mut writer, reader) = tokio::io::duplex(8); + let producer = async { + writer + .write_all(format!("SECRET\n{SAFE}\n{SAFE}").as_bytes()) + .await + .unwrap(); + drop(writer); + }; + let mut records = Vec::new(); + tokio::join!(producer, drain(reader, |record| records.push(record))); + assert_eq!(records, [SAFE]); +} + +#[tokio::test] +async fn concurrent_large_stderr_cannot_deadlock_stdout_or_hide_exit_failure() { + for exit in [0, 7] { + let script = format!( + "head -c 2097152 /dev/zero >&2; printf '%s\\n' '{SAFE}' >&2; printf payload; exit {exit}" + ); + let command = script_command(None, None, "unused", &script); + let (status, bytes) = bounded_output(command, Duration::from_secs(5), 64) + .await + .unwrap(); + assert_eq!(status.code(), Some(exit)); + assert_eq!(bytes, b"payload"); + } +} + +#[tokio::test] +async fn oversized_stdout_cancels_infinite_stderr_without_waiting_for_deadline() { + let command = script_command( + None, + None, + "unused", + "(while :; do printf secret >&2; done) & head -c 65 /dev/zero; wait", + ); + let started = std::time::Instant::now(); + assert!( + bounded_output(command, Duration::from_secs(10), 64) + .await + .is_none() + ); + assert!(started.elapsed() < Duration::from_secs(2)); +} + +#[cfg(target_os = "linux")] +async fn assert_processes_stopped(path: &std::path::Path) { + let pids = std::fs::read_to_string(path).unwrap(); + for _ in 0..100 { + let alive = pids.split_whitespace().any(|pid| { + std::fs::read_to_string(format!("/proc/{pid}/stat")) + .ok() + .and_then(|stat| { + stat.rsplit_once(") ") + .map(|(_, rest)| !rest.starts_with('Z')) + }) + .unwrap_or(false) + }); + if !alive { + return; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + panic!("command process group survived cancellation"); +} + +#[cfg(target_os = "linux")] +#[tokio::test] +async fn deadline_and_cancellation_kill_pipe_holding_descendants() { + for cancel in [false, true] { + let temp = tempfile::tempdir().unwrap(); + let pids = temp.path().join("pids"); + let command = script_command( + None, + None, + pids.to_str().unwrap(), + "sleep 30 & printf '%s %s' \"$$\" \"$!\" > \"$1\"; wait", + ); + let task = tokio::spawn(bounded_output(command, Duration::from_millis(500), 64)); + tokio::time::timeout(Duration::from_secs(2), async { + loop { + if std::fs::read_to_string(&pids).is_ok_and(|s| s.split_whitespace().count() == 2) { + break; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .unwrap(); + if cancel { + task.abort(); + assert!(task.await.unwrap_err().is_cancelled()); + } else { + assert!(task.await.unwrap().is_none()); + } + assert_processes_stopped(&pids).await; + } +} diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs index a1451bed579..d6aec3956ec 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs @@ -1,4 +1,5 @@ //! Discover mobile-created sessions without copying their transcripts locally. +use super::super::cloud_preview::StageTimer; use super::*; #[derive(Deserialize)] @@ -24,7 +25,9 @@ pub(super) fn available() -> bool { } pub(super) fn refresh() -> Result { + let mut timer = StageTimer::new("inventory_refresh"); if !available() { + timer.outcome("skipped_unavailable"); return Ok(false); } let home = storage::jcode_dir()?; @@ -32,8 +35,11 @@ pub(super) fn refresh() -> Result { let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() .build()?; - runtime.block_on(async { + let result = runtime.block_on(async { + let mut discovery = StageTimer::new("inventory_discovery"); let inventory = crate::mobile_sync::discover_cloud_sessions().await?; + discovery.outcome("success"); + drop(discovery); let candidates: Vec<_> = inventory .into_iter() .filter(|s| should_verify(&home, &s.session_id)) @@ -41,7 +47,10 @@ pub(super) fn refresh() -> Result { if candidates.is_empty() { return Ok(false); } + let mut prepare = StageTimer::new("inventory_prepare_ssh"); prepare_ssh_config(&config).await?; + prepare.outcome("success"); + drop(prepare); let ids = candidates .iter() .map(|s| s.session_id.as_str()) @@ -53,14 +62,29 @@ pub(super) fn refresh() -> Result { &ids, &super::super::cloud_preview::projection_script("inventory"), ); + let mut fetch = StageTimer::new("inventory_ssh"); let (status, bytes) = crate::tui::cloud_git::bounded_output(command, Duration::from_secs(10), 1024 * 1024) .await .ok_or_else(|| anyhow::anyhow!("cloud inventory verification unavailable"))?; anyhow::ensure!(status.success(), "cloud inventory verification failed"); + fetch.outcome("success"); + drop(fetch); + let mut parse = StageTimer::new("inventory_parse"); let verified: VerifiedInventory = serde_json::from_slice(&bytes)?; - register(&home, candidates, verified) - }) + parse.outcome("success"); + drop(parse); + let mut registration = StageTimer::new("inventory_register"); + let result = register(&home, candidates, verified); + if result.is_ok() { + registration.outcome("success"); + } + result + }); + if result.is_ok() { + timer.outcome("success"); + } + result } fn valid_id(id: &str) -> bool { diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs index a267884ad5e..bc64f61945a 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory_tests.rs @@ -1,5 +1,48 @@ use super::*; +#[cfg(unix)] +#[test] +fn inventory_projection_uses_bounded_transport_without_mixing_timing_into_json() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _guard = HomeGuard::install(home.path()); + let id = "session_inventory_transport"; + let mut session = Session::create_with_id( + id.into(), + None, + Some("Private title must stay out of timing records".into()), + ); + session.migration_epoch = 8; + session.save().unwrap(); + let mut command = crate::tui::cloud_git::script_command( + None, + None, + id, + &super::super::super::cloud_preview::projection_script("inventory"), + ); + command.env("JCODE_HOME", home.path()); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let (status, bytes) = runtime + .block_on(crate::tui::cloud_git::bounded_output( + command, + Duration::from_secs(5), + 1024 * 1024, + )) + .unwrap(); + assert!(status.success()); + let inventory: VerifiedInventory = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(inventory.sessions.len(), 1); + assert_eq!(inventory.sessions[0].session_id, id); + assert_eq!(inventory.sessions[0].epoch, 8); + assert_eq!( + inventory.sessions[0].title.as_deref(), + Some("Private title must stay out of timing records") + ); +} + struct HomeGuard(Option); impl HomeGuard { From e256031add26856b611e3c7880fd842b89251203 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:40:57 -0800 Subject: [PATCH 033/236] Measure preview stages around their actual operations --- .../src/tui/session_picker/cloud_preview.rs | 60 ++++++++++++++----- .../tui/session_picker/cloud_preview/cache.rs | 10 ++-- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs index b452f0fac93..80d77c5a674 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs @@ -29,8 +29,15 @@ impl StageTimer { } } +#[cfg(test)] +thread_local! { + static STAGE_TIMINGS: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; +} + impl Drop for StageTimer { fn drop(&mut self) { + #[cfg(test)] + STAGE_TIMINGS.with_borrow_mut(|events| events.push((self.stage, self.started.elapsed()))); crate::logging::info(&format!( "[CLOUD_TIMING] stage={} outcome={} duration_ms={}", self.stage, @@ -40,9 +47,9 @@ impl Drop for StageTimer { } } -fn stage_result(result: Result, stage: &'static str) -> Option { +fn stage_result(operation: impl FnOnce() -> Result, stage: &'static str) -> Option { let mut timer = StageTimer::new(stage); - match result { + match operation() { Ok(value) => { timer.outcome("success"); Some(value) @@ -157,7 +164,7 @@ fn parse_for_session(bytes: &[u8], session_id: &str) -> Option { fn fetch_bytes(session_id: &str, host: &str) -> Option> { let mut timer = StageTimer::new("ssh"); - let config = stage_result(crate::storage::jcode_dir(), "ssh_config")?.join("cloud/ssh_config"); + let config = stage_result(crate::storage::jcode_dir, "ssh_config")?.join("cloud/ssh_config"); let command = crate::tui::cloud_git::compressed_script_command( Some(host), config.is_file().then_some(config.as_path()), @@ -165,9 +172,11 @@ fn fetch_bytes(session_id: &str, host: &str) -> Option> { &projection_script("preview"), ); let runtime = stage_result( - tokio::runtime::Builder::new_current_thread() - .enable_all() - .build(), + || { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + }, "ssh_runtime", )?; let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output( @@ -201,7 +210,7 @@ fn parse_preview( timer.outcome("skipped_absent"); None } else { - let result = stage_result(serde_json::from_slice(bytes), "telemetry_decode"); + let result = stage_result(|| serde_json::from_slice(bytes), "telemetry_decode"); if result.is_some() { timer.outcome("success"); } @@ -213,26 +222,49 @@ fn parse_preview( fn parse_transcript(bytes: &[u8]) -> Option { let split = bytes.iter().position(|b| *b == 0)?; - let dir = stage_result(tempfile::tempdir(), "parse_tempdir")?; + let dir = stage_result(tempfile::tempdir, "parse_tempdir")?; let path = dir.path().join("preview.json"); stage_result( - std::fs::write(&path, &bytes[..split]), + || std::fs::write(&path, &bytes[..split]), "parse_snapshot_write", )?; stage_result( - std::fs::write( - crate::session::session_journal_path_from_snapshot(&path), - &bytes[split + 1..], - ), + || { + std::fs::write( + crate::session::session_journal_path_from_snapshot(&path), + &bytes[split + 1..], + ) + }, "parse_journal_write", )?; - stage_result(Session::load_from_path(&path), "parse_hydrate") + stage_result(|| Session::load_from_path(&path), "parse_hydrate") } #[cfg(test)] mod tests { use super::*; + #[test] + fn stage_result_times_the_operation_not_only_result_conversion() { + STAGE_TIMINGS.with_borrow_mut(Vec::clear); + let value = stage_result( + || { + std::thread::sleep(Duration::from_millis(10)); + Ok::<_, ()>(42) + }, + "test_operation", + ); + assert_eq!(value, Some(42)); + STAGE_TIMINGS.with_borrow(|events| { + assert!( + events + .iter() + .any(|(stage, elapsed)| *stage == "test_operation" + && *elapsed >= Duration::from_millis(10)) + ); + }); + } + fn wire(id: &str, title: &str) -> Vec { let session = Session::create_with_id(id.into(), None, Some(title.into())); let mut bytes = serde_json::to_vec(&session).unwrap(); diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs index 2d2b3436a23..9a5a3657cd9 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/cache.rs @@ -14,7 +14,7 @@ const TTL: Duration = Duration::from_secs(15); pub(super) fn path(session_id: &str, host: &str, epoch: u64) -> Option { let mut timer = super::StageTimer::new("cache_scope"); - let root = super::stage_result(crate::storage::jcode_dir(), "cache_directory")?; + let root = super::stage_result(crate::storage::jcode_dir, "cache_directory")?; let owner = crate::subscription_catalog::configured_api_key().unwrap_or_default(); let endpoint = crate::subscription_api::configured_api_base(); let path = scoped_key_path(&root, session_id, host, epoch, &owner, &endpoint); @@ -67,14 +67,14 @@ pub(super) fn read(path: &Path) -> Option> { } Err(_) => return None, }; - if super::stage_result(file.metadata(), "cache_metadata")?.len() > MAX_BYTES as u64 { + if super::stage_result(|| file.metadata(), "cache_metadata")?.len() > MAX_BYTES as u64 { timer.outcome("oversized"); return None; } // Do not trust metadata for allocation or bounds: the file can grow concurrently. let mut bytes = Vec::new(); super::stage_result( - file.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes), + || file.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes), "cache_read_io", )?; timer.outcome(if bytes.len() <= MAX_BYTES { @@ -88,10 +88,10 @@ pub(super) fn read(path: &Path) -> Option> { pub(super) fn fresh(path: &Path) -> bool { let mut timer = super::StageTimer::new("cache_freshness"); let fresh = super::stage_result( - fs::metadata(path).and_then(|metadata| metadata.modified()), + || fs::metadata(path).and_then(|metadata| metadata.modified()), "cache_timestamp", ) - .and_then(|modified| super::stage_result(modified.elapsed(), "cache_age")) + .and_then(|modified| super::stage_result(|| modified.elapsed(), "cache_age")) .is_some_and(|age| age < TTL); timer.outcome(if fresh { "fresh" From 025d8793eca9062f7d86e3d1df4d481b249967f4 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:44:47 -0800 Subject: [PATCH 034/236] Fix CI: rustfmt and refresh size/swallowed-error budgets --- crates/jcode-app-core/src/server/client_lifecycle.rs | 5 ++++- crates/jcode-protocol/src/protocol_tests.rs | 7 +++---- scripts/code_size_budget.json | 10 +++++----- scripts/swallowed_error_budget.json | 10 +++++----- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/jcode-app-core/src/server/client_lifecycle.rs b/crates/jcode-app-core/src/server/client_lifecycle.rs index 16340719d2e..d0a28a8c26f 100644 --- a/crates/jcode-app-core/src/server/client_lifecycle.rs +++ b/crates/jcode-app-core/src/server/client_lifecycle.rs @@ -82,7 +82,10 @@ const REQUEST_HANDLER_STALL_THRESHOLDS_MS: [u64; 3] = [2_000, 10_000, 60_000]; mod subscribe_context; use subscribe_context::*; -fn event_for_transport(mut event: ServerEvent, supports_turn_identity: bool) -> Option { +fn event_for_transport( + mut event: ServerEvent, + supports_turn_identity: bool, +) -> Option { if supports_turn_identity { return Some(event); } diff --git a/crates/jcode-protocol/src/protocol_tests.rs b/crates/jcode-protocol/src/protocol_tests.rs index f2d52ccc097..24774f20aa7 100644 --- a/crates/jcode-protocol/src/protocol_tests.rs +++ b/crates/jcode-protocol/src/protocol_tests.rs @@ -101,9 +101,8 @@ fn swarm_live_status_defaults_and_roundtrips() { fn sdk_login_notify_is_accepted_without_subscribe() { // The SDK login flow sends exactly this frame on a fresh connection. // Requiring Subscribe first dropped it, so Desktop never saw new models. - let request: Request = serde_json::from_str( - r#"{"type":"notify_auth_changed","id":1,"provider":"claude"}"#, - ) - .unwrap(); + let request: Request = + serde_json::from_str(r#"{"type":"notify_auth_changed","id":1,"provider":"claude"}"#) + .unwrap(); assert!(request.is_lightweight_control_request()); } diff --git a/scripts/code_size_budget.json b/scripts/code_size_budget.json index c65229641f0..aa382d02801 100644 --- a/scripts/code_size_budget.json +++ b/scripts/code_size_budget.json @@ -8,14 +8,14 @@ "crates/jcode-app-core/src/overnight.rs": 1275, "crates/jcode-app-core/src/server.rs": 2543, "crates/jcode-app-core/src/server/client_actions.rs": 1335, - "crates/jcode-app-core/src/server/client_lifecycle.rs": 3481, + "crates/jcode-app-core/src/server/client_lifecycle.rs": 3484, "crates/jcode-app-core/src/server/client_session.rs": 2072, - "crates/jcode-app-core/src/server/client_state.rs": 1488, + "crates/jcode-app-core/src/server/client_state.rs": 1415, "crates/jcode-app-core/src/server/comm_control.rs": 2581, "crates/jcode-app-core/src/server/comm_session.rs": 1314, "crates/jcode-app-core/src/server/debug_server_state.rs": 1269, "crates/jcode-app-core/src/server/jade_relay.rs": 1338, - "crates/jcode-app-core/src/server/provider_control.rs": 1401, + "crates/jcode-app-core/src/server/provider_control.rs": 1447, "crates/jcode-app-core/src/server/swarm.rs": 2123, "crates/jcode-app-core/src/tool/bash.rs": 1502, "crates/jcode-app-core/src/tool/browser.rs": 1203, @@ -90,7 +90,7 @@ "crates/jcode-tui/src/tui/app/tui_lifecycle.rs": 1650, "crates/jcode-tui/src/tui/app/tui_state.rs": 2797, "crates/jcode-tui/src/tui/app/turn.rs": 1702, - "crates/jcode-tui/src/tui/backend.rs": 1611, + "crates/jcode-tui/src/tui/backend.rs": 1610, "crates/jcode-tui/src/tui/info_widget.rs": 2236, "crates/jcode-tui/src/tui/mod.rs": 2478, "crates/jcode-tui/src/tui/session_picker.rs": 2591, @@ -107,7 +107,7 @@ "src/bin/tui_bench.rs": 1765, "src/cli/acp.rs": 2053, "src/cli/args.rs": 1301, - "src/cli/cloud_move.rs": 2788, + "src/cli/cloud_move.rs": 2776, "src/cli/commands.rs": 3584, "src/cli/dispatch.rs": 1533, "src/cli/provider_init.rs": 1955, diff --git a/scripts/swallowed_error_budget.json b/scripts/swallowed_error_budget.json index faf47d1299a..5c609a41fb1 100644 --- a/scripts/swallowed_error_budget.json +++ b/scripts/swallowed_error_budget.json @@ -1,9 +1,9 @@ { - "total": 3878, + "total": 3877, "totals_by_pattern": { "dot_ok": 1529, - "let_underscore": 1409, - "unwrap_or_default": 940 + "let_underscore": 1410, + "unwrap_or_default": 938 }, "tracked_files": { "crates/jcode-app-core/src/agent.rs": { @@ -219,7 +219,7 @@ "crates/jcode-app-core/src/server/client_state.rs": { "dot_ok": 4, "let_underscore": 0, - "unwrap_or_default": 6 + "unwrap_or_default": 4 }, "crates/jcode-app-core/src/server/comm_await.rs": { "dot_ok": 0, @@ -313,7 +313,7 @@ }, "crates/jcode-app-core/src/server/provider_control.rs": { "dot_ok": 0, - "let_underscore": 31, + "let_underscore": 32, "unwrap_or_default": 0 }, "crates/jcode-app-core/src/server/reload.rs": { From e2e2c32f939a621978d9ccc80d3d310d6a24119d Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 23:58:40 -0800 Subject: [PATCH 035/236] Mobile push: fix 400s from u64 turn ids, readable bodies (final answer lead, questions as needs_input, clean errors, parsed background-task wakes, word-boundary truncation, titles for wakes) --- crates/jcode-app-core/src/agent/status.rs | 24 ++ crates/jcode-app-core/src/mobile_push.rs | 364 +++++++++++++++++- .../src/server/client_processing.rs | 7 +- 3 files changed, 389 insertions(+), 6 deletions(-) diff --git a/crates/jcode-app-core/src/agent/status.rs b/crates/jcode-app-core/src/agent/status.rs index 9d85f3767c6..0af5be85e91 100644 --- a/crates/jcode-app-core/src/agent/status.rs +++ b/crates/jcode-app-core/src/agent/status.rs @@ -177,6 +177,30 @@ impl Agent { assistant_output_tail_after(&self.session.messages, start_index) } + /// The final visible assistant prose of this turn (its answer), used for + /// notification previews. Tool calls and reasoning are excluded. + pub fn final_assistant_text_after(&self, start_index: usize) -> Option { + self.session + .messages + .iter() + .skip(start_index) + .rev() + .filter(|message| message.role == Role::Assistant) + .find_map(|message| { + let text = message + .content + .iter() + .filter_map(|block| match block { + ContentBlock::Text { text, .. } => Some(text.trim()), + _ => None, + }) + .filter(|text| !text.is_empty()) + .collect::>() + .join("\n\n"); + (!text.is_empty()).then_some(text) + }) + } + pub fn last_upstream_provider(&self) -> Option { self.last_upstream_provider .clone() diff --git a/crates/jcode-app-core/src/mobile_push.rs b/crates/jcode-app-core/src/mobile_push.rs index f00b4b688f1..12a0de2fb38 100644 --- a/crates/jcode-app-core/src/mobile_push.rs +++ b/crates/jcode-app-core/src/mobile_push.rs @@ -50,8 +50,22 @@ pub fn build_payload( let title = session_title .map(str::trim) .filter(|t| !t.is_empty()) - .map(|t| truncate_chars(t, 120)); - let collapsed = body.split_whitespace().collect::>().join(" "); + .map(|t| truncate_chars(&plain_inline(t), 120)) + .or_else(|| lookup_session_title(session_id)); + // An answer that ends by asking the user something is waiting on them. + let (kind, summary) = match kind { + PushKind::TurnDone => match trailing_question(body) { + Some(question) => (PushKind::NeedsInput, question), + None => (kind, summarize_answer(body)), + }, + PushKind::NeedsInput => ( + kind, + trailing_question(body).unwrap_or_else(|| summarize_answer(body)), + ), + PushKind::TurnError => (kind, summarize_error(body)), + PushKind::Wake => (kind, summarize_wake(body)), + }; + let collapsed = summary.split_whitespace().collect::>().join(" "); let body = if collapsed.is_empty() { match kind { PushKind::TurnDone => "Turn finished".to_string(), @@ -60,17 +74,289 @@ pub fn build_payload( PushKind::Wake => "Session update".to_string(), } } else { - truncate_chars(&collapsed, BODY_MAX_CHARS) + truncate_words(&collapsed, BODY_MAX_CHARS) }; PushPayload { kind, session_id: session_id.to_string(), session_title: title, body, - turn_id, + // Message ids are random u64s. JSON numbers above 2^53 lose precision + // in JavaScript and the API rejects them, so fold into the safe range. + turn_id: turn_id.map(|id| id & JS_SAFE_INT_MASK), + } +} + +const JS_SAFE_INT_MASK: u64 = (1 << 53) - 1; + +fn lookup_session_title(session_id: &str) -> Option { + if cfg!(test) || session_id.is_empty() { + return None; + } + crate::process_title::terminal_display_title_for_id(session_id) + .map(|t| plain_inline(t.trim())) + .filter(|t| !t.is_empty()) + .map(|t| truncate_chars(&t, 120)) +} + +/// Strip inline Markdown so a notification reads as plain prose. +fn plain_inline(text: &str) -> String { + static LINK: std::sync::OnceLock = std::sync::OnceLock::new(); + static EMPH: std::sync::OnceLock = std::sync::OnceLock::new(); + let link = LINK.get_or_init(|| regex::Regex::new(r"!?\[([^\]]*)\]\([^)]*\)").unwrap()); + let emph = EMPH.get_or_init(|| { + regex::Regex::new(r"\*\*([^*]+)\*\*|__([^_]+)__|\*([^*\s][^*]*)\*|~~([^~]+)~~").unwrap() + }); + let text = link.replace_all(text, "$1"); + let text = emph.replace_all(&text, "$1$2$3$4"); + text.replace('`', "") +} + +/// Turn Markdown into plain lines: drop code blocks, tables, rules and +/// headings markers, list bullets and inline emphasis. +fn plain_lines(text: &str) -> Vec { + let mut out = Vec::new(); + let mut in_fence = false; + for raw in text.lines() { + let line = raw.trim(); + if line.starts_with("```") || line.starts_with("~~~") { + in_fence = !in_fence; + continue; + } + if in_fence || line.is_empty() { + if !in_fence && out.last().is_some_and(|l: &String| !l.is_empty()) { + out.push(String::new()); + } + continue; + } + if line.starts_with('|') + || line + .chars() + .all(|c| matches!(c, '-' | '*' | '_' | '=' | ' ')) + { + continue; + } + // Headings are labels ("Summary"), not content. + if line.starts_with('#') { + continue; + } + let line = line.trim_start_matches('>').trim(); + let line = strip_list_marker(line); + let line = plain_inline(line); + let line = line.trim(); + if !line.is_empty() { + out.push(line.to_string()); + } + } + while out.last().is_some_and(String::is_empty) { + out.pop(); + } + out +} + +fn strip_list_marker(line: &str) -> &str { + for marker in ["- [ ] ", "- [x] ", "- ", "* ", "+ ", "• "] { + if let Some(rest) = line.strip_prefix(marker) { + return rest; + } + } + let digits = line.chars().take_while(char::is_ascii_digit).count(); + if digits > 0 { + let rest = &line[digits..]; + if let Some(rest) = rest.strip_prefix(". ").or_else(|| rest.strip_prefix(") ")) { + return rest; + } + } + line +} + +/// Lead of a final answer: the first paragraph, which by convention carries +/// the conclusion. Bullet items are joined into one sentence-like line. +fn summarize_answer(text: &str) -> String { + let lines = plain_lines(text); + let mut para = Vec::new(); + for line in &lines { + if line.is_empty() { + if !para.is_empty() && para.iter().map(|l: &&String| l.len()).sum::() >= 40 { + break; + } + continue; + } + para.push(line); + } + join_sentences(para.iter().map(|l| l.as_str())) +} + +fn join_sentences<'a>(parts: impl Iterator) -> String { + let mut out = String::new(); + for part in parts { + if !out.is_empty() { + if !out.ends_with(['.', '!', '?', ':', ';']) { + out.push('.'); + } + out.push(' '); + } + out.push_str(part); + } + out +} + +/// If the answer ends by asking the user a question, return that question. +fn trailing_question(text: &str) -> Option { + let lines = plain_lines(text); + let last = lines.iter().rev().find(|l| !l.is_empty())?; + if !last.trim_end_matches(['*', '_', ')']).ends_with('?') { + return None; + } + // Keep the last sentence, plus the one before if it sets up the question. + let mut start = 0; + for (i, c) in last.char_indices() { + if matches!(c, '.' | '!' | '?') && i + 1 < last.len() - 1 { + let next = last[i + c.len_utf8()..].chars().next(); + if next.is_some_and(char::is_whitespace) { + start = i + c.len_utf8(); + } + } + } + let question = last[start..].trim(); + (question.chars().count() >= 8).then(|| question.to_string()) +} + +/// Errors arrive as `context: cause: cause`. Drop JSON blobs and noise, keep +/// the most specific readable cause. +fn summarize_error(text: &str) -> String { + static JSON: std::sync::OnceLock = std::sync::OnceLock::new(); + let json = JSON.get_or_init(|| regex::Regex::new(r"\{.*\}").unwrap()); + let flat = text.split_whitespace().collect::>().join(" "); + // Pull a human message out of an embedded provider JSON error if present. + let message = json.find(&flat).and_then(|m| { + serde_json::from_str::(m.as_str()) + .ok() + .and_then(|v| { + v.pointer("/error/message") + .or_else(|| v.get("message")) + .and_then(|m| m.as_str()) + .map(str::to_string) + }) + }); + let stripped = json.replace_all(&flat, ""); + let mut parts: Vec<&str> = stripped + .split(": ") + .map(|p| p.trim().trim_end_matches([':', ','])) + .filter(|p| !p.is_empty()) + .collect(); + parts.dedup(); + let head = parts.join(": "); + match message { + Some(m) if !head.contains(m.as_str()) => { + if head.is_empty() { + m + } else { + format!("{m} ({head})") + } + } + _ => head, } } +/// Wake notifications are agent-facing Markdown. Rewrite the known shapes +/// into one readable line and strip instructions meant for the model. +fn summarize_wake(text: &str) -> String { + if let Some(task) = crate::message::parse_background_task_notification_markdown(text) { + let label = task + .display_name + .clone() + .unwrap_or_else(|| task.tool_name.clone()); + let status = task.status.trim_start_matches(['✓', '✗', '↻', ' ']).trim(); + let duration = humanize_secs(&task.duration); + let mut line = format!("{label} {status} in {duration}"); + if status == "failed" + && task.exit_label.starts_with("exit ") + && task.exit_label != "exit n/a" + { + line.push_str(&format!(" ({})", task.exit_label)); + } + let detail = task + .failure_summary + .as_deref() + .map(plain_inline) + .or_else(|| task.preview.as_deref().and_then(last_meaningful_line)); + if let Some(detail) = detail.filter(|d| !d.trim().is_empty()) { + line.push_str(": "); + line.push_str(detail.trim()); + } + return line; + } + if let Some(rest) = text.trim().strip_prefix("**Background task stalled** ") { + let header = rest.lines().next().unwrap_or_default(); + let label = header + .split(" · ") + .nth(1) + .map(plain_inline) + .unwrap_or_default(); + let label = label + .split(" (") + .next() + .unwrap_or_default() + .trim() + .to_string(); + let idle = header + .split("no output or progress for ") + .nth(1) + .and_then(|s| s.split(' ').next()) + .map(humanize_secs) + .unwrap_or_default(); + return format!("{label} has been silent for {idle} and may be stuck") + .trim() + .to_string(); + } + let lines: Vec = plain_lines(text) + .into_iter() + .filter(|l| !l.is_empty() && !l.contains("action=\"") && !l.starts_with("Full output:")) + .collect(); + join_sentences(lines.iter().map(String::as_str)) +} + +fn last_meaningful_line(text: &str) -> Option { + text.lines() + .map(str::trim) + .rev() + .find(|l| l.chars().filter(|c| c.is_alphanumeric()).count() >= 4) + .map(|l| truncate_chars(l, 120)) +} + +fn humanize_secs(value: &str) -> String { + let Ok(secs) = value.trim_end_matches('s').parse::() else { + return value.to_string(); + }; + let secs = secs.round() as u64; + match secs { + 0..=59 => format!("{secs}s"), + 60..=3599 => match secs % 60 { + 0 => format!("{}m", secs / 60), + s => format!("{}m {s}s", secs / 60), + }, + _ => format!("{}h {}m", secs / 3600, (secs % 3600) / 60), + } +} + +/// Truncate at a word boundary so previews never end mid-word. +fn truncate_words(text: &str, max: usize) -> String { + if text.chars().count() <= max { + return text.to_string(); + } + let head: String = text.chars().take(max.saturating_sub(1)).collect(); + let cut = head + .rfind(char::is_whitespace) + .filter(|&i| i >= head.len() * 2 / 3) + .unwrap_or(head.len()); + let mut out = head[..cut] + .trim_end_matches([',', ';', ':', ' ', '-']) + .to_string(); + out.push('…'); + out +} + /// Last `max` chars are most useful for an assistant reply preview? No: the /// start of a sentence reads better, so keep the head. fn truncate_chars(text: &str, max: usize) -> String { @@ -463,6 +749,76 @@ mod tests { ); } + #[test] + fn mobile_push_turn_id_is_js_safe() { + let p = build_payload( + PushKind::TurnDone, + "s", + None, + "ok", + Some(8338987288606337570), + ); + assert!(p.turn_id.unwrap() <= (1 << 53) - 1); + assert_eq!( + build_payload(PushKind::TurnDone, "s", None, "ok", Some(42)).turn_id, + Some(42) + ); + } + + #[test] + fn mobile_push_answer_uses_lead_paragraph_as_plain_text() { + let answer = "## Summary\n\nFixed the **login** bug in `auth.rs`; all 12 tests pass.\n\n- detail one\n- detail two\n\n```rust\nfn x() {}\n```"; + let p = build_payload(PushKind::TurnDone, "s", Some("Fix `login`"), answer, None); + assert_eq!(p.body, "Fixed the login bug in auth.rs; all 12 tests pass."); + assert_eq!(p.session_title.as_deref(), Some("Fix login")); + let bullets = "- Updated parser\n- Added tests\n- Shipped v2 to production servers today"; + assert_eq!( + build_payload(PushKind::TurnDone, "s", None, bullets, None).body, + "Updated parser. Added tests. Shipped v2 to production servers today" + ); + } + + #[test] + fn mobile_push_trailing_question_becomes_needs_input() { + let p = build_payload( + PushKind::TurnDone, + "s", + None, + "I found two options.\n\nShould I use OAuth or a password?", + None, + ); + assert_eq!(p.kind, PushKind::NeedsInput); + assert_eq!(p.body, "Should I use OAuth or a password?"); + let p = build_payload( + PushKind::TurnDone, + "s", + None, + "Done. Want me to deploy it?", + None, + ); + assert_eq!( + (p.kind, p.body.as_str()), + (PushKind::NeedsInput, "Want me to deploy it?") + ); + } + + #[test] + fn mobile_push_errors_and_wakes_are_readable() { + let e = r#"Provider request failed: HTTP 529: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}"#; + assert_eq!( + build_payload(PushKind::TurnError, "s", None, e, None).body, + "Overloaded (Provider request failed: HTTP 529)" + ); + let bg = "**Background task** `abc123` · `bash` · ✓ completed · 754.2s · exit 0\n\n```text\nCompiling...\ntest result: ok. 42 passed\n```\n\n_Full output:_ `bg action=\"output\" task_id=\"abc123\"`"; + assert_eq!( + build_payload(PushKind::Wake, "s", None, bg, None).body, + "bash completed in 12m 34s: test result: ok. 42 passed" + ); + let long = "word ".repeat(60); + let p = build_payload(PushKind::Wake, "s", None, &long, None); + assert!(p.body.ends_with("word…")); + } + #[test] fn mobile_push_suppression_window() { let now = Instant::now(); diff --git a/crates/jcode-app-core/src/server/client_processing.rs b/crates/jcode-app-core/src/server/client_processing.rs index 0fd75791a45..d4fe3e09339 100644 --- a/crates/jcode-app-core/src/server/client_processing.rs +++ b/crates/jcode-app-core/src/server/client_processing.rs @@ -340,10 +340,13 @@ pub(super) async fn start_processing_message( id, error )), } - let (completion_report, push_title) = { + let (completion_report, push_body, push_title) = { let agent = report_agent.lock().await; ( agent.recent_assistant_output_tail_after(start_message_index), + agent + .final_assistant_text_after(start_message_index) + .unwrap_or_default(), agent.session_display_title_or_name(), ) }; @@ -361,7 +364,7 @@ pub(super) async fn start_processing_message( crate::mobile_push::PushKind::TurnDone, &push_session_id, Some(&push_title), - completion_report.as_deref().unwrap_or_default(), + &push_body, Some(id), ); } From 9b8c8529493624300c283f90c5e2286889d5675a Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sat, 7 Nov 2026 23:58:56 -0800 Subject: [PATCH 036/236] Show cloud repo git status on overscroll line --- crates/jcode-tui/src/tui/ui_input.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index 27902b75919..98d05020175 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -1193,7 +1193,7 @@ pub(super) fn draw_overscroll_status(frame: &mut Frame, app: &dyn TuiState, area app.working_dir().as_deref(), ), branch: overscroll_git_branch(&data), - git: data.git_info.clone(), + git: overscroll_git_info(&data).cloned(), context: overscroll_context_usage(&data), account_remaining: overscroll_account_remaining(&data), auth: overscroll_auth_label(data.auth_method), @@ -1246,7 +1246,7 @@ pub(crate) fn preview_overscroll_line_for_host( data.working_dir.as_deref(), ), branch: overscroll_git_branch(data), - git: data.git_info.clone(), + git: overscroll_git_info(data).cloned(), context: overscroll_context_usage(data), account_remaining: overscroll_account_remaining(data), auth: overscroll_auth_label(data.auth_method), @@ -1551,10 +1551,23 @@ fn overscroll_truncate_spans(spans: Vec>, max_width: usize) -> Vec } /// Format a working dir path home-relative (~/foo/bar), keeping the last 2 segments. +/// Git facts for the status line. Cloud sessions keep their repository in +/// `dual_git_info.cloud`, so prefer that over the (absent) local `git_info`. +fn overscroll_git_info( + data: &crate::tui::info_widget::InfoWidgetData, +) -> Option<&crate::tui::info_widget::GitInfo> { + if let Some(dual) = &data.dual_git_info + && let crate::tui::cloud_git::RepoState::Ready(info) = &dual.cloud + { + return Some(info); + } + data.git_info.as_ref() +} + /// Compact git branch label for the status line and fact stack. Truncated so /// long branch names cannot crowd out the other facts. fn overscroll_git_branch(data: &crate::tui::info_widget::InfoWidgetData) -> Option { - let branch = data.git_info.as_ref()?.branch.trim(); + let branch = overscroll_git_info(data)?.branch.trim(); if branch.is_empty() { return None; } From 92ca1819ad10e112471f60088ea51084df9da563 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:03:40 -0800 Subject: [PATCH 037/236] feat(swarm): auto-dispatch workers for worker-owned todo lanes, replacing run_plan --- .../jcode-app-core/src/agent/tool_handoff.rs | 4 +- crates/jcode-app-core/src/server.rs | 20 + .../src/server/lane_dispatch.rs | 298 ++++ .../src/server/lane_dispatch_tests.rs | 95 ++ crates/jcode-app-core/src/server/swarm.rs | 2 +- .../src/server/swarm_todo_link.rs | 19 + crates/jcode-app-core/src/tool/communicate.rs | 1328 +---------------- .../src/tool/communicate_schema_tests.rs | 32 +- .../src/tool/communicate_tests.rs | 496 +----- .../src/tool/communicate_tests/assignment.rs | 94 -- .../communicate_tests/credential_failures.rs | 177 --- .../src/tool/communicate_tests/end_to_end.rs | 57 - .../tool/communicate_tests/input_format.rs | 6 +- crates/jcode-app-core/src/tool/todo.rs | 42 +- .../src/tool/todo_lane_dependency_tests.rs | 41 + crates/jcode-app-core/src/tool/todo_tests.rs | 3 +- crates/jcode-base/src/mobile_sync_tests.rs | 41 + crates/jcode-base/src/process_title.rs | 1 + crates/jcode-base/src/todo.rs | 1 + crates/jcode-task-types/src/lib.rs | 8 + .../src/completion_report_tests.rs | 1 + crates/jcode-todo-policy/src/lib.rs | 4 +- crates/jcode-todo-policy/src/stages.rs | 35 + crates/jcode-todo-policy/src/tests.rs | 1 + .../part_01/improvement_commands.rs | 3 + .../app/tests/commands_accounts_02/part_02.rs | 3 + .../tests/remote_events_reload_01/part_01.rs | 1 + .../tests/remote_events_reload_01/part_02.rs | 4 + .../tests/remote_events_reload_02/part_01.rs | 4 + .../tests/remote_startup_input_01/part_01.rs | 1 + .../tests/remote_startup_input_02/part_01.rs | 2 + .../tui/app/tests/scroll_copy_01/part_02.rs | 1 + .../app/tests/state_model_poke_02/part_01.rs | 1 + .../src/tui/app/tests/state_model_poke_03.rs | 13 + .../jcode-tui/src/tui/app/tests/todo_card.rs | 3 + crates/jcode-tui/src/tui/app/todos_view.rs | 1 + crates/jcode-tui/src/tui/app/turn_notify.rs | 1 + .../src/tui/session_picker/loading_tests.rs | 2 + crates/jcode-tui/src/tui/ui_messages/tests.rs | 8 + crates/jcode-tui/src/tui/ui_todo_changes.rs | 1 + src/cli/commands/menubar_tests.rs | 1 + 41 files changed, 680 insertions(+), 2176 deletions(-) create mode 100644 crates/jcode-app-core/src/server/lane_dispatch.rs create mode 100644 crates/jcode-app-core/src/server/lane_dispatch_tests.rs delete mode 100644 crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs diff --git a/crates/jcode-app-core/src/agent/tool_handoff.rs b/crates/jcode-app-core/src/agent/tool_handoff.rs index 90c47319289..333ae4f2122 100644 --- a/crates/jcode-app-core/src/agent/tool_handoff.rs +++ b/crates/jcode-app-core/src/agent/tool_handoff.rs @@ -80,8 +80,8 @@ pub(super) fn reload_interrupted_tool_result(tc: &ToolCall, elapsed_secs: f64) - .get("action") .and_then(|value| value.as_str()) .unwrap_or(""); - let is_wait_like = (tc.name == "bg" && action == "wait") - || (tc.name == "swarm" && matches!(action, "await_members" | "run_plan")); + let is_wait_like = + (tc.name == "bg" && action == "wait") || (tc.name == "swarm" && action == "await_members"); if is_wait_like { let input = serde_json::to_string(&tc.input).unwrap_or_else(|_| "{}".to_string()); diff --git a/crates/jcode-app-core/src/server.rs b/crates/jcode-app-core/src/server.rs index 13904abd87b..eace686427e 100644 --- a/crates/jcode-app-core/src/server.rs +++ b/crates/jcode-app-core/src/server.rs @@ -47,6 +47,7 @@ pub use group_transfer::{GroupTransferBundle, GroupTransferSelection, GroupTrans pub use swarm_persistence::{PersistedSwarmMember, PersistedSwarmState, PersistedVersionedPlan}; mod idle_hibernation; mod jade_relay; +mod lane_dispatch; mod lifecycle; mod live_status; mod live_turn; @@ -1138,6 +1139,22 @@ impl Server { shutdown_signals: Arc::clone(&self.shutdown_signals), soft_interrupt_queues: Arc::clone(&self.soft_interrupt_queues), }); + lane_dispatch::register_context(lane_dispatch::LaneDispatchContext { + provider: Arc::clone(&self.provider), + sessions: Arc::clone(&self.sessions), + global_session_id: Arc::clone(&self.session_id), + swarm_state: self.swarm_state.clone(), + channel_subscriptions: Arc::clone(&self.channel_subscriptions), + channel_subscriptions_by_session: Arc::clone(&self.channel_subscriptions_by_session), + event_history: Arc::clone(&self.event_history), + event_counter: Arc::clone(&self.event_counter), + swarm_event_tx: self.swarm_event_tx.clone(), + mcp_pool: Arc::clone(&self.mcp_pool), + soft_interrupt_queues: Arc::clone(&self.soft_interrupt_queues), + swarm_mutation_runtime: self.swarm_mutation_runtime.clone(), + client_connections: Arc::clone(&self.client_connections), + }); + lane_dispatch::spawn_startup_scan(Duration::from_secs(5)); // Every resume History asks whether the session has persisted // background tasks. The first ask after a reload scans the whole task // directory (~250ms) while clients are reconnecting; build the index @@ -2288,6 +2305,9 @@ impl Server { // swarm strip so a coordinator can see each managed agent's work. BusEvent::TodoUpdated(event) => { dispatch_swarm_todo_progress(&event, &swarm_members, &swarms_by_id).await; + // Worker-owned lanes whose dependencies are met get a + // worker automatically; no launch call is involved. + self::lane_dispatch::on_todos_saved(&event.session_id, &event.todos); // A coordinator's todo list is its lane plan: keep a // running plan mirroring every save, so new or reshaped // lanes are picked up without relaunching. diff --git a/crates/jcode-app-core/src/server/lane_dispatch.rs b/crates/jcode-app-core/src/server/lane_dispatch.rs new file mode 100644 index 00000000000..83db6498577 --- /dev/null +++ b/crates/jcode-app-core/src/server/lane_dispatch.rs @@ -0,0 +1,298 @@ +//! Automatic worker dispatch for worker-owned todo lanes. +//! +//! A root session marks a todo lane `owner: "worker"`. Whenever its todo list +//! is saved (including mirrored saves when a worker finishes a lane item), the +//! server spawns one swarm worker for every worker-owned lane whose +//! dependencies are satisfied and that has no owner yet. There is no launch +//! call and no per-plan concurrency number: lane dependencies decide when a +//! lane starts, and the global `agents.swarm_max_concurrent_agents` cap holds +//! back the rest as queued until capacity frees up. +//! +//! The spawned worker is recorded on the lane's `assigned_to` before the save +//! that follows, so repeated saves, plan edits and server reloads never spawn +//! a duplicate for the same lane. + +use super::comm_session::handle_comm_spawn; +use super::swarm_mutation_state::SwarmMutationRuntime; +use super::{ + ChannelSubscriptions, ClientConnectionInfo, SessionAgents, SessionInterruptQueues, SwarmEvent, + SwarmMember, SwarmState, +}; +use crate::protocol::ServerEvent; +use crate::provider::Provider; +use crate::todo::TodoItem; +use std::collections::{HashMap, HashSet}; +use std::sync::{Arc, Mutex as StdMutex, OnceLock}; +use std::time::Duration; +use tokio::sync::{OnceCell, RwLock, broadcast, mpsc}; + +/// Server-owned handles the dispatcher needs to spawn workers. Registered once +/// when the server starts; without it, dispatch is a no-op (tests, embedders). +pub(super) struct LaneDispatchContext { + pub provider: Arc, + pub sessions: SessionAgents, + pub global_session_id: Arc>, + pub swarm_state: SwarmState, + pub channel_subscriptions: ChannelSubscriptions, + pub channel_subscriptions_by_session: ChannelSubscriptions, + pub event_history: Arc>>, + pub event_counter: Arc, + pub swarm_event_tx: broadcast::Sender, + pub mcp_pool: Arc>>, + pub soft_interrupt_queues: SessionInterruptQueues, + pub swarm_mutation_runtime: SwarmMutationRuntime, + pub client_connections: Arc>>, +} + +static CONTEXT: OnceLock = OnceLock::new(); + +pub(super) fn register_context(context: LaneDispatchContext) { + let _ = CONTEXT.set(context); +} + +/// After a server start or reload, re-run dispatch for every root member so +/// lanes that became ready while the server was down (or whose spawn was cut +/// off mid-flight) still get a worker. Lanes already owned are skipped by the +/// persisted `assigned_to`, so this never duplicates a worker. +pub(super) fn spawn_startup_scan(delay: Duration) { + let Some(context) = CONTEXT.get() else { + return; + }; + let members = Arc::clone(&context.swarm_state.members); + tokio::spawn(async move { + tokio::time::sleep(delay).await; + let roots: Vec = members + .read() + .await + .values() + .filter(|member| member.report_back_to_session_id.is_none()) + .map(|member| member.session_id.clone()) + .collect(); + for session_id in roots { + if let Ok(todos) = crate::todo::load_todos(&session_id) { + on_todos_saved(&session_id, &todos); + } + } + }); +} + +/// Lanes with a spawn in flight, keyed by `(coordinator, lane id)`. The +/// assignment is persisted by the spawn itself; this only covers the window +/// before that save lands. +fn in_flight() -> &'static StdMutex> { + static IN_FLIGHT: OnceLock>> = OnceLock::new(); + IN_FLIGHT.get_or_init(|| StdMutex::new(HashSet::new())) +} + +/// Coordinators with lanes held back by the live-agent cap. Retried on a +/// timer because capacity frees up through events in other sessions. +fn queued_sessions() -> &'static StdMutex> { + static QUEUED: OnceLock>> = OnceLock::new(); + QUEUED.get_or_init(|| StdMutex::new(HashSet::new())) +} + +const QUEUED_RETRY_INTERVAL: Duration = Duration::from_secs(10); + +/// Ready, unowned, worker-owned lanes of `todos`, in plan order. +/// +/// A lane is ready when its plan item has no open blockers. It is unowned when +/// no open member of the lane has `assigned_to` set. +pub(crate) fn dispatchable_lanes(todos: &[TodoItem]) -> Vec { + let open = |todo: &&TodoItem| !matches!(todo.status.as_str(), "completed" | "cancelled"); + super::swarm_todo_link::plan_items_from_todos(todos) + .into_iter() + .filter(|item| item.blocked_by.is_empty()) + .filter(|item| { + let members: Vec<&TodoItem> = todos + .iter() + .filter(open) + .filter(|todo| crate::todo::stages::lane_id(todo) == item.id) + .collect(); + !members.is_empty() + && members + .iter() + .any(|todo| crate::todo::stages::lane_requests_worker(todo)) + && members.iter().all(|todo| todo.assigned_to.is_none()) + }) + .map(|item| item.id) + .collect() +} + +/// React to a todo save: spawn workers for newly dispatchable lanes. +pub(super) fn on_todos_saved(session_id: &str, todos: &[TodoItem]) { + if CONTEXT.get().is_none() { + return; + } + let lanes = dispatchable_lanes(todos); + if lanes.is_empty() { + queued_sessions() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(session_id); + return; + } + let session_id = session_id.to_string(); + tokio::spawn(async move { dispatch(session_id, lanes).await }); +} + +async fn dispatch(session_id: String, lanes: Vec) { + let Some(context) = CONTEXT.get() else { + return; + }; + // Only a root session (no spawner) coordinates worker lanes. Workers get + // their lane with owners cleared, so this is defense in depth. + let is_root = context + .swarm_state + .members + .read() + .await + .get(&session_id) + .is_some_and(|member: &SwarmMember| member.report_back_to_session_id.is_none()); + if !is_root { + return; + } + for lane in lanes { + let key = (session_id.clone(), lane.clone()); + if !in_flight() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .insert(key.clone()) + { + continue; + } + // Re-read from disk: the triggering event may predate an assignment + // persisted by an earlier dispatch of the same lane. + let still_dispatchable = crate::todo::load_todos(&session_id) + .map(|todos| dispatchable_lanes(&todos).contains(&lane)) + .unwrap_or(false); + if !still_dispatchable { + in_flight() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(&key); + continue; + } + let outcome = spawn_lane_worker(context, &session_id, &lane).await; + in_flight() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(&key); + match outcome { + Ok(worker) => crate::logging::info(&format!( + "lane dispatch: {session_id} lane {lane} -> worker {worker}" + )), + Err(message) if is_capacity_error(&message) => { + crate::logging::info(&format!( + "lane dispatch: {session_id} lane {lane} queued: {message}" + )); + schedule_queued_retry(&session_id); + // Later lanes would hit the same cap. + break; + } + Err(message) => crate::logging::warn(&format!( + "lane dispatch: {session_id} lane {lane} failed: {message}" + )), + } + } +} + +fn is_capacity_error(message: &str) -> bool { + message.contains("live-agent limit reached") || message.contains("member limit reached") +} + +fn schedule_queued_retry(session_id: &str) { + let newly_queued = queued_sessions() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .insert(session_id.to_string()); + if !newly_queued { + return; + } + let session_id = session_id.to_string(); + tokio::spawn(async move { + loop { + tokio::time::sleep(QUEUED_RETRY_INTERVAL).await; + let still_queued = queued_sessions() + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .remove(&session_id); + if !still_queued { + return; + } + let Ok(todos) = crate::todo::load_todos(&session_id) else { + return; + }; + let lanes = dispatchable_lanes(&todos); + if lanes.is_empty() { + return; + } + // dispatch re-inserts the session when the cap still holds; that + // insert starts a fresh retry loop, so this one ends. + dispatch(session_id.clone(), lanes).await; + return; + } + }); +} + +async fn spawn_lane_worker( + context: &LaneDispatchContext, + session_id: &str, + lane: &str, +) -> Result { + let (tx, mut rx) = mpsc::unbounded_channel::(); + let mcp_pool = super::get_shared_mcp_pool(&context.mcp_pool).await; + handle_comm_spawn( + 0, + session_id.to_string(), + Some(lane.to_string()), + None, + None, + // A fresh nonce per attempt: a lane that was reopened after its + // worker ended must get a new worker, not a replayed old response. + // Duplicate dispatch is prevented by `in_flight` and `assigned_to`. + Some(format!( + "lane-dispatch:{lane}:{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_nanos()) + .unwrap_or_default() + )), + None, + None, + None, + None, + &tx, + &context.sessions, + &context.global_session_id, + &context.provider, + &context.swarm_state.members, + &context.swarm_state.swarms_by_id, + &context.swarm_state.coordinators, + &context.swarm_state.plans, + &context.channel_subscriptions, + &context.channel_subscriptions_by_session, + &context.event_history, + &context.event_counter, + &context.swarm_event_tx, + &mcp_pool, + &context.soft_interrupt_queues, + &context.swarm_mutation_runtime, + &context.client_connections, + ) + .await; + drop(tx); + match rx.recv().await { + Some(ServerEvent::CommSpawnResponse { new_session_id, .. }) + if !new_session_id.is_empty() => + { + Ok(new_session_id) + } + Some(ServerEvent::Error { message, .. }) => Err(message), + Some(other) => Err(format!("unexpected spawn response: {other:?}")), + None => Err("spawn produced no response".to_string()), + } +} + +#[cfg(test)] +#[path = "lane_dispatch_tests.rs"] +mod tests; diff --git a/crates/jcode-app-core/src/server/lane_dispatch_tests.rs b/crates/jcode-app-core/src/server/lane_dispatch_tests.rs new file mode 100644 index 00000000000..4cc3da64f23 --- /dev/null +++ b/crates/jcode-app-core/src/server/lane_dispatch_tests.rs @@ -0,0 +1,95 @@ +use super::dispatchable_lanes; +use crate::todo::TodoItem; + +fn item(id: &str, stage: u32, lane: &str, owner: Option<&str>) -> TodoItem { + TodoItem { + id: id.to_string(), + content: format!("task {id}"), + status: "pending".to_string(), + priority: "high".to_string(), + stage: Some(stage), + group: Some(lane.to_string()), + lane_owner: owner.map(str::to_string), + ..Default::default() + } +} + +#[test] +fn self_owned_lanes_never_dispatch() { + let todos = vec![item("a", 1, "api", None), item("b", 1, "docs", None)]; + assert!(dispatchable_lanes(&todos).is_empty()); +} + +#[test] +fn ready_worker_lanes_in_one_phase_start_together() { + let todos = vec![ + item("a", 1, "api", Some("worker")), + item("b", 1, "docs", Some("worker")), + item("c", 1, "mine", None), + ]; + let lanes = dispatchable_lanes(&todos); + assert_eq!(lanes.len(), 2, "{lanes:?}"); +} + +#[test] +fn later_phase_waits_then_unlocks_on_completion() { + let mut todos = vec![ + item("a", 1, "api", None), + item("b", 2, "client", Some("worker")), + ]; + assert!(dispatchable_lanes(&todos).is_empty()); + todos[0].status = "completed".to_string(); + assert_eq!(dispatchable_lanes(&todos).len(), 1); +} + +#[test] +fn explicit_dependency_unlocks_lane_early() { + let mut consumer = item("c", 2, "client", Some("worker")); + consumer.lane_after = Some(vec!["a".into()]); + let mut todos = vec![ + item("a", 1, "api", None), + item("b", 1, "api", None), + consumer, + ]; + assert!(dispatchable_lanes(&todos).is_empty()); + todos[0].status = "completed".to_string(); + assert_eq!(dispatchable_lanes(&todos).len(), 1); +} + +#[test] +fn owned_lane_is_not_dispatched_again() { + let mut todos = vec![ + item("a", 1, "api", Some("worker")), + item("b", 1, "api", Some("worker")), + ]; + assert_eq!(dispatchable_lanes(&todos).len(), 1); + todos[0].assigned_to = Some("worker-1".into()); + todos[1].assigned_to = Some("worker-1".into()); + // Plan edits and reloads re-evaluate the saved list; the persisted owner + // keeps the lane from getting a second worker. + todos.push({ + let mut extra = item("x", 1, "api", Some("worker")); + extra.assigned_to = Some("worker-1".into()); + extra + }); + assert!(dispatchable_lanes(&todos).is_empty()); +} + +#[test] +fn completed_lane_is_not_dispatched() { + let mut done = item("a", 1, "api", Some("worker")); + done.status = "completed".into(); + assert!(dispatchable_lanes(&[done]).is_empty()); +} + +#[test] +fn normalize_makes_owner_lane_wide() { + let mut todos = vec![ + item("a", 1, "api", Some("worker")), + item("b", 1, "api", None), + item("c", 1, "docs", Some("self")), + ]; + crate::todo::stages::normalize_lane_owners(&mut todos); + assert_eq!(todos[1].lane_owner.as_deref(), Some("worker")); + assert_eq!(todos[2].lane_owner, None); +} diff --git a/crates/jcode-app-core/src/server/swarm.rs b/crates/jcode-app-core/src/server/swarm.rs index 9fbd9b0904a..1239b7019a7 100644 --- a/crates/jcode-app-core/src/server/swarm.rs +++ b/crates/jcode-app-core/src/server/swarm.rs @@ -399,7 +399,7 @@ impl DeadMemberSalvage { )); } parts.push( - "Queued tasks will be picked up by assign_next/run_plan; check plan_status for details." + "Queued worker lanes start automatically as dependencies and capacity allow; check plan_status for details." .to_string(), ); parts.join(" ") diff --git a/crates/jcode-app-core/src/server/swarm_todo_link.rs b/crates/jcode-app-core/src/server/swarm_todo_link.rs index efac7f4fc06..5b9abd710df 100644 --- a/crates/jcode-app-core/src/server/swarm_todo_link.rs +++ b/crates/jcode-app-core/src/server/swarm_todo_link.rs @@ -414,6 +414,25 @@ pub(crate) async fn resync_plan_after_todo_save( return None; } let mut plans = swarm_plans.write().await; + // Worker-owned lanes are dispatched without a launch call, so the first + // save that has one seeds the lane plan that status views read. + let wants_plan = todos.iter().any(crate::todo::stages::lane_requests_worker); + if wants_plan + && plans + .get(&swarm_id) + .is_none_or(|plan| plan.items.is_empty()) + { + let items = plan_items_from_todos(todos); + if !items.is_empty() { + let plan = plans + .entry(swarm_id.clone()) + .or_insert_with(super::VersionedPlan::new); + plan.replace_items(items); + plan.participants.insert(session_id.to_string()); + plan.version += 1; + return Some(swarm_id); + } + } let plan = plans.get_mut(&swarm_id)?; let items = resync_lane_plan(todos, &plan.items)?; plan.replace_items(items); diff --git a/crates/jcode-app-core/src/tool/communicate.rs b/crates/jcode-app-core/src/tool/communicate.rs index 7e7698b021d..87c7f6d753c 100644 --- a/crates/jcode-app-core/src/tool/communicate.rs +++ b/crates/jcode-app-core/src/tool/communicate.rs @@ -1,17 +1,16 @@ #![cfg_attr(test, allow(clippy::await_holding_lock))] use super::{Tool, ToolContext, ToolOutput}; -use crate::background::TaskResult; use crate::plan::PlanItem; use crate::protocol::{ AgentInfo, AgentStatusSnapshot, AwaitedMemberStatus, CommDeliveryMode, ContextEntry, HistoryMessage, PlanGraphStatus, Request, ServerEvent, SwarmChannelInfo, ToolCallSummary, comm_cleanup_candidate_session_ids, default_comm_await_target_statuses, - default_comm_cleanup_target_statuses, default_comm_run_await_statuses, - format_comm_awaited_members_with_reports, format_comm_channels, format_comm_context_entries, - format_comm_context_history, format_comm_members, format_comm_plan_followup, - format_comm_plan_status, format_comm_status_snapshot, format_comm_tool_summary, - latest_assistant_comm_report, resolve_optional_comm_target_session, + default_comm_cleanup_target_statuses, format_comm_awaited_members_with_reports, + format_comm_channels, format_comm_context_entries, format_comm_context_history, + format_comm_members, format_comm_plan_followup, format_comm_plan_status, + format_comm_status_snapshot, format_comm_tool_summary, latest_assistant_comm_report, + resolve_optional_comm_target_session, }; use anyhow::Result; use async_trait::async_trait; @@ -22,10 +21,6 @@ use std::collections::HashMap; const REQUEST_ID: u64 = 1; -/// Default number of workers `run_plan` keeps active at once when the caller -/// does not pass `concurrency_limit`. -const DEFAULT_RUN_PLAN_CONCURRENCY: usize = 4; - mod transport; use transport::{send_request, send_request_with_timeout}; @@ -76,10 +71,6 @@ fn default_cleanup_target_statuses() -> Vec { default_comm_cleanup_target_statuses() } -fn default_run_await_statuses() -> Vec { - default_comm_run_await_statuses() -} - fn cleanup_candidate_session_ids( owner_session_id: &str, members: &[AgentInfo], @@ -141,40 +132,10 @@ fn coordination_in_flight_count( ) } -/// Sessions `run_plan` should await as genuinely in-flight on *this* plan. -/// -/// A member counts only when it is both in-flight (`queued`/`running`) **and** a -/// drivable worker for this run: headless, or owned by the coordinator -/// (`report_back_to_session_id == coordinator`). This deliberately excludes -/// independent, client-attached human sessions that merely share the swarm and -/// happen to sit in a `queued` status. Awaiting those would hang `run_plan` -/// forever even though every plan task is already terminal (they are never auto -/// driven), which is exactly the stall this scoping prevents. -/// -/// Pure over an already-fetched member list so the coordination loop can reuse -/// one `CommList` snapshot for both in-flight scoping and failure-wave -/// classification. -fn in_flight_swarm_session_ids(members: &[AgentInfo], coordinator_session_id: &str) -> Vec { - members - .iter() - .filter(|member| member.session_id != coordinator_session_id) - .filter(|member| swarm_member_is_in_flight(member)) - .filter(|member| swarm_member_is_drivable_worker(member, coordinator_session_id)) - .map(|member| member.session_id.clone()) - .collect() -} - -/// Fetch-and-filter convenience over [`in_flight_swarm_session_ids`] for call -/// sites that do not otherwise need the member snapshot. -async fn fetch_in_flight_swarm_sessions(session_id: &str) -> Result> { - let members = fetch_swarm_members(session_id).await?; - Ok(in_flight_swarm_session_ids(&members, session_id)) -} - -/// Whether `member` is a worker `run_plan` can rely on to autonomously execute an +/// Whether `member` is a worker the coordinator can rely on to autonomously execute an /// assignment (and therefore one it is safe to await): a spawned headless worker, /// or one owned by the coordinator that issued the run. Foreign client-attached -/// sessions are not drivable and must not gate `run_plan` completion. +/// sessions are not drivable and must not count toward its in-flight workers. fn swarm_member_is_drivable_worker(member: &AgentInfo, coordinator_session_id: &str) -> bool { member.is_headless.unwrap_or(false) || member.report_back_to_session_id.as_deref() == Some(coordinator_session_id) @@ -262,1154 +223,6 @@ async fn stop_swarm_sessions( WorkerCleanupOutcome { stopped, failed } } -/// Free swarm member capacity mid-run by stopping finished workers owned by -/// this coordinator. `run_plan` spawns a fresh worker per node by default and -/// normally cleans up only at the end of the run, so on large plans membership -/// grows monotonically toward the swarm member cap and fresh spawns start -/// getting refused. `exclude` protects workers assigned earlier in this loop -/// whose queued status may not have propagated yet. Returns how many workers -/// were stopped. -/// -/// Tradeoff: a stopped `ready` worker may have been a composite planner whose -/// synthesis node would otherwise be routed back to it (planner affinity). -/// Assignment falls back to a fresh or other eligible worker in that case, -/// which is an acceptable degradation when the alternative is aborting the run -/// at the member cap. -async fn cleanup_finished_workers_for_capacity( - ctx: &ToolContext, - exclude: &[String], - reporter: &RunPlanReporter, -) -> usize { - let Ok(members) = fetch_swarm_members(&ctx.session_id).await else { - return 0; - }; - let candidates: Vec = cleanup_candidate_session_ids( - &ctx.session_id, - &members, - &default_cleanup_target_statuses(), - &[], - false, - ) - .into_iter() - .filter(|session_id| !exclude.iter().any(|assigned| assigned == session_id)) - .collect(); - if candidates.is_empty() { - return 0; - } - let outcome = stop_swarm_sessions(ctx, candidates, false).await; - reporter - .log(&format!("member-cap recovery: {}", outcome.describe())) - .await; - outcome.stopped.len() -} - -/// How often the background progress card is refreshed from live plan state -/// while the driver is blocked awaiting workers. -const RUN_PLAN_PROGRESS_REFRESH_SECS: u64 = 15; - -/// Whether the driver should abandon the current member-await and start a new -/// coordination loop because the plan's ready frontier grew while it was -/// blocked. Pure for unit testing. -/// -/// `ready_baseline` is the set of ready item ids observed at the top of the -/// loop that started this await. Any *new* ready id means work the driver has -/// never had a chance to dispatch: a failed node re-queued via `swarm retry`, -/// a node unblocked by an externally-driven completion, or a gate-injected -/// gap. Comparing against the baseline (instead of `!ready.is_empty()`) is -/// what prevents wake storms: items that were already ready when the await -/// began (e.g. just-assigned tasks still momentarily `queued`, or ready nodes -/// that could not be assigned to any drivable worker) do not re-trigger, so a -/// permanently-stuck ready node wakes the driver at most once per await. -fn await_should_wake_for_new_ready( - ready_baseline: &std::collections::HashSet, - summary: &PlanGraphStatus, -) -> bool { - summary - .ready_ids - .iter() - .any(|id| !ready_baseline.contains(id)) -} - -async fn await_swarm_progress( - ctx: &ToolContext, - session_ids: Vec, - timeout_minutes: u64, - reporter: &RunPlanReporter, - assignment_count: usize, - ready_baseline: &std::collections::HashSet, -) -> Result<()> { - let request = Request::CommAwaitMembers { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - target_status: default_run_await_statuses(), - session_ids, - mode: Some("any".to_string()), - timeout_secs: Some(timeout_minutes.max(1) * 60), - // run_plan needs the result inline to drive its coordination loop, so it - // explicitly opts out of the background-by-default behavior. - background: false, - notify: false, - wake: false, - }; - let socket_timeout = std::time::Duration::from_secs(timeout_minutes.max(1) * 60 + 30); - let await_members = send_request_with_timeout(request, Some(socket_timeout)); - tokio::pin!(await_members); - - // While blocked on the await (potentially many minutes), periodically - // re-read live plan + member state and push it to the progress card. - // Without this, worker completions and externally-assigned work (manual - // `assign_task`) only surface at the driver's own wave boundaries, so the - // card goes stale for the whole await. Refresh failures are ignored: the - // card is best-effort and the await result is what drives the loop. - let refresh_period = std::time::Duration::from_secs(RUN_PLAN_PROGRESS_REFRESH_SECS); - let mut refresh = - tokio::time::interval_at(tokio::time::Instant::now() + refresh_period, refresh_period); - refresh.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - - // Detached plan drivers must not label the foreground agent as waiting. - let _wait_guard = (!reporter.is_background()) - .then(|| crate::wait_registry::register(&ctx.session_id, "Waiting for swarm members")); - let response = loop { - tokio::select! { - result = &mut await_members => break result, - _ = refresh.tick() => { - let summary = match fetch_plan_status(&ctx.session_id).await { - Ok(summary) => summary, - Err(_) => continue, - }; - if reporter.is_background() { - let live_active = fetch_in_flight_swarm_sessions(&ctx.session_id) - .await - .map(|sessions| sessions.len()) - .unwrap_or(0); - let (completed, total, message) = - run_plan_progress_snapshot(&summary, live_active, assignment_count); - reporter.progress(completed, total, message).await; - } - // Ready frontier grew while blocked (a `swarm retry` re-queued - // failed nodes, an external completion unblocked work, a gate - // injected gaps): return to the coordination loop so the new - // work is dispatched under the normal budget instead of - // waiting out the current wave. The abandoned await is a - // plain request future; dropping it cancels only our wait, - // not the workers. - if await_should_wake_for_new_ready(ready_baseline, &summary) { - reporter - .log("ready frontier grew during await (retry/requeue or external unblock); re-entering dispatch loop") - .await; - return Ok(()); - } - } - } - }; - - match response { - Ok(ServerEvent::CommAwaitMembersResponse { - completed, summary, .. - }) => { - if completed { - Ok(()) - } else { - Err(anyhow::anyhow!( - "Timed out waiting for swarm progress: {}", - summary - )) - } - } - Ok(response) => ensure_success(&response), - Err(e) => Err(anyhow::anyhow!( - "Failed while awaiting swarm progress: {}", - e - )), - } -} - -/// Decide how many swarm workers `run_plan` keeps active at once: an explicit -/// limit wins (clamped to >= 1), otherwise the small default. -fn resolve_run_plan_concurrency(requested: Option) -> usize { - requested.map_or(DEFAULT_RUN_PLAN_CONCURRENCY, |explicit| explicit.max(1)) -} - -/// Running tally of how well a `run_plan` drive used its concurrency budget. -/// -/// Tracks peak parallelism and how often open slots sat idle with no ready -/// work, so the terminal report shows whether the budget was used. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -struct RunPlanUtilization { - /// Highest number of simultaneously in-flight tasks observed. - peak_in_flight: usize, - /// Coordination loops observed. - loops: usize, - /// Loops where open worker slots existed but the plan had nothing ready to - /// dispatch into them (budget idle due to graph narrowness, not the cap). - starved_loops: usize, -} - -impl RunPlanUtilization { - /// Record one coordination loop. `open_slots` is `None` when the budget is - /// unbounded (`concurrency_limit == usize::MAX`): an infinite budget has no - /// meaningful starvation denominator, so only peak parallelism is tracked. - fn record_loop(&mut self, in_flight: usize, open_slots: Option, dispatched: usize) { - self.loops += 1; - self.peak_in_flight = self.peak_in_flight.max(in_flight + dispatched); - if let Some(open_slots) = open_slots - && open_slots > 0 - && dispatched < open_slots - { - self.starved_loops += 1; - } - } - - /// Render the utilization line for the terminal report. - fn report(&self, concurrency_limit: usize) -> String { - let limit_label = if concurrency_limit == usize::MAX { - "unbounded".to_string() - } else { - concurrency_limit.to_string() - }; - format!( - "Budget utilization: peak {} of {} concurrent worker slot(s); {} of {} loop(s) had idle capacity with nothing ready.", - self.peak_in_flight, limit_label, self.starved_loops, self.loops - ) - } -} - -/// Extract the background task id from its output file path -/// (`.output`), mirroring the bash tool's convention so progress -/// updates can be routed back to the background task manager. -fn task_id_from_output_path(path: &std::path::Path) -> Option<&str> { - path.file_name()?.to_str()?.strip_suffix(".output") -} - -/// Progress/log sink for a `run_plan` execution. -/// -/// In background mode this appends human-readable lines to the background -/// task's output file and pushes determinate progress (terminal/total plan -/// nodes) into the background task manager, so the UI renders a live swarm -/// progress card and `bg status` stays meaningful. In inline (blocking) mode -/// every method is a no-op. -struct RunPlanReporter { - task_id: Option, - output_path: Option, -} - -impl RunPlanReporter { - fn inline() -> Self { - Self { - task_id: None, - output_path: None, - } - } - - fn background(output_path: &std::path::Path) -> Self { - Self { - task_id: task_id_from_output_path(output_path).map(str::to_string), - output_path: Some(output_path.to_path_buf()), - } - } - - /// Whether this reporter feeds a live background progress card (inline - /// reporters are no-ops, so refresh polling would be wasted requests). - fn is_background(&self) -> bool { - self.task_id.is_some() - } - - async fn log(&self, line: &str) { - let Some(path) = &self.output_path else { - return; - }; - use tokio::io::AsyncWriteExt; - if let Ok(mut file) = tokio::fs::OpenOptions::new() - .create(true) - .append(true) - .open(path) - .await - { - let _ = file.write_all(format!("{}\n", line).as_bytes()).await; - } - } - - async fn progress(&self, terminal: usize, total: usize, message: String) { - let Some(task_id) = &self.task_id else { - return; - }; - let progress = crate::bus::BackgroundTaskProgress { - kind: crate::bus::BackgroundTaskProgressKind::Determinate, - percent: None, - message: Some(message), - current: Some(terminal as u64), - total: Some(total as u64), - unit: Some("nodes".to_string()), - eta_seconds: None, - updated_at: chrono::Utc::now().to_rfc3339(), - source: crate::bus::BackgroundTaskProgressSource::Reported, - } - .normalize(); - let _ = crate::background::global() - .update_progress(task_id, progress) - .await; - } - - /// Record an explicit checkpoint (a JCODE_CHECKPOINT-style milestone) on - /// the background task, so pause/alert moments surface as checkpoint events - /// in the UI instead of only trailing the output log. No-op inline. - async fn checkpoint(&self, message: &str) { - self.log(message).await; - let Some(task_id) = &self.task_id else { - return; - }; - let progress = crate::bus::BackgroundTaskProgress { - kind: crate::bus::BackgroundTaskProgressKind::Indeterminate, - percent: None, - message: Some(message.to_string()), - current: None, - total: None, - unit: None, - eta_seconds: None, - updated_at: chrono::Utc::now().to_rfc3339(), - source: crate::bus::BackgroundTaskProgressSource::Reported, - } - .normalize(); - let _ = crate::background::global() - .update_checkpoint(task_id, progress) - .await; - } - - /// Rewrite the output file so `summary` leads and the progressive log - /// trails it. Background completion previews take the first ~500 chars of - /// the output file, so the terminal summary must come first for the - /// agent's wake notification to be useful. - async fn finalize(&self, summary: &str) { - let Some(path) = &self.output_path else { - return; - }; - let log = tokio::fs::read_to_string(path).await.unwrap_or_default(); - let content = if log.trim().is_empty() { - format!("{}\n", summary) - } else { - format!("{}\n\n--- run log ---\n{}", summary, log) - }; - let _ = tokio::fs::write(path, content).await; - } -} - -/// Per-process registry of sessions with a `run_plan` driver claimed or -/// running. The duplicate-driver guard does its check-and-insert under this -/// one lock, so two `run_plan` calls racing in the same batch cannot both -/// pass. Deliberately per-process: a stale `Running` status file left on disk -/// by a previous (reloaded/crashed) server process must never block -/// restarting the driver. -fn run_plan_driver_claims() -> &'static std::sync::Mutex> { - static CLAIMS: std::sync::OnceLock>> = - std::sync::OnceLock::new(); - CLAIMS.get_or_init(|| std::sync::Mutex::new(HashMap::new())) -} - -enum RunPlanDriverClaim { - /// Claimed, background task not spawned yet. - Starting, - /// Driver spawned as this background task. - Running(String), -} - -enum RunPlanDriverClaimResult { - Claimed(RunPlanClaimGuard), - /// A driver already holds the claim. Carries its task id when known - /// (None while the winner is still between claim and spawn). - AlreadyRunning(Option), -} - -/// RAII holder for a `Starting` claim. Dropping it without -/// [`RunPlanClaimGuard::record_task`] releases the claim, so a cancelled or -/// failed startup path cannot permanently block `run_plan` for the session. -struct RunPlanClaimGuard { - session_id: String, - defused: bool, -} - -impl RunPlanClaimGuard { - /// Upgrade the claim to `Running(task_id)`. From here staleness is - /// resolved via `BackgroundTaskManager::is_live_task`: once the driver - /// task finishes (and is pruned from the live map), the next claim - /// replaces this entry. - fn record_task(mut self, task_id: &str) { - let mut claims = run_plan_driver_claims() - .lock() - .unwrap_or_else(|poisoned| poisoned.into_inner()); - claims.insert( - self.session_id.clone(), - RunPlanDriverClaim::Running(task_id.to_string()), - ); - self.defused = true; - } -} - -impl Drop for RunPlanClaimGuard { - fn drop(&mut self) { - if self.defused { - return; - } - let mut claims = run_plan_driver_claims() - .lock() - .unwrap_or_else(|poisoned| poisoned.into_inner()); - // Only release a claim this guard still owns. - if matches!( - claims.get(&self.session_id), - Some(RunPlanDriverClaim::Starting) - ) { - claims.remove(&self.session_id); - } - } -} - -/// Atomically claim the `run_plan` driver slot for `session_id`. -/// -/// Check-and-insert happens under one lock. An existing `Running` claim only -/// blocks while its background task is still live in this process; a claim -/// left by a finished (pruned) or pre-reload driver is replaced. -fn try_claim_run_plan_driver( - manager: &crate::background::BackgroundTaskManager, - session_id: &str, -) -> RunPlanDriverClaimResult { - let mut claims = run_plan_driver_claims() - .lock() - .unwrap_or_else(|poisoned| poisoned.into_inner()); - match claims.get(session_id) { - Some(RunPlanDriverClaim::Starting) => { - return RunPlanDriverClaimResult::AlreadyRunning(None); - } - Some(RunPlanDriverClaim::Running(task_id)) => { - if manager.is_live_task(task_id) { - return RunPlanDriverClaimResult::AlreadyRunning(Some(task_id.clone())); - } - // Stale claim: the driver task already finished or belonged to a - // previous process image. Fall through and take over. - } - None => {} - } - claims.insert(session_id.to_string(), RunPlanDriverClaim::Starting); - RunPlanDriverClaimResult::Claimed(RunPlanClaimGuard { - session_id: session_id.to_string(), - defused: false, - }) -} - -/// Make the swarm plan mirror the caller's todo list: one item per lane. -/// -/// `launch` calls this every time, so the plan can never drift from the -/// todos; the server keeps the status and owner of lanes already running. -/// The legacy `run_plan` only seeds an empty plan, so a hand-written -/// `propose_plan` still wins there. Returns quietly with no open todos. -async fn sync_plan_from_todos(ctx: &ToolContext, only_if_empty: bool) -> Result<()> { - if only_if_empty - && fetch_plan_status(&ctx.session_id) - .await - .is_ok_and(|summary| summary.item_count > 0) - { - return Ok(()); - } - let todos = crate::todo::load_todos(&ctx.session_id).unwrap_or_default(); - let items = crate::server::swarm_todo_link::plan_items_from_todos(&todos); - if items.is_empty() { - return Ok(()); - } - let request = Request::CommProposePlan { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - items, - }; - let response = send_request(request) - .await - .map_err(|e| anyhow::anyhow!("Failed to seed swarm plan from todos: {}", e))?; - ensure_success(&response) -} - -/// Drive `run_plan` as a managed background task and return immediately. -/// -/// The coordinating agent stays responsive: the plan loop runs inside the -/// shared `BackgroundTaskManager` (task id, progress card, `bg` tool -/// integration), and completion is delivered through the standard notify/wake -/// path like any other background task. -async fn run_swarm_plan_in_background( - ctx: &ToolContext, - params: CommunicateInput, -) -> Result { - // Validate the plan inline so an empty/broken plan errors immediately - // instead of as a delayed background failure. - let initial_summary = fetch_plan_status(&ctx.session_id).await?; - if initial_summary.item_count == 0 { - return Ok(ToolOutput::new("No swarm plan items to run.")); - } - - // Refuse to start a second driver for the same session: two concurrent - // run_plan loops would race on assignments and double-spawn workers. The - // claim is check-and-insert under one lock, so two run_plan calls in the - // same batch cannot both pass. Only drivers live in this process count; a - // stale "running" status file left by a server reload must not block - // restarting the driver (the claim map is per-process and dead task ids - // fail the is_live_task check). - let manager = crate::background::global(); - let claim = match try_claim_run_plan_driver(manager, &ctx.session_id) { - RunPlanDriverClaimResult::Claimed(claim) => claim, - RunPlanDriverClaimResult::AlreadyRunning(existing) => { - return Ok(ToolOutput::new(match existing { - Some(task_id) => format!( - "A swarm run_plan driver is already running for this session (task {}). \ - Check it with `bg action=\"status\" task_id=\"{}\"` or `swarm plan_status` instead of starting another.", - task_id, task_id - ), - None => "A swarm run_plan driver is already starting for this session. \ - Check it with `swarm plan_status` instead of starting another." - .to_string(), - })); - } - }; - - let notify = params.notify.unwrap_or(true); - let wake = params.wake.unwrap_or(true); - // Keep the display name free of the "·" separator used by the background - // notification markdown header, or downstream parsing mis-splits the label. - let display_name = format!("run_plan ({} tasks)", initial_summary.item_count); - - let bg_ctx = ctx.clone(); - let info = crate::background::global() - .spawn_with_notify( - "swarm", - Some(display_name.clone()), - &ctx.session_id, - notify, - wake, - move |output_path| async move { - let reporter = RunPlanReporter::background(&output_path); - match run_swarm_plan_to_terminal(&bg_ctx, ¶ms, &reporter).await { - Ok(output) => { - reporter.finalize(&output.output).await; - Ok(TaskResult::completed(Some(0))) - } - Err(error) => { - let message = format!("run_plan failed: {}", error); - reporter.finalize(&message).await; - Ok(TaskResult::failed(None, message)) - } - } - }, - ) - .await; - claim.record_task(&info.task_id); - - let delivery_note = if wake { - "You'll be woken with the result when the plan reaches a terminal state." - } else if notify { - "A notification will appear when the plan reaches a terminal state." - } else { - "Notifications disabled. Use the `bg` tool to check status." - }; - let output = format!( - "🐝 Swarm plan running in background.\n\n\ - Task ID: {}\n\ - Plan: {} task(s)\n\ - Output file: {}\n\n\ - {}\n\ - Check progress: use the `bg` tool with action=\"status\" and task_id=\"{}\", or `swarm plan_status`.\n\ - Note: a server reload stops this driver (workers keep running); rerun `swarm run_plan` to resume driving the same plan.", - info.task_id, - initial_summary.item_count, - info.output_file.display(), - delivery_note, - info.task_id, - ); - - Ok(ToolOutput::new(output) - .with_title(format!("Swarm run_plan in background: {}", info.task_id)) - .with_metadata(json!({ - "background": true, - "swarm": true, - "task_id": info.task_id, - "display_name": display_name, - "output_file": info.output_file.to_string_lossy(), - "status_file": info.status_file.to_string_lossy(), - }))) -} - -/// Hint appended to every `run_plan` driver failure: the driver exits without -/// the end-of-run cleanup, so spawned workers keep running even when -/// `retain_agents=false`, and the caller must know how to stop or resume them. -const RUN_PLAN_WORKER_RETENTION_HINT: &str = "\nSpawned workers were retained; run `swarm cleanup` to stop them, rerun `swarm run_plan` to resume driving the same plan, or `swarm plan_status` to inspect."; - -/// Append the worker-retention hint to a driver failure message, idempotently -/// so wrappers that re-report an already-hinted error do not duplicate it. -fn with_worker_retention_hint(message: String) -> String { - if message.contains("swarm cleanup") { - message - } else { - format!("{message}{RUN_PLAN_WORKER_RETENTION_HINT}") - } -} - -/// How the `run_plan` assignment loop should react to a `CommAssignNext` error. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum AssignErrorAction { - /// No more runnable work or no eligible workers: stop assigning this loop - /// and continue with in-flight work. - BreakGracefully, - /// The swarm hit its total member cap so fresh spawns are refused: free - /// finished owned workers and/or fall back to reusing ready workers instead - /// of aborting the whole run. - RecoverCapacity, - /// Anything else is a real failure. - Fail, -} - -fn classify_assign_error(message: &str) -> AssignErrorAction { - if message.contains("No runnable unassigned tasks") - || message.contains("No ready or completed swarm agents") - { - AssignErrorAction::BreakGracefully - } else if message.contains("Swarm member limit reached") { - AssignErrorAction::RecoverCapacity - } else { - AssignErrorAction::Fail - } -} - -/// Next step for a slot whose assignment was refused by the member cap. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum CapRecoveryStep { - /// Cleanup freed capacity: retry the slot keeping the fresh-spawn preference. - RetryFresh, - /// Nothing was freed: retry the slot in reuse-only mode (no spawning). - RetryReuse, - /// Recovery already ran and the cap still refuses this slot: stop assigning - /// this loop and continue with in-flight work. - GiveUp, -} - -/// Pure recovery policy for a member-cap refusal, keyed on how many times this -/// slot already hit the cap (`cap_hits`) and how many workers the incremental -/// cleanup freed. Kept side-effect free so the fallback contract is unit -/// testable without a live swarm. -fn cap_recovery_step(cap_hits: usize, freed: usize) -> CapRecoveryStep { - if cap_hits > 1 { - CapRecoveryStep::GiveUp - } else if freed > 0 { - CapRecoveryStep::RetryFresh - } else { - CapRecoveryStep::RetryReuse - } -} - -/// Count each plan node at most once as terminal: completed, failed, blocked, -/// and cycle sets overlap in places (and failed nodes appear in none of the -/// legacy three), so a plain sum both over- and under-counts. -fn plan_terminal_node_count(summary: &PlanGraphStatus) -> usize { - summary - .completed_ids - .iter() - .chain(summary.failed_ids.iter()) - .chain(summary.blocked_ids.iter()) - .chain(summary.cycle_ids.iter()) - .collect::>() - .len() -} - -/// Numbers for the `run_plan` background progress card. Pure for unit testing. -/// -/// The percent-driving pair is `(completed, total)`: only *completed* nodes -/// count toward 100%, so a run where most nodes failed reads as mostly -/// unfinished instead of "98% complete" (failed/blocked counts are surfaced in -/// the message instead). `live_active` is the count of in-flight worker -/// sessions observed from member state; the card shows whichever of plan -/// execution state (`active_ids`) or live member state is larger, so nodes -/// assigned outside this driver (e.g. manual `assign_task`) still show as -/// active. -fn run_plan_progress_snapshot( - summary: &PlanGraphStatus, - live_active: usize, - assignment_count: usize, -) -> (usize, usize, String) { - let completed = summary.completed_ids.len(); - let active = summary.active_ids.len().max(live_active); - let message = format!( - "completed {} · failed {} · blocked {} · active {} · assignments {}", - completed, - summary.failed_ids.len(), - summary.blocked_ids.len(), - active, - assignment_count - ); - (completed, summary.item_count, message) -} - -/// Terminal-state summary line for `run_plan`, including failed nodes so a run -/// with failures never reads like a clean finish. Pure for unit testing. -fn format_run_plan_terminal_summary( - loop_count: usize, - summary: &PlanGraphStatus, - assignment_count: usize, -) -> String { - let mut output = format!( - "Swarm plan reached terminal/blocked state after {} loop(s). completed={} failed={} blocked={} cycles={} active={} assignments={}", - loop_count, - summary.completed_ids.len(), - summary.failed_ids.len(), - summary.blocked_ids.len(), - summary.cycle_ids.len(), - summary.active_ids.len(), - assignment_count - ); - if !summary.failed_ids.is_empty() { - output.push_str(&format!( - "\nFailed nodes: {}. This run did NOT finish cleanly; inspect them with `swarm plan_status` and retry or salvage before trusting the result.", - summary.failed_ids.join(", ") - )); - // Recorded failure reasons make the summary self-explanatory: a wave - // of "task failed: ... 401 Unauthorized" lines names the root cause - // without another plan_status round-trip. - for id in &summary.failed_ids { - if let Some(reason) = summary.failed_reasons.get(id) { - output.push_str(&format!("\n {}: {}", id, reason)); - } - } - } - output -} - -/// Minimum number of credential-failed workers that count as a wave rather -/// than an isolated bad worker. -const CREDENTIAL_FAILURE_WAVE_MIN_WORKERS: usize = 2; - -/// How recent a worker's credential failure must be (via `status_age_secs`) to -/// count toward a wave. Old failed workers from a previous, already-diagnosed -/// wave must not re-trip the breaker after the user fixes auth and retries. -const CREDENTIAL_FAILURE_WAVE_WINDOW_SECS: u64 = 60; - -/// A wave of worker failures that share one credential-shaped root cause. -/// -/// When dispatched workers die within seconds of assignment with 401 / -/// `invalid_grant` / `authentication_error`-style errors, the credential is -/// broken for every worker on that route: assigning more nodes only fails more -/// of the plan. Detecting the wave lets `run_plan` pause dispatching and -/// surface the one real fix instead of silently burning the graph. -#[derive(Debug, Clone, PartialEq, Eq)] -struct CredentialFailureWave { - /// Failed worker sessions in the wave. - session_ids: Vec, - /// Representative failure detail (first observed). - sample_detail: String, - /// Provider named by the failing workers, when known (e.g. "anthropic"). - provider: Option, -} - -/// Detect a credential-failure wave in a swarm member snapshot. -/// -/// A wave exists when, with **zero completed plan nodes**, at least -/// [`CREDENTIAL_FAILURE_WAVE_MIN_WORKERS`] drivable workers sit in `failed` -/// status whose detail classifies as a credential failure (via the shared -/// [`crate::provider::error_looks_like_credential_failure`] classifier) and -/// whose failure is recent (`status_age_secs <= window_secs`). Pure over its -/// inputs so the breaker contract is unit-testable without a live swarm. -fn detect_credential_failure_wave( - members: &[AgentInfo], - coordinator_session_id: &str, - completed_node_count: usize, - window_secs: u64, -) -> Option { - if completed_node_count > 0 { - return None; - } - let mut session_ids = Vec::new(); - let mut sample_detail: Option = None; - let mut provider: Option = None; - for member in members { - if member.session_id == coordinator_session_id { - continue; - } - if !swarm_member_is_drivable_worker(member, coordinator_session_id) { - continue; - } - if member.status.as_deref() != Some("failed") { - continue; - } - let Some(detail) = member.detail.as_deref() else { - continue; - }; - if !crate::provider::error_looks_like_credential_failure(detail) { - continue; - } - // Require a known, recent failure age: stale failed workers (or ones - // whose age did not propagate) must not re-trip the breaker. - if !matches!(member.status_age_secs, Some(age) if age <= window_secs) { - continue; - } - session_ids.push(member.session_id.clone()); - if sample_detail.is_none() { - sample_detail = Some(detail.to_string()); - } - if provider.is_none() { - provider = member.provider_name.clone(); - } - } - if session_ids.len() < CREDENTIAL_FAILURE_WAVE_MIN_WORKERS { - return None; - } - Some(CredentialFailureWave { - session_ids, - sample_detail: sample_detail.unwrap_or_default(), - provider, - }) -} - -/// The `jcode login` invocation most likely to fix a credential wave for -/// `provider`, mapping provider names to their login provider keys. -fn credential_login_fix_hint(provider: Option<&str>) -> String { - let lowered = provider.map(str::to_ascii_lowercase); - let target = match lowered.as_deref() { - Some("anthropic" | "claude") => "claude", - Some("openai" | "codex") => "openai", - Some("google" | "gemini") => "gemini", - Some(other) if !other.trim().is_empty() => other, - _ => "", - }; - format!("`jcode login --provider {target}`") -} - -/// Actionable pause message for a credential-failure wave: names the failed -/// workers, the credential-shaped root cause, and the fix. Pure for unit -/// testing; used both as the run error and as the swarm broadcast body. -fn format_credential_failure_wave_error(wave: &CredentialFailureWave, window_secs: u64) -> String { - format!( - "run_plan paused dispatching: {count} worker(s) failed within {window_secs}s with \ - credential/auth failures and no plan node has completed (e.g. {first}: \"{sample}\"). \ - A broken credential (expired OAuth session, revoked refresh token, or invalid API key) \ - fails every worker on that route, so assigning more nodes would only fail more of the \ - plan. Fix auth first: run {login_hint} (or pin a working API-key route), then requeue \ - the failed nodes (`swarm retry`) and run `swarm run_plan` again.", - count = wave.session_ids.len(), - first = wave - .session_ids - .first() - .map(String::as_str) - .unwrap_or("worker"), - sample = wave.sample_detail, - login_hint = credential_login_fix_hint(wave.provider.as_deref()), - ) -} - -/// Best-effort broadcast of a plan-level alert to the whole swarm, so live -/// members and attached UIs see why dispatch stopped. -async fn broadcast_plan_alert(ctx: &ToolContext, message: &str) -> Result<()> { - let request = Request::CommMessage { - id: REQUEST_ID, - from_session: ctx.session_id.clone(), - message: message.to_string(), - to_session: None, - channel: None, - wake: None, - delivery: None, - tldr: Some("run_plan paused: credential failure wave; fix auth then retry".to_string()), - to_swarm: None, - require_target: false, - }; - match send_request(request).await { - Ok(response) => ensure_success(&response), - Err(e) => Err(anyhow::anyhow!("Failed to broadcast plan alert: {}", e)), - } -} - -async fn run_swarm_plan_to_terminal( - ctx: &ToolContext, - params: &CommunicateInput, - reporter: &RunPlanReporter, -) -> Result { - // Every driver-failure exit (assignment failure, await timeout, stall, - // max-loops, even a mid-run plan-status fetch error) leaves spawned workers - // running because the end-of-run cleanup never executes, regardless of - // retain_agents. Append the retention hint uniformly here so no failure - // path can forget it. - match run_swarm_plan_loop(ctx, params, reporter).await { - Ok(output) => Ok(output), - Err(error) => Err(anyhow::anyhow!(with_worker_retention_hint( - error.to_string() - ))), - } -} - -async fn run_swarm_plan_loop( - ctx: &ToolContext, - params: &CommunicateInput, - reporter: &RunPlanReporter, -) -> Result { - // Fail fast when the plan cannot be read before spawning any workers. - fetch_plan_status(&ctx.session_id).await?; - let concurrency_limit = resolve_run_plan_concurrency(params.concurrency_limit); - let timeout_minutes = params.timeout_minutes.unwrap_or(60).max(1); - let retain_agents = params.retain_agents.unwrap_or(false); - let spawn_if_needed = params.spawn_if_needed.or(Some(true)); - // Default to a fresh worker per task. Reusing a worker that already completed - // a *different* task carries that conversation into the next assignment, and - // the model often just re-reports its prior result instead of doing the new - // work. Callers can opt into reuse with `prefer_spawn=false`. - let prefer_spawn = params.prefer_spawn.or(Some(true)); - let mut assignment_count = 0usize; - let mut loop_count = 0usize; - let max_loops = 200usize; - let mut utilization = RunPlanUtilization::default(); - // Consecutive loops where an active task exists but no drivable worker is - // awaitable. This is normally a brief transition (a composite re-waking to - // synthesize, or a just-finished task whose member status has not propagated), - // so we back off and re-check a few times before declaring a real stall. - let mut transient_stall_loops = 0usize; - let max_transient_stall_loops = 5usize; - - loop { - loop_count += 1; - if loop_count > max_loops { - return Err(anyhow::anyhow!( - "run_plan exceeded {} coordination loops; leaving workers untouched for inspection", - max_loops - )); - } - - let summary = fetch_plan_status(&ctx.session_id).await?; - if summary.item_count == 0 { - return Ok(ToolOutput::new("No swarm plan items to run.")); - } - - let members = fetch_swarm_members(&ctx.session_id).await?; - let in_flight_sessions = in_flight_swarm_session_ids(&members, &ctx.session_id); - - // Credential-failure circuit breaker: when a wave of workers dies with - // 401/invalid_grant-style auth errors and nothing has completed, the - // credential is broken for every worker on that route. Pausing here - // (before the terminal check) means even a fully-burned first wave - // surfaces the root cause and the fix instead of a bare - // "failed=N" terminal summary with no explanation. - if let Some(wave) = detect_credential_failure_wave( - &members, - &ctx.session_id, - summary.completed_ids.len(), - CREDENTIAL_FAILURE_WAVE_WINDOW_SECS, - ) { - let message = - format_credential_failure_wave_error(&wave, CREDENTIAL_FAILURE_WAVE_WINDOW_SECS); - reporter.checkpoint(&message).await; - if let Err(error) = broadcast_plan_alert(ctx, &message).await { - reporter - .log(&format!( - "failed to broadcast credential-failure alert to the swarm: {error}" - )) - .await; - } - return Err(anyhow::anyhow!(message)); - } - - let terminal_count = plan_terminal_node_count(&summary); - let (progress_completed, progress_total, progress_message) = - run_plan_progress_snapshot(&summary, in_flight_sessions.len(), assignment_count); - reporter - .progress(progress_completed, progress_total, progress_message) - .await; - let no_more_runnable = summary.active_ids.is_empty() - && summary.next_ready_ids.is_empty() - && in_flight_sessions.is_empty(); - if no_more_runnable || terminal_count >= summary.item_count { - let mut output = - format_run_plan_terminal_summary(loop_count, &summary, assignment_count); - output.push_str(&format!("\n{}", utilization.report(concurrency_limit))); - if retain_agents { - output.push_str("\nRetained spawned workers because retain_agents=true."); - } else { - // Run the automatic end-of-plan cleanup with a sanitized input: - // `force`, `session_ids`, and `target_status` on the run_plan - // call are meant for explicit stop/cleanup/await actions, and - // leaking `force=true` here would force-stop every terminal - // swarm member (including user-created idle sessions) instead - // of only the workers this coordinator owns. - let cleanup_params = CommunicateInput { - force: None, - session_ids: None, - target_status: None, - ..params.clone() - }; - let cleanup = cleanup_swarm_workers(ctx, &cleanup_params).await?; - output.push_str(&format!("\n{}", cleanup)); - } - return Ok(ToolOutput::new(output)); - } - - let active_count = summary.active_ids.len().max(in_flight_sessions.len()); - let available_slots = concurrency_limit.saturating_sub(active_count); - let mut assigned_sessions = Vec::new(); - // Member-cap fallback state, reset each coordination loop. When the swarm - // hits its total member cap, fresh spawns are refused; instead of aborting - // the whole run we first free finished owned workers (incremental cleanup) - // and retry, then fall back to reuse-only assignment (no spawning), and - // only after that stop assigning and continue with in-flight work. - let mut cap_hits = 0usize; - let mut reuse_only = false; - let mut slots_remaining = available_slots; - while slots_remaining > 0 { - let request = Request::CommAssignNext { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - target_session: params.target_session.clone(), - working_dir: params.working_dir.clone(), - prefer_spawn: if reuse_only { - Some(false) - } else { - prefer_spawn - }, - spawn_if_needed: if reuse_only { - Some(false) - } else { - spawn_if_needed - }, - message: params.message.clone(), - model: params.model.clone(), - // Worker effort is not agent-controlled: the server applies - // `agents.swarm_effort` (default "low"). - effort: None, - }; - match send_request(request).await { - Ok(ServerEvent::CommAssignTaskResponse { - task_id, - target_session, - .. - }) => { - assignment_count += 1; - slots_remaining -= 1; - reporter - .log(&format!("assigned {} -> {}", task_id, target_session)) - .await; - assigned_sessions.push(target_session); - } - Ok(ServerEvent::Error { message, .. }) => { - match classify_assign_error(&message) { - AssignErrorAction::BreakGracefully => break, - AssignErrorAction::RecoverCapacity => { - cap_hits += 1; - let freed = if cap_hits == 1 { - cleanup_finished_workers_for_capacity( - ctx, - &assigned_sessions, - reporter, - ) - .await - } else { - 0 - }; - match cap_recovery_step(cap_hits, freed) { - CapRecoveryStep::RetryFresh => { - // Cleanup freed member slots; retry this slot - // with the fresh-spawn preference intact. - } - CapRecoveryStep::RetryReuse => { - reuse_only = true; - reporter - .log( - "member cap reached and no finished workers to free; \ - falling back to reusing ready workers (prefer_spawn=false)", - ) - .await; - } - CapRecoveryStep::GiveUp => { - reporter - .log( - "member cap still reached after recovery; \ - continuing with in-flight work", - ) - .await; - break; - } - } - } - AssignErrorAction::Fail => { - return Err(anyhow::anyhow!(message)); - } - } - } - Ok(response) => ensure_success(&response)?, - Err(e) => return Err(anyhow::anyhow!("Failed to assign next swarm task: {}", e)), - } - } - utilization.record_loop( - active_count, - (concurrency_limit != usize::MAX).then_some(available_slots), - assigned_sessions.len(), - ); - - let await_sessions = if assigned_sessions.is_empty() { - in_flight_sessions - } else { - assigned_sessions - }; - - if await_sessions.is_empty() { - if active_count > 0 { - // An active task exists but nothing drivable is awaitable. This is - // usually transient: a composite is re-waking to synthesize, or a - // worker just finished and its member status has not propagated yet. - // Re-check a few times with a short backoff before giving up, and - // bail early if the plan reaches a terminal state in the meantime. - transient_stall_loops += 1; - if transient_stall_loops <= max_transient_stall_loops { - tokio::time::sleep(std::time::Duration::from_millis(500)).await; - continue; - } - return Err(anyhow::anyhow!( - "run_plan found {} active task(s) but no running swarm members to await after {} re-checks; inspect plan_status and member list before retrying", - active_count, - max_transient_stall_loops - )); - } - // Nothing was assigned this loop, nothing is in flight, yet the plan is - // not terminal. This means some non-terminal task cannot be driven, e.g. - // it is already assigned to a session run_plan cannot drive (a foreign or - // stale member). Spinning here would busy-loop to the max-loop cap, so - // surface the stuck state with the offending tasks instead. - let stuck: Vec = summary - .next_ready_ids - .iter() - .chain(summary.ready_ids.iter()) - .cloned() - .collect::>() - .into_iter() - .collect(); - let detail = if stuck.is_empty() { - "no ready tasks and no in-flight workers".to_string() - } else { - format!( - "runnable task(s) {} could not be assigned to any drivable worker", - stuck.join(", ") - ) - }; - return Err(anyhow::anyhow!( - "run_plan stalled after {} loop(s): {}. This usually means a task is assigned to a session run_plan cannot drive (foreign or stale member). Reassign with an explicit target_session, or clear the stale assignment, then retry.", - loop_count, - detail - )); - } - // Baseline for requeue pickup: everything ready at the top of this - // loop either gets assigned below or is known-undispatchable this - // wave. Anything ready *beyond* this set while we block (a retried - // failure, an external unblock) should cut the await short. - let ready_baseline: std::collections::HashSet = - summary.ready_ids.iter().cloned().collect(); - await_swarm_progress( - ctx, - await_sessions, - timeout_minutes, - reporter, - assignment_count, - &ready_baseline, - ) - .await?; - // Real progress (an await completed); clear the transient-stall backoff so - // a later genuine stall starts counting fresh. - transient_stall_loops = 0; - } -} - async fn spawn_assignment_session(ctx: &ToolContext, params: &CommunicateInput) -> Result { let spawn_request = Request::CommSpawn { id: REQUEST_ID, @@ -1678,11 +491,10 @@ impl CommunicateTool { /// Swarm actions advertised to models. Real usage is dominated by messaging, /// listing and spawning; the plan/task-graph actions (propose_plan, -/// assign_task, assign_next, fill_slots, run_plan, resync_plan, ...) are -/// subsumed by `launch` and lane-owning `spawn`. Unlisted actions still +/// assign_task, assign_next, resync_plan, ...) are subsumed by worker-owned +/// todo lanes, which the server dispatches itself, and lane-owning `spawn`. Unlisted actions still /// execute for old transcripts and scripts, they are just not offered. const ADVERTISED_SWARM_ACTIONS: &[&str] = &[ - "launch", "spawn", "message", "status", @@ -1754,12 +566,8 @@ struct CommunicateInput { #[serde(default)] delivery: Option, #[serde(default)] - concurrency_limit: Option, - #[serde(default)] force: Option, #[serde(default)] - retain_agents: Option, - #[serde(default)] status: Option, #[serde(default)] validation: Option, @@ -1938,27 +746,14 @@ impl Tool for CommunicateTool { "minimum": 1, "description": "await_members; returned ID can be used in todo.waiting_on." }, - "background": { - "type": "boolean", - "description": "launch: default true; false waits for completion." - }, "notify": { "type": "boolean", "description": "await_members: default true." }, - "concurrency_limit": { - "type": "integer", - "minimum": 1, - "description": "Max live launch workers." - }, "force": { "type": "boolean", "description": "stop: allow stopping non-owned/user-created sessions; default false." }, - "retain_agents": { - "type": "boolean", - "description": "launch: keep workers after completion; default false." - }, "wake": { "type": "boolean", "description": "Wake on message/await completion; default true." @@ -2843,100 +1638,12 @@ impl Tool for CommunicateTool { } } - "fill_slots" => { - let concurrency_limit = params.concurrency_limit.ok_or_else(|| { - anyhow::anyhow!("'concurrency_limit' is required for fill_slots action") - })?; - - let summary = fetch_plan_status(&ctx.session_id).await?; - let members = fetch_swarm_members(&ctx.session_id).await?; - - let active_count = - coordination_in_flight_count(&summary, &members, &ctx.session_id); - if active_count >= concurrency_limit { - return Ok(ToolOutput::new(format!( - "Window already full: {} active/in-flight task(s) >= limit {}", - active_count, concurrency_limit - ))); - } - - let mut assignments = Vec::new(); - let available_slots = concurrency_limit.saturating_sub(active_count); - for _ in 0..available_slots { - let request = Request::CommAssignNext { - id: REQUEST_ID, - session_id: ctx.session_id.clone(), - target_session: params.target_session.clone(), - working_dir: params.working_dir.clone(), - prefer_spawn: params.prefer_spawn, - spawn_if_needed: params.spawn_if_needed, - message: params.message.clone(), - model: params.model.clone(), - // Worker effort is not agent-controlled: the server applies - // `agents.swarm_effort` (default "low"). - effort: None, - }; - - match send_request(request).await { - Ok(ServerEvent::CommAssignTaskResponse { - task_id, - target_session, - .. - }) => assignments.push(format!("{} -> {}", task_id, target_session)), - Ok(ServerEvent::Error { message, .. }) - if message.contains("No runnable unassigned tasks") - || message.contains("No ready or completed swarm agents") => - { - break; - } - Ok(response) => { - ensure_success(&response)?; - } - Err(e) => { - return Err(anyhow::anyhow!("Failed to fill slots: {}", e)); - } - } - } - - if assignments.is_empty() { - Ok(ToolOutput::new(format!( - "No assignments made. Active: {}, limit: {}", - active_count, concurrency_limit - ))) - } else { - let mut output = format!( - "Filled {} slot(s):\n{}", - assignments.len(), - assignments - .into_iter() - .map(|line| format!("- {}", line)) - .collect::>() - .join("\n") - ); - if let Ok(summary) = fetch_plan_status(&ctx.session_id).await { - output.push_str(&format!("\n{}", format_plan_followup(&summary))); - } - Ok(ToolOutput::new(output)) - } - } - - "launch" | "run_plan" => { - // The coordinator's todo list is the plan: each lane (a group - // within a stage) becomes one item owned by one worker, whose - // own todo list is the lane. `launch` re-syncs every time so - // the plan never drifts; legacy `run_plan` keeps a hand-written - // `propose_plan` by only seeding an empty plan. - sync_plan_from_todos(&ctx, params.action == "run_plan").await?; - // Background-by-default: the plan driver runs as a managed - // background task (progress card, bg tool, notify/wake) so the - // coordinating agent stays responsive. Pass background=false - // to block inline until the plan reaches a terminal state. - if params.background.unwrap_or(true) { - run_swarm_plan_in_background(&ctx, params.clone()).await - } else { - run_swarm_plan_to_terminal(&ctx, ¶ms, &RunPlanReporter::inline()).await - } - } + "launch" | "run_plan" | "fill_slots" => Err(anyhow::anyhow!( + "'{}' was removed. Mark a todo lane `owner: \"worker\"` (todo phases[].lanes[].owner, \ + or update lane_owner) and the server spawns a worker for it as soon as its \ + dependencies are met. Use `plan_status` to watch lanes.", + params.action + )), "start" | "start_task" | "wake" | "resume" | "retry" | "reassign" | "replace" | "salvage" => { @@ -3071,8 +1778,7 @@ impl Tool for CommunicateTool { let timeout_minutes = params.timeout_minutes.unwrap_or(60); let timeout_secs = timeout_minutes * 60; // Public member waits are always asynchronous. The blocking - // CommAwaitMembers protocol remains available internally for the - // run_plan coordination loop, but agents must not park an entire + // CommAwaitMembers protocol remains available internally, but agents must not park an entire // turn waiting on a worker or a long-lived socket. let blocking_was_requested = params.background == Some(false); let background = true; @@ -3136,7 +1842,7 @@ impl Tool for CommunicateTool { _ => Err(anyhow::anyhow!( "Unknown action '{}'. Valid actions: share, share_append, read, message, broadcast, dm, channel, list, list_channels, channel_members, \ propose_plan, approve_plan, reject_plan, spawn, stop, assign_role, status, plan_status, summary, read_context, \ - resync_plan, assign_task, assign_next, fill_slots, run_plan, cleanup, start, start_task, wake, resume, retry, reassign, replace, salvage, subscribe_channel, unsubscribe_channel, await_members. \ + resync_plan, assign_task, assign_next, cleanup, start, start_task, wake, resume, retry, reassign, replace, salvage, subscribe_channel, unsubscribe_channel, await_members. \ To read messages addressed to you, use action='read'.", params.action )), diff --git a/crates/jcode-app-core/src/tool/communicate_schema_tests.rs b/crates/jcode-app-core/src/tool/communicate_schema_tests.rs index b9b36083ab7..24fbcbeabd0 100644 --- a/crates/jcode-app-core/src/tool/communicate_schema_tests.rs +++ b/crates/jcode-app-core/src/tool/communicate_schema_tests.rs @@ -98,16 +98,7 @@ fn spawning_actions_cannot_bypass_worker_model_policy() { swarm_model_allowlist: vec!["claude:claude-opus-4-8".to_string()], ..Default::default() }; - for action in [ - "spawn", - "assign_task", - "assign_next", - "fill_slots", - "run_plan", - "launch", - "retry", - "resume", - ] { + for action in ["spawn", "assign_task", "assign_next", "retry", "resume"] { let input: CommunicateInput = serde_json::from_value(json!({ "action": action, "model": "claude:claude-opus-4-8" })) @@ -312,13 +303,7 @@ fn existing_tool_keeps_prompt_while_new_tool_loads_edit() { #[test] fn spawning_action_inputs_preserve_requested_model() { - for action in [ - "spawn", - "assign_task", - "assign_next", - "fill_slots", - "run_plan", - ] { + for action in ["spawn", "assign_task", "assign_next"] { for model in [ "z-ai/glm-5.2:free", "openai-api:gpt-5.5", @@ -339,13 +324,7 @@ fn spawning_action_inputs_preserve_requested_model() { #[test] fn spawning_action_inputs_allow_omitted_or_null_model() { - for action in [ - "spawn", - "assign_task", - "assign_next", - "fill_slots", - "run_plan", - ] { + for action in ["spawn", "assign_task", "assign_next"] { let without_model: CommunicateInput = serde_json::from_value(json!({"action": action, "label": "reviewer"})).unwrap(); assert!(without_model.model.is_none()); @@ -438,7 +417,6 @@ fn schema_advertises_a_lean_lane_first_action_set() { .filter_map(|a| a.as_str()) .collect(); for kept in [ - "launch", "spawn", "message", "status", @@ -463,6 +441,7 @@ fn schema_advertises_a_lean_lane_first_action_set() { "reject_plan", "assign_task", "assign_next", + "launch", "fill_slots", "run_plan", "resync_plan", @@ -488,9 +467,6 @@ fn schema_advertises_a_lean_lane_first_action_set() { "phase", "prompt", "session_ids", - "background", - "concurrency_limit", - "retain_agents", "delivery", "wake", ] { diff --git a/crates/jcode-app-core/src/tool/communicate_tests.rs b/crates/jcode-app-core/src/tool/communicate_tests.rs index 470032d70e7..6bb8fa8dd78 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests.rs @@ -1,10 +1,9 @@ use super::{ CommunicateInput, CommunicateTool, canonical_swarm_action, cleanup_candidate_session_ids, - coordination_in_flight_count, default_await_target_statuses, default_cleanup_target_statuses, - format_awaited_members, format_awaited_members_with_reports, format_members, - format_plan_status, format_swarm_model_list, latest_assistant_report, - resolve_optional_target_session, resolve_run_plan_concurrency, swarm_member_is_drivable_worker, - swarm_member_is_in_flight, + coordination_in_flight_count, default_cleanup_target_statuses, format_awaited_members, + format_awaited_members_with_reports, format_members, format_plan_status, + format_swarm_model_list, latest_assistant_report, resolve_optional_target_session, + swarm_member_is_drivable_worker, swarm_member_is_in_flight, }; use crate::message::{Message, StreamEvent, ToolDefinition}; use crate::protocol::{ @@ -30,490 +29,6 @@ fn tool_is_named_swarm() { assert_eq!(CommunicateTool::new().name(), "swarm"); } -#[test] -fn task_id_from_output_path_extracts_background_task_id() { - assert_eq!( - super::task_id_from_output_path(Path::new("/tmp/tasks/123abc.output")), - Some("123abc") - ); - assert_eq!( - super::task_id_from_output_path(Path::new("/tmp/tasks/123abc.status.json")), - None - ); -} - -#[tokio::test] -async fn run_plan_reporter_finalize_puts_summary_before_log() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let output_path = dir.path().join("tsk42.output"); - let reporter = super::RunPlanReporter::background(&output_path); - assert_eq!(reporter.task_id.as_deref(), Some("tsk42")); - - reporter.log("assigned a -> session_fox").await; - reporter.log("assigned b -> session_wolf").await; - reporter - .finalize("Swarm plan reached terminal state.") - .await; - - let content = tokio::fs::read_to_string(&output_path) - .await - .expect("output file"); - let summary_idx = content - .find("Swarm plan reached terminal state.") - .expect("summary present"); - let log_idx = content.find("assigned a -> session_fox").expect("log kept"); - assert!( - summary_idx < log_idx, - "summary must lead the output file so completion previews are useful:\n{content}" - ); -} - -#[tokio::test] -async fn run_plan_reporter_inline_is_a_no_op() { - let reporter = super::RunPlanReporter::inline(); - assert!(reporter.task_id.is_none()); - // Must not panic or create files. - reporter.log("ignored").await; - reporter.progress(1, 2, "ignored".to_string()).await; - reporter.finalize("ignored").await; -} - -#[tokio::test] -async fn run_plan_driver_guard_blocks_while_driver_task_is_live() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager = crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()); - let session = "session-guard-live"; - - // Keep the fake driver alive long enough for the second claim to observe it. - let info = manager - .spawn_with_notify("swarm", None, session, false, false, |_| async { - tokio::time::sleep(Duration::from_secs(5)).await; - Ok(crate::background::TaskResult::completed(Some(0))) - }) - .await; - assert!(manager.is_live_task(&info.task_id)); - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(claim) => claim.record_task(&info.task_id), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("first claim for a fresh session must succeed") - } - } - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::AlreadyRunning(Some(task_id)) => { - assert_eq!(task_id, info.task_id); - } - super::RunPlanDriverClaimResult::AlreadyRunning(None) => { - panic!("claim was recorded with a task id, so the blocker should carry it") - } - super::RunPlanDriverClaimResult::Claimed(_) => { - panic!("second claim must be blocked while the driver task is live") - } - } - - manager.cancel(&info.task_id).await.expect("cancel driver"); -} - -#[tokio::test] -async fn run_plan_driver_guard_allows_restart_after_stale_driver() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager = crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()); - let session = "session-guard-stale"; - - // Simulate the pre-reload world: a status file on disk still says a swarm - // driver is Running for this session, and the per-process claim map still - // holds its task id, but no such task is live in this process (the map is - // fresh after reload / the task was pruned on completion). - let stale_task_id = "stalezzzz1"; - let stale_status = serde_json::json!({ - "task_id": stale_task_id, - "tool_name": "swarm", - "session_id": session, - "status": "running", - "exit_code": null, - "error": null, - "started_at": chrono::Utc::now().to_rfc3339(), - "completed_at": null, - "duration_secs": null, - "detached": false, - "notify": false, - "wake": false - }); - tokio::fs::write( - manager.status_path_for(stale_task_id), - serde_json::to_string_pretty(&stale_status).expect("serialize stale status"), - ) - .await - .expect("write stale status file"); - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(claim) => claim.record_task(stale_task_id), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("fresh session must be claimable") - } - } - assert!( - !manager.is_live_task(stale_task_id), - "stale task must not be live in this process" - ); - - // The stale Running status file and stale claim must not block restarting - // the driver. - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(_claim) => {} - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("stale (non-live) driver must not block a restart") - } - } -} - -#[tokio::test] -async fn run_plan_driver_guard_is_atomic_for_racing_claims() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager: &'static crate::background::BackgroundTaskManager = Box::leak(Box::new( - crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()), - )); - let session = "session-guard-race"; - - // Two run_plan calls in one batch race the claim; exactly one may win. - let mut join_set = tokio::task::JoinSet::new(); - for _ in 0..2 { - join_set.spawn(async move { - match super::try_claim_run_plan_driver(manager, session) { - // Keep the claim held (as the winner does while spawning). - super::RunPlanDriverClaimResult::Claimed(claim) => Some(claim), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => None, - } - }); - } - let mut held_claims = Vec::new(); - while let Some(result) = join_set.join_next().await { - if let Some(claim) = result.expect("claim task should not panic") { - held_claims.push(claim); - } - } - assert_eq!(held_claims.len(), 1, "exactly one racing claim may win"); -} - -#[tokio::test] -async fn run_plan_driver_guard_releases_claim_on_drop_without_task() { - let dir = tempfile::TempDir::new().expect("tempdir"); - let manager = crate::background::BackgroundTaskManager::with_output_dir(dir.path().into()); - let session = "session-guard-drop"; - - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(claim) => drop(claim), - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("fresh session must be claimable") - } - } - - // A failed/cancelled startup path must not permanently block the session. - match super::try_claim_run_plan_driver(&manager, session) { - super::RunPlanDriverClaimResult::Claimed(_claim) => {} - super::RunPlanDriverClaimResult::AlreadyRunning(_) => { - panic!("dropped Starting claim must be released") - } - } -} - -#[test] -fn run_plan_concurrency_defaults_and_clamps() { - assert_eq!( - resolve_run_plan_concurrency(None), - super::DEFAULT_RUN_PLAN_CONCURRENCY - ); - assert_eq!(resolve_run_plan_concurrency(Some(5)), 5); - assert_eq!(resolve_run_plan_concurrency(Some(0)), 1); -} - -#[test] -fn run_plan_utilization_tracks_peak_and_starvation() { - let mut util = super::RunPlanUtilization::default(); - - // Loop 1: 0 in flight, 8 open slots, dispatched 8 -> budget fully used. - util.record_loop(0, Some(8), 8); - // Loop 2: 8 in flight, 0 open slots, dispatched 0 -> saturated, not starved. - util.record_loop(8, Some(0), 0); - // Loop 3: 2 in flight, 6 open slots, dispatched 1 -> starved (5 idle slots). - util.record_loop(2, Some(6), 1); - - assert_eq!(util.peak_in_flight, 8); - assert_eq!(util.loops, 3); - assert_eq!(util.starved_loops, 1); - - let report = util.report(8); - assert!(report.contains("peak 8 of 8")); - assert!(report.contains("1 of 3 loop(s)")); -} - -#[test] -fn run_plan_utilization_handles_unbounded_budget() { - let mut util = super::RunPlanUtilization::default(); - // Unbounded budget: open slots are not meaningful, so no starvation - // accounting, but peak parallelism still records. - util.record_loop(10, None, 5); - assert_eq!(util.peak_in_flight, 15); - assert_eq!(util.starved_loops, 0); - let report = util.report(usize::MAX); - assert!(report.contains("peak 15 of unbounded")); -} - -#[test] -fn await_wakes_only_for_ready_items_beyond_the_wave_baseline() { - let baseline: std::collections::HashSet = - ["stuck".to_string(), "assigned".to_string()].into(); - let mut summary = crate::protocol::PlanGraphStatus { - swarm_id: None, - version: 3, - item_count: 6, - ready_ids: vec!["stuck".to_string()], - blocked_ids: Vec::new(), - active_ids: vec!["a1".to_string()], - completed_ids: Vec::new(), - failed_ids: Vec::new(), - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - // Items already ready at wave start (even permanently-undispatchable - // ones) must not wake the driver: that would busy-spin the await. - assert!(!super::await_should_wake_for_new_ready(&baseline, &summary)); - - // No ready items at all: keep waiting on members. - summary.ready_ids.clear(); - assert!(!super::await_should_wake_for_new_ready(&baseline, &summary)); - - // A retried failed node re-enters ready as a NEW id -> wake and dispatch. - summary.ready_ids = vec!["stuck".to_string(), "retried-node".to_string()]; - assert!(super::await_should_wake_for_new_ready(&baseline, &summary)); -} - -#[test] -fn run_plan_progress_counts_only_completed_toward_percent_and_shows_live_active() { - // Regression: a plan with 33 completed / 116 failed of 152 used to report - // terminal/total = 149/152 (~98%) with active 0 while four externally - // assigned workers were still running. - let summary = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 9, - item_count: 152, - ready_ids: Vec::new(), - blocked_ids: Vec::new(), - active_ids: Vec::new(), - completed_ids: (0..33).map(|i| format!("c{i}")).collect(), - failed_ids: (0..116).map(|i| format!("f{i}")).collect(), - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - let (completed, total, message) = super::run_plan_progress_snapshot(&summary, 4, 137); - // Percent driver is completed/total: 33/152 (~22%), never ~98%. - assert_eq!(completed, 33); - assert_eq!(total, 152); - // Failed nodes are surfaced separately, and live in-flight workers show as - // active even when the plan's own active_ids is empty (external - // assign_task dispatches). - assert_eq!( - message, - "completed 33 · failed 116 · blocked 0 · active 4 · assignments 137" - ); - - // The normalized background progress percent derived from (current,total) - // must match completed/total, not terminal/total. - let progress = crate::bus::BackgroundTaskProgress { - kind: crate::bus::BackgroundTaskProgressKind::Determinate, - percent: None, - message: Some(message), - current: Some(completed as u64), - total: Some(total as u64), - unit: Some("nodes".to_string()), - eta_seconds: None, - updated_at: chrono::Utc::now().to_rfc3339(), - source: crate::bus::BackgroundTaskProgressSource::Reported, - } - .normalize(); - let percent = progress.percent.expect("determinate percent"); - assert!( - (percent - 21.71).abs() < 0.1, - "33/152 must normalize to ~21.7%, got {percent}" - ); -} - -#[test] -fn run_plan_progress_active_prefers_plan_execution_state_when_larger() { - let summary = crate::protocol::PlanGraphStatus { - swarm_id: None, - version: 1, - item_count: 10, - ready_ids: Vec::new(), - blocked_ids: vec!["b1".to_string()], - active_ids: vec!["a1".to_string(), "a2".to_string(), "a3".to_string()], - completed_ids: vec!["c1".to_string(), "c2".to_string()], - failed_ids: vec!["f1".to_string()], - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - // Plan says 3 active but only 1 live member is observable (e.g. status - // propagation lag): keep the larger plan-state number. - let (completed, total, message) = super::run_plan_progress_snapshot(&summary, 1, 5); - assert_eq!((completed, total), (2, 10)); - assert_eq!( - message, - "completed 2 · failed 1 · blocked 1 · active 3 · assignments 5" - ); -} - -#[test] -fn assign_error_classification_recovers_on_member_cap_instead_of_failing() { - use super::AssignErrorAction; - - // Graceful exhaustion of work or workers ends the assignment burst. - assert_eq!( - super::classify_assign_error( - "No runnable unassigned tasks are available in the swarm plan" - ), - AssignErrorAction::BreakGracefully - ); - assert_eq!( - super::classify_assign_error( - "No ready or completed swarm agents are available for automatic task assignment." - ), - AssignErrorAction::BreakGracefully - ); - - // The member cap must trigger recovery (cleanup + reuse fallback), not a - // run-aborting failure. The server wraps the cap message in a spawn-failure - // prefix, so classification must match on the substring. - assert_eq!( - super::classify_assign_error( - "Failed to spawn preferred worker: Swarm member limit reached (max 1000). \ - This swarm already has 1000 agents; it cannot spawn more." - ), - AssignErrorAction::RecoverCapacity - ); - - // Anything else is still a real failure. - assert_eq!( - super::classify_assign_error("Not in a swarm."), - AssignErrorAction::Fail - ); -} - -#[test] -fn cap_recovery_prefers_cleanup_then_reuse_then_gives_up() { - use super::CapRecoveryStep; - - // First cap hit with freed capacity: retry keeping the fresh-spawn preference. - assert_eq!(super::cap_recovery_step(1, 3), CapRecoveryStep::RetryFresh); - // First cap hit but nothing could be freed: fall back to reusing ready workers. - assert_eq!(super::cap_recovery_step(1, 0), CapRecoveryStep::RetryReuse); - // Recovery already ran and the cap still refuses: continue with in-flight - // work instead of aborting or spinning. - assert_eq!(super::cap_recovery_step(2, 0), CapRecoveryStep::GiveUp); - assert_eq!(super::cap_recovery_step(3, 5), CapRecoveryStep::GiveUp); -} - -#[test] -fn run_plan_driver_failures_carry_worker_retention_hint() { - // Every driver-failure path must tell the caller the spawned workers are - // still running and how to stop them. - let hinted = super::with_worker_retention_hint( - "run_plan stalled after 3 loop(s): no ready tasks and no in-flight workers.".to_string(), - ); - assert!(hinted.contains("Spawned workers were retained")); - assert!(hinted.contains("swarm cleanup")); - - // Max-loops keeps its intentional retention-for-inspection wording but - // still gains the actionable hint. - let max_loops = super::with_worker_retention_hint( - "run_plan exceeded 200 coordination loops; leaving workers untouched for inspection" - .to_string(), - ); - assert!(max_loops.contains("swarm cleanup")); - - // Idempotent: re-wrapping (e.g. the background wrapper re-reporting the - // error) must not duplicate the hint. - let twice = super::with_worker_retention_hint(hinted.clone()); - assert_eq!(twice.matches("Spawned workers were retained").count(), 1); -} - -#[test] -fn run_plan_terminal_summary_reports_failed_nodes() { - let base = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 1, - item_count: 4, - ready_ids: Vec::new(), - blocked_ids: Vec::new(), - active_ids: Vec::new(), - completed_ids: vec!["a".to_string(), "b".to_string()], - failed_ids: vec!["c".to_string(), "d".to_string()], - failed_reasons: Default::default(), - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - - let with_failures = super::format_run_plan_terminal_summary(5, &base, 7); - assert!(with_failures.contains("completed=2")); - assert!(with_failures.contains("failed=2")); - assert!(with_failures.contains("Failed nodes: c, d")); - assert!(with_failures.contains("did NOT finish cleanly")); - - // A clean run reports failed=0 and no failure callout. - let clean = crate::protocol::PlanGraphStatus { - completed_ids: vec![ - "a".to_string(), - "b".to_string(), - "c".to_string(), - "d".to_string(), - ], - failed_ids: Vec::new(), - failed_reasons: Default::default(), - ..base - }; - let clean_summary = super::format_run_plan_terminal_summary(5, &clean, 7); - assert!(clean_summary.contains("failed=0")); - assert!(!clean_summary.contains("Failed nodes")); -} - -#[test] -fn plan_terminal_node_count_includes_failed_without_double_counting() { - let summary = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 1, - item_count: 4, - ready_ids: Vec::new(), - blocked_ids: vec!["x".to_string()], - active_ids: Vec::new(), - completed_ids: vec!["a".to_string()], - failed_ids: vec!["c".to_string()], - failed_reasons: Default::default(), - // "x" is both blocked and cyclic; it must count once. - cycle_ids: vec!["x".to_string()], - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - // a (completed) + c (failed) + x (blocked/cycle, deduped) = 3. Without - // failed_ids in the terminal count a run with failed nodes would never - // satisfy terminal_count >= item_count and run_plan could spin or stall. - assert_eq!(super::plan_terminal_node_count(&summary), 3); -} - #[test] fn canonical_swarm_action_maps_common_synonyms() { assert_eq!(canonical_swarm_action("inbox"), "read"); @@ -1119,9 +634,6 @@ async fn wait_for_member_presence( } } -#[path = "communicate_tests/credential_failures.rs"] -mod credential_failures; - include!("communicate_tests/input_format.rs"); include!("communicate_tests/end_to_end.rs"); include!("communicate_tests/assignment.rs"); diff --git a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs index 15131df85cc..d7a79757cba 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/assignment.rs @@ -467,100 +467,6 @@ async fn communicate_assign_next_can_spawn_if_needed_server_side() { server_task.abort(); } -#[tokio::test] -async fn communicate_fill_slots_tops_up_to_concurrency_limit() { - let _env_lock = crate::storage::lock_test_env(); - let runtime_dir = tempfile::TempDir::new().expect("runtime tempdir"); - let repo_dir = std::env::current_dir().expect("repo cwd"); - let socket_path = runtime_dir.path().join("jcode.sock"); - let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); - // Keep the developer's real config (agents.swarm_model, providers) out of spawns. - let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); - let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); - let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); - - let provider: Arc = Arc::new(DelayedTestProvider { - delay: Duration::from_millis(300), - }); - let server = Arc::new(Server::new(provider)); - let mut server_task = { - let server = Arc::clone(&server); - tokio::spawn(async move { server.run().await }) - }; - - wait_for_server_socket(&socket_path, &mut server_task) - .await - .expect("server socket should be ready"); - - let mut watcher = RawClient::connect(&socket_path) - .await - .expect("watcher should connect"); - watcher - .subscribe(&repo_dir) - .await - .expect("watcher subscribe"); - - let watcher_session = watcher.session_id().await.expect("watcher session id"); - let tool = CommunicateTool::new(); - let ctx = test_ctx(&watcher_session, &repo_dir); - - tool.execute( - json!({ - "action": "assign_role", - "target_session": watcher_session, - "role": "coordinator" - }), - ctx.clone(), - ) - .await - .expect("self-promotion to coordinator should succeed"); - - tool.execute( - json!({ - "action": "propose_plan", - "plan_items": [{ - "id": "task-1", - "content": "first task", - "status": "queued", - "priority": "high" - }, { - "id": "task-2", - "content": "second task", - "status": "queued", - "priority": "high" - }, { - "id": "task-3", - "content": "third task", - "status": "queued", - "priority": "high" - }] - }), - ctx.clone(), - ) - .await - .expect("plan proposal should succeed"); - - let output = tool - .execute( - json!({ - "action": "fill_slots", - "concurrency_limit": 2, - "spawn_if_needed": true - }), - ctx, - ) - .await - .expect("fill_slots should succeed"); - - assert!( - output.output.contains("Filled 2 slot(s):"), - "unexpected fill_slots output: {}", - output.output - ); - - server_task.abort(); -} - #[tokio::test] async fn communicate_assign_task_can_prefer_fresh_spawn_over_reuse() { let _env_lock = crate::storage::lock_test_env(); diff --git a/crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs b/crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs deleted file mode 100644 index 20acbe9e5fc..00000000000 --- a/crates/jcode-app-core/src/tool/communicate_tests/credential_failures.rs +++ /dev/null @@ -1,177 +0,0 @@ -use super::*; - -#[test] -fn default_await_members_targets_include_ready() { - assert_eq!( - default_await_target_statuses(), - vec!["ready", "completed", "stopped", "failed", "crashed"] - ); -} - -fn credential_failed_worker(session_id: &str, detail: &str, age_secs: u64) -> AgentInfo { - AgentInfo { - session_id: session_id.to_string(), - status: Some("failed".to_string()), - detail: Some(detail.to_string()), - role: Some("agent".to_string()), - is_headless: Some(true), - report_back_to_session_id: Some("coord".to_string()), - status_age_secs: Some(age_secs), - provider_name: Some("anthropic".to_string()), - ..Default::default() - } -} - -#[test] -fn credential_failure_wave_detected_for_recent_auth_failed_workers() { - // The observed incident: every dispatched worker died within seconds with - // an Anthropic 401 (expired OAuth + revoked refresh token) and nothing - // completed. That must classify as a wave, not as N independent failures. - let members = vec![ - AgentInfo { - session_id: "coord".to_string(), - status: Some("running".to_string()), - role: Some("coordinator".to_string()), - ..Default::default() - }, - credential_failed_worker("w1", "Anthropic API error (401 Unauthorized)", 2), - credential_failed_worker("w2", "Anthropic API error (401 Unauthorized)", 3), - credential_failed_worker("w3", "invalid_grant: refresh token invalid", 5), - ]; - let wave = super::super::detect_credential_failure_wave(&members, "coord", 0, 60) - .expect("three recent credential failures with zero completions is a wave"); - assert_eq!(wave.session_ids, vec!["w1", "w2", "w3"]); - assert_eq!(wave.sample_detail, "Anthropic API error (401 Unauthorized)"); - assert_eq!(wave.provider.as_deref(), Some("anthropic")); - - let message = super::super::format_credential_failure_wave_error(&wave, 60); - assert!(message.contains("paused dispatching")); - assert!(message.contains("3 worker(s)")); - assert!(message.contains("401 Unauthorized")); - assert!(message.contains("`jcode login --provider claude`")); -} - -#[test] -fn credential_failure_wave_requires_at_least_two_workers() { - let members = vec![credential_failed_worker( - "w1", - "Anthropic API error (401 Unauthorized)", - 2, - )]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 0, 60), - None, - "one bad worker is not a wave" - ); -} - -#[test] -fn credential_failure_wave_not_detected_once_anything_completed() { - // Completions prove the credential works (or worked); later auth failures - // are then per-worker problems, not a route-wide outage to halt over. - let members = vec![ - credential_failed_worker("w1", "Anthropic API error (401 Unauthorized)", 2), - credential_failed_worker("w2", "Anthropic API error (401 Unauthorized)", 3), - ]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 1, 60), - None - ); -} - -#[test] -fn credential_failure_wave_ignores_stale_and_non_credential_failures() { - let members = vec![ - // Stale: failed long before this window (e.g. a previous, already - // diagnosed wave; the user has since re-authenticated and retried). - credential_failed_worker("old", "Anthropic API error (401 Unauthorized)", 3600), - // Unknown age must not count either. - AgentInfo { - status_age_secs: None, - ..credential_failed_worker("ageless", "401 Unauthorized", 0) - }, - // Non-credential failure. - credential_failed_worker("crashed", "worker panicked: index out of bounds", 2), - // Only one recent credential failure remains: below the wave minimum. - credential_failed_worker("w1", "Anthropic API error (401 Unauthorized)", 2), - ]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 0, 60), - None - ); -} - -#[test] -fn credential_failure_wave_ignores_foreign_members() { - // A foreign, client-attached session that failed with an auth error is not - // one of run_plan's workers; it must not trip the breaker. - let foreign = AgentInfo { - is_headless: Some(false), - report_back_to_session_id: None, - ..credential_failed_worker("foreign", "401 Unauthorized", 2) - }; - let members = vec![ - foreign, - credential_failed_worker("w1", "401 Unauthorized", 2), - ]; - assert_eq!( - super::super::detect_credential_failure_wave(&members, "coord", 0, 60), - None - ); -} - -#[test] -fn credential_login_fix_hint_maps_provider_names() { - assert_eq!( - super::super::credential_login_fix_hint(Some("anthropic")), - "`jcode login --provider claude`" - ); - assert_eq!( - super::super::credential_login_fix_hint(Some("OpenAI")), - "`jcode login --provider openai`" - ); - assert_eq!( - super::super::credential_login_fix_hint(Some("copilot")), - "`jcode login --provider copilot`" - ); - assert_eq!( - super::super::credential_login_fix_hint(None), - "`jcode login --provider `" - ); -} - -#[test] -fn run_plan_terminal_summary_includes_recorded_failure_reasons() { - let mut failed_reasons = std::collections::BTreeMap::new(); - failed_reasons.insert( - "c".to_string(), - "task failed: Anthropic API error (401 Unauthorized)".to_string(), - ); - let summary = crate::protocol::PlanGraphStatus { - swarm_id: Some("swarm-a".to_string()), - version: 1, - item_count: 2, - ready_ids: Vec::new(), - blocked_ids: Vec::new(), - active_ids: Vec::new(), - completed_ids: vec!["a".to_string()], - failed_ids: vec!["c".to_string()], - failed_reasons, - cycle_ids: Vec::new(), - unresolved_dependency_ids: Vec::new(), - next_ready_ids: Vec::new(), - newly_ready_ids: Vec::new(), - }; - let output = super::super::format_run_plan_terminal_summary(3, &summary, 2); - assert!(output.contains("Failed nodes: c")); - assert!( - output.contains("c: task failed: Anthropic API error (401 Unauthorized)"), - "terminal summary must carry the recorded failure reason:\n{output}" - ); - - let plan_status = format_plan_status(&summary).output; - assert!( - plan_status.contains("c: task failed: Anthropic API error (401 Unauthorized)"), - "plan_status must display the recorded failure reason:\n{plan_status}" - ); -} diff --git a/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs b/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs index 33b2b70213b..6f044248240 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/end_to_end.rs @@ -239,63 +239,6 @@ async fn communicate_await_members_background_returns_immediately_and_notifies() server_task.abort(); } -#[tokio::test] -async fn communicate_run_plan_with_empty_plan_returns_inline_even_in_background_mode() { - let _env_lock = crate::storage::lock_test_env(); - let runtime_dir = tempfile::TempDir::new().expect("runtime tempdir"); - let repo_dir = std::env::current_dir().expect("repo cwd"); - let socket_path = runtime_dir.path().join("jcode.sock"); - let _runtime = EnvGuard::set("JCODE_RUNTIME_DIR", runtime_dir.path()); - // Keep the developer's real config (agents.swarm_model, providers) out of spawns. - let _home = EnvGuard::set("JCODE_HOME", runtime_dir.path()); - let _socket = EnvGuard::set("JCODE_SOCKET", &socket_path); - let _debug = EnvGuard::set("JCODE_DEBUG_CONTROL", "1"); - - let provider: Arc = Arc::new(DelayedTestProvider { - delay: Duration::from_millis(50), - }); - let server = Arc::new(Server::new(provider)); - let mut server_task = { - let server = Arc::clone(&server); - tokio::spawn(async move { server.run().await }) - }; - - let socket_path = runtime_dir.path().join("jcode.sock"); - wait_for_server_socket(&socket_path, &mut server_task) - .await - .expect("server socket should be ready"); - - let mut client = RawClient::connect(&socket_path) - .await - .expect("client should connect"); - client.subscribe(&repo_dir).await.expect("subscribe"); - let session = client.session_id().await.expect("session id"); - - let tool = CommunicateTool::new(); - let ctx = test_ctx(&session, &repo_dir); - - // Background is the default; with no plan the validation happens inline and - // no background task should be started. - let output = tokio::time::timeout( - Duration::from_secs(5), - tool.execute(json!({"action": "run_plan"}), ctx.clone()), - ) - .await - .expect("run_plan should return promptly") - .expect("run_plan should succeed"); - assert!( - output.output.contains("No swarm plan items to run."), - "expected inline empty-plan response, got: {}", - output.output - ); - assert!( - output.metadata.is_none(), - "empty plan must not start a background driver" - ); - - server_task.abort(); -} - #[tokio::test] async fn communicate_status_returns_busy_snapshot_for_running_member() { let _env_lock = crate::storage::lock_test_env(); diff --git a/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs b/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs index d6f020c9384..f3fe55cd712 100644 --- a/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs +++ b/crates/jcode-app-core/src/tool/communicate_tests/input_format.rs @@ -89,13 +89,11 @@ fn communicate_input_accepts_prefer_spawn() { #[test] fn communicate_input_accepts_cleanup_lifecycle_flags() { let parsed: CommunicateInput = serde_json::from_value(serde_json::json!({ - "action": "run_plan", - "force": true, - "retain_agents": true + "action": "stop", + "force": true })) .expect("lifecycle flags should deserialize"); assert_eq!(parsed.force, Some(true)); - assert_eq!(parsed.retain_agents, Some(true)); } #[test] diff --git a/crates/jcode-app-core/src/tool/todo.rs b/crates/jcode-app-core/src/tool/todo.rs index 2513f7cfa7e..0db6c4acf3e 100644 --- a/crates/jcode-app-core/src/tool/todo.rs +++ b/crates/jcode-app-core/src/tool/todo.rs @@ -31,8 +31,9 @@ mod external_blocker_tests; #[path = "todo_lane_dependency_tests.rs"] mod lane_dependency_tests; -/// A prerequisite edit naming one member applies atomically to its entire lane. -fn patch_lane_after(todos: &mut [Value], id: &str, value: Value) -> Result<()> { +/// A lane-wide field edit naming one member applies atomically to its entire +/// lane (`lane_after`, `lane_owner`). +fn patch_lane_field(todos: &mut [Value], id: &str, field: &str, value: Value) -> Result<()> { let typed: Vec = serde_json::from_value(Value::Array(todos.to_vec()))?; let target = typed .iter() @@ -41,12 +42,16 @@ fn patch_lane_after(todos: &mut [Value], id: &str, value: Value) -> Result<()> { let lane = crate::todo::stages::lane_id(target); for (raw, todo) in todos.iter_mut().zip(&typed) { if crate::todo::stages::lane_id(todo) == lane { - raw["lane_after"] = value.clone(); + raw[field] = value.clone(); } } Ok(()) } +fn patch_lane_after(todos: &mut [Value], id: &str, value: Value) -> Result<()> { + patch_lane_field(todos, id, "lane_after", value) +} + fn validate_external_blockers(previous: &[TodoItem], todos: &[TodoItem]) -> Result<()> { let now = chrono::Utc::now(); for todo in todos { @@ -352,6 +357,9 @@ fn apply_todo_patch(mut input: Value, stored: impl FnOnce() -> Value) -> Result< if let Some(value) = fields.get("lane_after") { patch_lane_after(&mut todos, id, value.clone())?; } + if let Some(value) = fields.get("lane_owner") { + patch_lane_field(&mut todos, id, "lane_owner", value.clone())?; + } } for mut add in adds { @@ -550,7 +558,7 @@ fn flatten_phases(input: &mut Value) -> Result<()> { .ok_or_else(|| anyhow::anyhow!("lane must be an object"))?; if lane .keys() - .any(|key| !["lane", "items", "after"].contains(&key.as_str())) + .any(|key| !["lane", "items", "after", "owner"].contains(&key.as_str())) { bail!("unexpected lane field"); } @@ -573,9 +581,17 @@ fn flatten_phases(input: &mut Value) -> Result<()> { .as_object() .cloned() .ok_or_else(|| anyhow::anyhow!("item must be an object"))?; - if ["phase", "lane", "stage", "group", "wave", "lane_after"] - .iter() - .any(|key| item.contains_key(*key)) + if [ + "phase", + "lane", + "stage", + "group", + "wave", + "lane_after", + "lane_owner", + ] + .iter() + .any(|key| item.contains_key(*key)) { bail!("hierarchical items must not own phase/lane/stage/group membership"); } @@ -594,6 +610,9 @@ fn flatten_phases(input: &mut Value) -> Result<()> { if let Some(after) = lane.get("after") { item.insert("lane_after".into(), after.clone()); } + if let Some(owner) = lane.get("owner").filter(|owner| !owner.is_null()) { + item.insert("lane_owner".into(), owner.clone()); + } todos.push(Value::Object(item)); } } @@ -1435,6 +1454,10 @@ impl Tool for TodoTool { "items": {"type": "string", "minLength": 1}, "description": "Earlier-phase prerequisite task names. Updates whole lane; null restores phase barrier." }); + schema["properties"]["todos"]["items"]["properties"]["lane_owner"] = json!({ + "type": "string", "enum": ["self", "worker"], + "description": "Who runs the lane. worker: a swarm worker auto-spawns when the lane is ready. Updates whole lane." + }); let mut add_properties = schema["properties"]["todos"]["items"]["properties"].clone(); add_properties["after"] = json!({ "type": "string", @@ -1465,6 +1488,7 @@ impl Tool for TodoTool { properties.remove("phase"); properties.remove("lane"); properties.remove("lane_dependencies"); + properties.remove("lane_owner"); } item["additionalProperties"] = json!(false); schema["properties"]["phases"] = json!({ @@ -1477,6 +1501,9 @@ impl Tool for TodoTool { "type": ["array", "null"], "minItems": 1, "uniqueItems": true, "items": {"type": "string", "minLength": 1}, "description": "Earlier-phase prerequisite task names; replaces phase barrier. Omit/null for normal ordering." + }, "owner": { + "type": "string", "enum": ["self", "worker"], + "description": "Who runs the lane. Default self. worker: a swarm worker auto-spawns when the lane is ready, no launch call." }, "items": {"type": "array", "minItems": 1, "items": item}} } }}} @@ -1499,6 +1526,7 @@ impl Tool for TodoTool { // Goals/plan-only writes keep the stored todo list. let previous = load_todos(&ctx.session_id).unwrap_or_default(); let mut todos = params.todos.unwrap_or_else(|| previous.clone()); + crate::todo::stages::normalize_lane_owners(&mut todos); crate::todo::store::validate_session_lane_dependencies( &crate::storage::jcode_dir()?, &ctx.session_id, diff --git a/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs b/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs index 5eb161f3afb..81a2e39d155 100644 --- a/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs +++ b/crates/jcode-app-core/src/tool/todo_lane_dependency_tests.rs @@ -136,3 +136,44 @@ fn conflicting_dependency_aliases_are_rejected() { ); assert!(result.unwrap_err().to_string().contains("not both")); } + +#[test] +fn phase_lane_owner_marks_every_member_and_update_flips_whole_lane() { + let todos = parsed(json!({"phases": [ + {"phase": 1, "lanes": [ + {"lane": "api", "owner": "worker", "items": [ + {"id": "a", "content": "A", "status": "pending", "priority": "high"}, + {"id": "b", "content": "B", "status": "pending", "priority": "high"} + ]}, + {"lane": "mine", "items": [{"id": "c", "content": "C", "status": "pending", "priority": "high"}]} + ]} + ]})); + assert!( + todos[..2] + .iter() + .all(crate::todo::stages::lane_requests_worker) + ); + assert!(!crate::todo::stages::lane_requests_worker(&todos[2])); + + let stored = json!([item("a", 1, "api"), item("b", 1, "api")]); + let updated = apply_todo_patch( + json!({"update": [{"id": "b", "lane_owner": "worker"}]}), + || stored, + ) + .unwrap(); + let todos = parsed(updated); + assert!(todos.iter().all(crate::todo::stages::lane_requests_worker)); +} + +#[test] +fn unknown_lane_owner_is_rejected() { + let mut bad = TodoItem { + id: "a".into(), + content: "A".into(), + status: "pending".into(), + priority: "high".into(), + ..Default::default() + }; + bad.lane_owner = Some("robot".into()); + assert!(crate::todo::stages::validate_lane_dependencies(&[bad]).is_err()); +} diff --git a/crates/jcode-app-core/src/tool/todo_tests.rs b/crates/jcode-app-core/src/tool/todo_tests.rs index 544109ce16f..cecb9c17850 100644 --- a/crates/jcode-app-core/src/tool/todo_tests.rs +++ b/crates/jcode-app-core/src/tool/todo_tests.rs @@ -366,7 +366,8 @@ fn schema_advertises_intent_and_todos() { assert!(plan_props.contains_key("understands_user_intent")); assert!(!plan_props.contains_key("alignment_score")); assert!(!plan_props.contains_key("user_intention_alignment")); - assert_eq!(plan_props.len(), 2); + assert!(plan_props.contains_key("title")); + assert_eq!(plan_props.len(), 3); let plan_required = props["plan"]["required"] .as_array() .expect("plan should advertise required fields"); diff --git a/crates/jcode-base/src/mobile_sync_tests.rs b/crates/jcode-base/src/mobile_sync_tests.rs index 6a4088927e2..1941831bfa2 100644 --- a/crates/jcode-base/src/mobile_sync_tests.rs +++ b/crates/jcode-base/src/mobile_sync_tests.rs @@ -1,6 +1,47 @@ use super::*; use jcode_session_types::StoredMessage; +#[tokio::test] +#[ignore = "manual local filesystem reconciliation benchmark"] +async fn benchmark_unchanged_reconciliation() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("benchmark-no-network")), + ("JCODE_API_BASE", std::ffi::OsStr::new("http://127.0.0.1:1")), + ]); + DIRTY_SESSIONS.lock().unwrap().clear(); + let dir = home.path().join("sessions"); + std::fs::create_dir_all(&dir).unwrap(); + let mut state = MobileSyncState { enabled: true, ..Default::default() }; + for index in 0..1300 { + let id = format!("benchmark-{index:04}"); + let path = dir.join(format!("{id}.json")); + // An unchanged revision must never need to deserialize a transcript. + std::fs::write(&path, b"{}").unwrap(); + state.sessions.insert(id, SessionProgress { + uploaded: 100, + mtime_ms: session_mtime_ms(&path), + revision: Some(session_revision(&path)), + fingerprint: Some(42), + }); + } + save_state(&state).unwrap(); + // Enabling increments the fence, so seed progress in the current generation. + state.generation = load_state().generation; + save_state(&state).unwrap(); + let client = reqwest::Client::new(); + let mut elapsed = Vec::new(); + for _ in 0..7 { + let started = Instant::now(); + assert_eq!(sync_once(&client).await.unwrap(), SyncReport::default()); + elapsed.push(started.elapsed().as_micros()); + } + elapsed.sort_unstable(); + eprintln!("unchanged reconciliation: candidates=1300 passes=7 median_us={} max_us={} samples_us={elapsed:?}", elapsed[3], elapsed[6]); +} + #[test] fn sync_timing_records_only_static_fields_and_elapsed_milliseconds() { let mut timing = SyncTiming::new("test_stage"); diff --git a/crates/jcode-base/src/process_title.rs b/crates/jcode-base/src/process_title.rs index 30cfdc589b8..d971407b5d1 100644 --- a/crates/jcode-base/src/process_title.rs +++ b/crates/jcode-base/src/process_title.rs @@ -238,6 +238,7 @@ mod tests { id: "window-title".to_string(), group: Some("resume title sync".to_string()), lane_after: None, + lane_owner: None, confidence: Some(crate::todo::ConfidenceState::Plausible), completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-base/src/todo.rs b/crates/jcode-base/src/todo.rs index 114c4d5695d..99c52d72895 100644 --- a/crates/jcode-base/src/todo.rs +++ b/crates/jcode-base/src/todo.rs @@ -306,6 +306,7 @@ mod tests { id: content.to_ascii_lowercase().replace(' ', "-"), group: group.map(str::to_string), lane_after: None, + lane_owner: None, confidence: None, completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-task-types/src/lib.rs b/crates/jcode-task-types/src/lib.rs index d2831a60e86..fb4b90554c5 100644 --- a/crates/jcode-task-types/src/lib.rs +++ b/crates/jcode-task-types/src/lib.rs @@ -457,6 +457,9 @@ where Ok(value) } +/// `TodoItem::lane_owner` value that requests an auto-spawned swarm worker. +pub const LANE_OWNER_WORKER: &str = "worker"; + #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct TodoItem { pub content: String, @@ -474,6 +477,11 @@ pub struct TodoItem { /// only to todos in earlier phases by first appearance. #[serde(default, skip_serializing_if = "Option::is_none")] pub lane_after: Option>, + /// Who runs this lane: [`LANE_OWNER_WORKER`] makes the swarm server spawn a + /// dedicated worker as soon as the lane is ready; anything else (default) + /// means this todo list's own agent runs it. Lane-wide, like `lane_after`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub lane_owner: Option, /// Forward-looking evidence state that this todo can be completed /// correctly. Legacy sessions stored a 0-100 score; numbers map onto the /// closest state on load. diff --git a/crates/jcode-todo-policy/src/completion_report_tests.rs b/crates/jcode-todo-policy/src/completion_report_tests.rs index 241398967dc..fbe0115580f 100644 --- a/crates/jcode-todo-policy/src/completion_report_tests.rs +++ b/crates/jcode-todo-policy/src/completion_report_tests.rs @@ -10,6 +10,7 @@ fn uncapped_and_preserves_every_todo_field() { priority: "high".into(), group: Some("lane".into()), lane_after: None, + lane_owner: None, details: Some(format!("Full details {i}")), confidence: Some(ConfidenceState::Verified), completion_confidence: Some(ConfidenceState::Validated), diff --git a/crates/jcode-todo-policy/src/lib.rs b/crates/jcode-todo-policy/src/lib.rs index 5c1fcefbc6e..84c557a2f59 100644 --- a/crates/jcode-todo-policy/src/lib.rs +++ b/crates/jcode-todo-policy/src/lib.rs @@ -34,8 +34,8 @@ pub const TODO_QUALITY_GATE_MAX_APPROX_TOKENS: usize = 64; pub use jcode_task_types::{ Autonomy, ConfidenceState, DeliveryState, Difficulty, FeedbackLoopCoverage, FeedbackLoopRelevance, FeedbackLoopState, FeedbackLoopTraceability, IntentUnderstanding, - IterationMaturity, TodoCompleteBlocker, TodoExternalBlocker, TodoGoal, TodoGoalChange, - TodoGoalField, TodoItem, TodoPlan, TodoPlanChange, TodoPlanField, + IterationMaturity, LANE_OWNER_WORKER, TodoCompleteBlocker, TodoExternalBlocker, TodoGoal, + TodoGoalChange, TodoGoalField, TodoItem, TodoPlan, TodoPlanChange, TodoPlanField, }; /// Return the canonical todo status for model-written status vocabulary. diff --git a/crates/jcode-todo-policy/src/stages.rs b/crates/jcode-todo-policy/src/stages.rs index ac24d5cafce..7d69bf88397 100644 --- a/crates/jcode-todo-policy/src/stages.rs +++ b/crates/jcode-todo-policy/src/stages.rs @@ -134,6 +134,13 @@ pub fn validate_lane_dependencies(todos: &[TodoItem]) -> Result<(), String> { } for (phase, stage) in stages.iter().enumerate() { for lane in &stage.lanes { + if let Some(owner) = todos[lane.items[0]].lane_owner.as_deref() + && !matches!(owner, "worker" | "self") + { + return Err(format!( + "lane owner must be \"worker\" or \"self\", got {owner:?}" + )); + } let expected = &todos[lane.items[0]].lane_after; if lane.items.iter().any(|&i| &todos[i].lane_after != expected) { return Err("lane_after must be consistent across every lane member".into()); @@ -166,6 +173,30 @@ pub fn validate_lane_dependencies(todos: &[TodoItem]) -> Result<(), String> { Ok(()) } +/// Whether this todo's lane asked for a dedicated auto-spawned swarm worker. +pub fn lane_requests_worker(todo: &TodoItem) -> bool { + todo.lane_owner.as_deref() == Some(crate::LANE_OWNER_WORKER) +} + +/// Make `lane_owner` lane-wide: every member takes the value of the lane's +/// first member that sets one, so adding an item to a worker lane keeps it a +/// worker lane and a single-item edit flips the whole lane. +pub fn normalize_lane_owners(todos: &mut [TodoItem]) { + let mut owners: std::collections::HashMap> = + std::collections::HashMap::new(); + for todo in todos.iter() { + let entry = owners.entry(lane_id(todo)).or_insert(None); + if entry.is_none() { + *entry = todo.lane_owner.clone(); + } + } + for todo in todos.iter_mut() { + let owner = owners.get(&lane_id(todo)).cloned().flatten(); + // `self` is the default; store only the meaningful value. + todo.lane_owner = owner.filter(|owner| owner != "self"); + } +} + #[cfg(test)] mod tests { use super::*; @@ -258,6 +289,9 @@ pub fn canonicalize_worker_lane( stage: template.stage, group: template.group.clone(), lane_after: template.lane_after.clone(), + // Inside a worker's own list the lane is the worker's: it must + // never re-dispatch it, so ownership stays coordinator-side. + lane_owner: None, ..todo.clone() }) .collect() @@ -290,6 +324,7 @@ pub fn merge_worker_lane( item.stage = template.stage; item.group = template.group.clone(); item.lane_after = template.lane_after.clone(); + item.lane_owner = template.lane_owner.clone(); item.assigned_to = Some(link.worker.clone()); if other_ids.contains(item.id.as_str()) { item.id = format!("{}:{}", link.worker, item.id); diff --git a/crates/jcode-todo-policy/src/tests.rs b/crates/jcode-todo-policy/src/tests.rs index 9b8350abe80..b83843ebbf9 100644 --- a/crates/jcode-todo-policy/src/tests.rs +++ b/crates/jcode-todo-policy/src/tests.rs @@ -610,6 +610,7 @@ fn todo(content: &str, status: &str, group: Option<&str>) -> TodoItem { id: content.to_ascii_lowercase().replace(' ', "-"), group: group.map(str::to_string), lane_after: None, + lane_owner: None, confidence: None, completion_confidence: None, confidence_history: Vec::new(), diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs index feaac49bbef..3be8d9292f8 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_01/improvement_commands.rs @@ -64,6 +64,7 @@ fn test_improve_status_summarizes_current_todos() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -82,6 +83,7 @@ fn test_improve_status_summarizes_current_todos() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -181,6 +183,7 @@ fn test_improve_resume_uses_saved_mode_and_current_todos() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs index 6e725ebe89e..0eecb825ee0 100644 --- a/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/commands_accounts_02/part_02.rs @@ -140,6 +140,7 @@ fn test_refactor_status_summarizes_current_todos() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -158,6 +159,7 @@ fn test_refactor_status_summarizes_current_todos() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -206,6 +208,7 @@ fn test_refactor_resume_uses_saved_mode_and_current_todos() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs index 7af9b53454c..04035335daa 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_01.rs @@ -1384,6 +1384,7 @@ fn test_remote_done_auto_pokes_again_when_todos_remain() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs index 81ab60f4486..3ea79f12a80 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_01/part_02.rs @@ -85,6 +85,7 @@ fn test_remote_auto_poke_followup_preserves_visible_timer_and_stays_hidden() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -145,6 +146,7 @@ fn test_remote_auto_poke_challenges_abrupt_confidence_increase() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -216,6 +218,7 @@ fn test_remote_auto_poke_completion_below_threshold_tells_model_to_keep_working( &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -281,6 +284,7 @@ fn test_remote_poke_status_and_off_update_state() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs index cfe6d02d8dc..6ca8f6aab53 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_events_reload_02/part_01.rs @@ -10,6 +10,7 @@ fn test_remote_poke_queues_when_turn_is_in_progress() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -59,6 +60,7 @@ fn test_remote_poke_queues_when_turn_is_in_progress() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -77,6 +79,7 @@ fn test_remote_poke_queues_when_turn_is_in_progress() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -173,6 +176,7 @@ fn test_remote_interrupted_auto_poke_requeues_after_deferred_poke() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs index 758d3f3bf4f..ee5d65ac83c 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_01/part_01.rs @@ -17,6 +17,7 @@ fn test_finish_turn_does_not_duplicate_existing_poke_followup() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs index 9b54fba5e83..b171f52f64a 100644 --- a/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/remote_startup_input_02/part_01.rs @@ -1409,6 +1409,7 @@ fn test_create_transfer_session_from_parent_copies_todos_and_uses_compacted_cont &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -1597,6 +1598,7 @@ fn test_escape_interrupt_disables_auto_poke_while_processing() { .push(super::commands::build_poke_message(&[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs b/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs index 7a58d71f110..f5fc13c6c8e 100644 --- a/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs +++ b/crates/jcode-tui/src/tui/app/tests/scroll_copy_01/part_02.rs @@ -461,6 +461,7 @@ fn test_remote_escape_interrupt_disables_auto_poke_while_processing() { .push(super::commands::build_poke_message(&[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs b/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs index 98193342981..bb45ebf0d3b 100644 --- a/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs +++ b/crates/jcode-tui/src/tui/app/tests/state_model_poke_02/part_01.rs @@ -1189,6 +1189,7 @@ fn test_context_command_reports_session_context_snapshot() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs b/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs index 4b42e182ab3..217f77f3d47 100644 --- a/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs +++ b/crates/jcode-tui/src/tui/app/tests/state_model_poke_03.rs @@ -13,6 +13,7 @@ fn test_open_work_rearms_auto_poke_after_completion_gate_breaker() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -63,6 +64,7 @@ fn test_cloud_handoff_defers_turn_end_auto_poke_without_disarming_it() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2341,6 +2343,7 @@ fn test_poke_arms_auto_poke_until_todos_are_done() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2380,6 +2383,7 @@ fn test_poke_status_reports_current_state() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2440,6 +2444,7 @@ fn test_poke_off_disarms_and_clears_queued_followup() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2495,6 +2500,7 @@ fn test_poke_queues_when_turn_is_in_progress() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2537,6 +2543,7 @@ fn test_poke_queues_when_turn_is_in_progress() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2555,6 +2562,7 @@ fn test_poke_queues_when_turn_is_in_progress() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2614,6 +2622,7 @@ fn test_finish_turn_auto_pokes_again_when_todos_remain() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2653,6 +2662,7 @@ fn test_finish_turn_auto_poke_queues_confidence_summary_when_todos_done() { &[ crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2673,6 +2683,7 @@ fn test_finish_turn_auto_poke_queues_confidence_summary_when_todos_done() { }, crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2958,6 +2969,7 @@ fn test_finish_turn_without_auto_poke_does_not_queue_confidence_summary() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, @@ -2999,6 +3011,7 @@ fn test_finish_turn_auto_poke_preserves_visible_turn_started() { &app.session.id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, group: None, diff --git a/crates/jcode-tui/src/tui/app/tests/todo_card.rs b/crates/jcode-tui/src/tui/app/tests/todo_card.rs index 1d27e02f8b0..94f482d9c68 100644 --- a/crates/jcode-tui/src/tui/app/tests/todo_card.rs +++ b/crates/jcode-tui/src/tui/app/tests/todo_card.rs @@ -266,6 +266,7 @@ fn refresh_todo_card_updates_content_when_todos_change() { let todo = |content: &str, status: &str| crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "t1".to_string(), content: content.to_string(), status: status.to_string(), @@ -316,6 +317,7 @@ fn refresh_todo_card_updates_content_when_goal_scores_change() { let session_id = app.session.id.clone(); let todos = [crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "t1".to_string(), content: "render scores".to_string(), status: "in_progress".to_string(), @@ -379,6 +381,7 @@ fn refresh_todo_card_updates_content_when_goal_scores_change() { fn pinned_band_todo(id: &str, content: &str, status: &str) -> crate::todo::TodoItem { crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: id.to_string(), content: content.to_string(), status: status.to_string(), diff --git a/crates/jcode-tui/src/tui/app/todos_view.rs b/crates/jcode-tui/src/tui/app/todos_view.rs index eb9519d2a96..3696c3a0f75 100644 --- a/crates/jcode-tui/src/tui/app/todos_view.rs +++ b/crates/jcode-tui/src/tui/app/todos_view.rs @@ -953,6 +953,7 @@ mod tests { priority: priority.to_string(), group: None, lane_after: None, + lane_owner: None, confidence: confidence.map(crate::todo::ConfidenceState::from_legacy_score), completion_confidence: completion_confidence .map(crate::todo::ConfidenceState::from_legacy_score), diff --git a/crates/jcode-tui/src/tui/app/turn_notify.rs b/crates/jcode-tui/src/tui/app/turn_notify.rs index 36ba3fb8acc..1650abd0112 100644 --- a/crates/jcode-tui/src/tui/app/turn_notify.rs +++ b/crates/jcode-tui/src/tui/app/turn_notify.rs @@ -417,6 +417,7 @@ mod tests { fn todo_named(content: &str, status: &str, blocked_by: &[&str]) -> TodoItem { TodoItem { lane_after: None, + lane_owner: None, content: content.to_string(), status: status.to_string(), priority: "medium".to_string(), diff --git a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs index 05754dc4958..50d1536eb4c 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs @@ -933,6 +933,7 @@ fn load_sessions_prefers_todo_group_over_generated_title() { session_id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, content: "Wire todo title into picker".to_string(), status: "in_progress".to_string(), priority: "high".to_string(), @@ -1022,6 +1023,7 @@ fn load_sessions_keeps_custom_title_over_todo_group() { session_id, &[crate::todo::TodoItem { lane_after: None, + lane_owner: None, content: "Wire todo title into picker".to_string(), status: "in_progress".to_string(), priority: "high".to_string(), diff --git a/crates/jcode-tui/src/tui/ui_messages/tests.rs b/crates/jcode-tui/src/tui/ui_messages/tests.rs index d19dec692c7..33754e9c402 100644 --- a/crates/jcode-tui/src/tui/ui_messages/tests.rs +++ b/crates/jcode-tui/src/tui/ui_messages/tests.rs @@ -1457,6 +1457,7 @@ fn render_todos_message_shows_grouped_card_with_status_glyphs() { fn todo(id: &str, content: &str, status: &str, group: Option<&str>) -> crate::todo::TodoItem { crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: id.to_string(), content: content.to_string(), status: status.to_string(), @@ -1541,6 +1542,7 @@ fn render_todos_message_shows_grouped_card_with_status_glyphs() { fn render_todos_message_shows_goal_scores_without_verbose_feedback() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, id: "1".to_string(), @@ -1605,6 +1607,7 @@ fn render_todos_message_shows_user_intention_when_understanding_is_unclear() { let long_text = "This deliberately long assessment detail should not consume several rows in a narrow terminal window"; let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Keep the task visible".to_string(), status: "in_progress".to_string(), @@ -1692,6 +1695,7 @@ fn render_todos_message_shows_user_intention_when_understanding_is_unclear() { fn render_todos_message_uses_readable_semantic_colors() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Tune the palette".to_string(), status: "in_progress".to_string(), @@ -1882,6 +1886,7 @@ fn render_todos_message_color_codes_every_intent_state() { fn render_todos_message_collapses_passing_quality_gates() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Verify the result".to_string(), status: "completed".to_string(), @@ -1938,6 +1943,7 @@ fn render_todos_message_collapses_passing_quality_gates() { fn render_todos_message_wraps_goal_scores_at_narrow_widths() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Render the card".to_string(), status: "in_progress".to_string(), @@ -1983,6 +1989,7 @@ fn render_todos_message_wraps_goal_scores_at_narrow_widths() { fn render_todos_message_packs_multiple_goal_gates_per_row() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, id: "1".to_string(), content: "Render the card".to_string(), status: "in_progress".to_string(), @@ -2054,6 +2061,7 @@ fn render_todos_message_bad_payload_falls_back_to_system() { fn render_todo_tool_result_uses_borderless_card_with_goal_scores() { let todos = vec![crate::todo::TodoItem { lane_after: None, + lane_owner: None, stage: None, details: None, id: "render".to_string(), diff --git a/crates/jcode-tui/src/tui/ui_todo_changes.rs b/crates/jcode-tui/src/tui/ui_todo_changes.rs index 49cb740b63f..61eb4281374 100644 --- a/crates/jcode-tui/src/tui/ui_todo_changes.rs +++ b/crates/jcode-tui/src/tui/ui_todo_changes.rs @@ -505,6 +505,7 @@ mod tests { priority: "medium".to_string(), group: None, lane_after: None, + lane_owner: None, confidence: None, completion_confidence: None, confidence_history: Vec::new(), diff --git a/src/cli/commands/menubar_tests.rs b/src/cli/commands/menubar_tests.rs index 88df7eaca1f..629dc4c9da5 100644 --- a/src/cli/commands/menubar_tests.rs +++ b/src/cli/commands/menubar_tests.rs @@ -95,6 +95,7 @@ fn session_menu_item_title_loads_persisted_todo_title() { id: "menubar-label".to_string(), group: Some("Meaningful menu labels".to_string()), lane_after: None, + lane_owner: None, confidence: Some(crate::todo::ConfidenceState::Plausible), completion_confidence: None, confidence_history: Vec::new(), From ccb930f2f75a315e7ae2ae6658392df5970b3f88 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:05:46 -0800 Subject: [PATCH 038/236] Mobile push: durable history log (~/.jcode/logs/mobile-push.jsonl) with payload, source excerpt, outcome and API error body; log skips; retry transient failures once --- crates/jcode-app-core/src/mobile_push.rs | 159 ++++++++++++++++-- .../src/server/client_processing.rs | 8 +- 2 files changed, 150 insertions(+), 17 deletions(-) diff --git a/crates/jcode-app-core/src/mobile_push.rs b/crates/jcode-app-core/src/mobile_push.rs index 12a0de2fb38..65f17f258da 100644 --- a/crates/jcode-app-core/src/mobile_push.rs +++ b/crates/jcode-app-core/src/mobile_push.rs @@ -498,7 +498,19 @@ pub fn notify( body: &str, turn_id: Option, ) { - if cfg!(test) || !enabled() { + if cfg!(test) { + return; + } + let requested = kind; + if !enabled() { + record( + requested, + None, + body, + "skipped", + Some("disabled by config or JCODE_MOBILE_PUSH"), + None, + ); return; } if SUPPRESSION @@ -507,18 +519,113 @@ pub fn notify( .unwrap_or(false) { crate::logging::debug("mobile push suppressed: no registered devices"); + record( + requested, + None, + body, + "skipped", + Some("no registered devices (10 min back-off)"), + None, + ); return; } let Ok(handle) = tokio::runtime::Handle::try_current() else { + record( + requested, + None, + body, + "skipped", + Some("no tokio runtime"), + None, + ); return; }; let payload = build_payload(kind, session_id, session_title, body, turn_id); + let source = body.to_string(); handle.spawn(async move { let Some((key, base)) = credentials() else { + record( + requested, + Some(&payload), + &source, + "skipped", + Some("not signed in (no JCODE_API_KEY)"), + None, + ); return; }; - send(&key, &base, &payload).await; + let mut attempt = send(&key, &base, &payload).await; + // Transient network failures (DNS, reset, timeout) get one retry. + if attempt.0 == "error" && attempt.2.is_none() { + tokio::time::sleep(Duration::from_secs(3)).await; + attempt = send(&key, &base, &payload).await; + if let Some(detail) = attempt.1.as_mut() { + detail.push_str(" (after retry)"); + } + } + let (outcome, detail, status) = attempt; + record( + requested, + Some(&payload), + &source, + outcome, + detail.as_deref(), + status, + ); + }); +} + +/// Record a turn that deliberately did not push, so the history explains +/// missing notifications too. +pub fn note_skipped(kind: PushKind, reason: &str) { + if !cfg!(test) { + record(kind, None, "", "skipped", Some(reason), None); + } +} + +/// Append one line per push attempt to `~/.jcode/logs/mobile-push.jsonl`: +/// what was requested, the exact payload sent, a source excerpt, and the +/// outcome. This is the push history used to debug notification quality. +fn record( + requested: PushKind, + payload: Option<&PushPayload>, + source: &str, + outcome: &str, + detail: Option<&str>, + http_status: Option, +) { + use std::io::Write; + let line = serde_json::json!({ + "ts": chrono::Utc::now().to_rfc3339(), + "requested_kind": requested, + "outcome": outcome, + "detail": detail, + "http_status": http_status, + "payload": payload, + "source_chars": source.chars().count(), + "source_excerpt": truncate_chars(source, 1500), }); + crate::logging::info(&format!( + "mobile push {:?} {outcome}{}", + payload.map(|p| p.kind).unwrap_or(requested), + detail.map(|d| format!(": {d}")).unwrap_or_default() + )); + let Some(dir) = dirs::home_dir().map(|h| h.join(".jcode").join("logs")) else { + return; + }; + let _ = std::fs::create_dir_all(&dir); + let path = dir.join("mobile-push.jsonl"); + // Keep the history bounded: rotate once past 5 MB. + if std::fs::metadata(&path).is_ok_and(|m| m.len() > 5 * 1024 * 1024) { + let _ = std::fs::rename(&path, dir.join("mobile-push.1.jsonl")); + } + if let Ok(mut file) = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&path) + { + let _ = writeln!(file, "{line}"); + } } #[derive(serde::Deserialize)] @@ -575,12 +682,15 @@ async fn send_mobile_resume( } } -async fn send(key: &str, base: &str, payload: &PushPayload) { +async fn send( + key: &str, + base: &str, + payload: &PushPayload, +) -> (&'static str, Option, Option) { let client = match reqwest::Client::builder().timeout(SEND_TIMEOUT).build() { Ok(c) => c, Err(error) => { - crate::logging::warn(&format!("mobile push client build failed: {error}")); - return; + return ("error", Some(format!("client build failed: {error}")), None); } }; let result = client @@ -591,28 +701,45 @@ async fn send(key: &str, base: &str, payload: &PushPayload) { .await; match result { Ok(resp) if resp.status().is_success() => { + let status = resp.status().as_u16(); let reply = match resp.json::().await { Ok(reply) => reply, Err(error) => { - crate::logging::warn(&format!("invalid mobile push reply: {error}")); - return; + return ( + "error", + Some(format!("invalid reply: {error}")), + Some(status), + ); } }; let devices = reply.devices; if let Ok(mut s) = SUPPRESSION.lock() { s.record_reply(devices, Instant::now()); } - crate::logging::debug(&format!( - "mobile push {:?} session={} sent={:?} devices={:?}", - payload.kind, payload.session_id, reply.sent, devices + let detail = format!("sent={:?} devices={:?}", reply.sent, devices); + let outcome = match (reply.sent, devices) { + (_, Some(0)) => "no_devices", + (Some(n), _) if n > 0 => "delivered", + _ => "undelivered", + }; + (outcome, Some(detail), Some(status)) + } + Ok(resp) => { + let status = resp.status().as_u16(); + // Keep the API's error body: it says exactly which field was rejected. + let body = resp.text().await.unwrap_or_default(); + crate::logging::warn(&format!( + "mobile push {:?} rejected: HTTP {status}: {}", + payload.kind, + truncate_chars(&body, 300) )); + ("rejected", Some(truncate_chars(&body, 500)), Some(status)) + } + Err(error) => { + let detail = crate::util::format_error_chain(&anyhow::Error::from(error)); + crate::logging::warn(&format!("mobile push failed: {detail}")); + ("error", Some(detail), None) } - Ok(resp) => crate::logging::warn(&format!( - "mobile push {:?} rejected: HTTP {}", - payload.kind, - resp.status() - )), - Err(error) => crate::logging::warn(&format!("mobile push failed: {error}")), } } diff --git a/crates/jcode-app-core/src/server/client_processing.rs b/crates/jcode-app-core/src/server/client_processing.rs index d4fe3e09339..c5658261343 100644 --- a/crates/jcode-app-core/src/server/client_processing.rs +++ b/crates/jcode-app-core/src/server/client_processing.rs @@ -368,7 +368,13 @@ pub(super) async fn start_processing_message( Some(id), ); } - Ok(()) => {} + Ok(()) => crate::mobile_push::note_skipped( + crate::mobile_push::PushKind::TurnDone, + &format!( + "turn too short ({}s) for a non-iOS session", + turn_started.elapsed().as_secs() + ), + ), Err(error) => crate::mobile_push::notify( crate::mobile_push::PushKind::TurnError, &push_session_id, From 83192304c113946c7b3306b6cdadb632af47be09 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:07:47 -0800 Subject: [PATCH 039/236] tui: show time-since-last and token count on swarm messages, icon expand badge --- crates/jcode-tui/src/tui/app.rs | 3 ++ .../src/tui/app/remote/server_events.rs | 13 +++-- .../src/tui/app/tests/oauth_login_progress.rs | 6 +-- .../src/tui/app/tests/scroll_copy_03.rs | 8 +-- crates/jcode-tui/src/tui/app/tui_lifecycle.rs | 2 + crates/jcode-tui/src/tui/ui_messages.rs | 50 +++++++++++++++++-- 6 files changed, 66 insertions(+), 16 deletions(-) diff --git a/crates/jcode-tui/src/tui/app.rs b/crates/jcode-tui/src/tui/app.rs index d0eb708715c..b47f61ff8cd 100644 --- a/crates/jcode-tui/src/tui/app.rs +++ b/crates/jcode-tui/src/tui/app.rs @@ -1424,6 +1424,9 @@ pub struct App { /// When `remote_swarm_members` last arrived, so worker stopwatches can /// tick locally between server snapshots. remote_swarm_members_received_at: Option, + /// When each swarm agent last sent us a notification, keyed by sender, so + /// notifications can show time since that agent's previous message. + swarm_last_message_at: std::collections::HashMap, // Latest swarm plan snapshot (local or remote server event stream) swarm_plan_items: Vec, swarm_plan_version: Option, diff --git a/crates/jcode-tui/src/tui/app/remote/server_events.rs b/crates/jcode-tui/src/tui/app/remote/server_events.rs index 83979b9ce7e..e6b2d4da006 100644 --- a/crates/jcode-tui/src/tui/app/remote/server_events.rs +++ b/crates/jcode-tui/src/tui/app/remote/server_events.rs @@ -3221,10 +3221,15 @@ fn handle_server_event_inner( )); return false; } - app.push_display_message(DisplayMessage::swarm( - presentation.title.clone(), - presentation.message.clone(), - )); + let now = std::time::Instant::now(); + let since_last = app + .swarm_last_message_at + .insert(sender.clone(), now) + .map(|prev| now.duration_since(prev).as_secs_f32()); + let mut swarm_message = + DisplayMessage::swarm(presentation.title.clone(), presentation.message.clone()); + swarm_message.duration_secs = since_last; + app.push_display_message(swarm_message); persist_replay_display_message( app, "swarm", diff --git a/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs b/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs index 12bcc9ec75e..82480fa2023 100644 --- a/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs +++ b/crates/jcode-tui/src/tui/app/tests/oauth_login_progress.rs @@ -214,19 +214,19 @@ fn oauth_login_progress_real_frame_has_clickable_link_and_expand_button() { crate::tui::ui::oauth_login_link_message_from_screen(link_col + 2, link_row), Some(index) ); - let (col, row) = find(&terminal, "▸ expand"); + let (col, row) = find(&terminal, "⊞"); assert!(app.try_toggle_swarm_expand_at(col + 2, row)); let expanded = render_and_snap(&app, &mut terminal); assert!(expanded.contains("client_id=jcode-fixture")); println!("Observed expanded login frame:\n{expanded}"); - let (col, row) = find(&terminal, "▾ collapse"); + let (col, row) = find(&terminal, "⊟"); assert!(app.try_toggle_swarm_expand_at(col + 2, row)); let collapsed = render_and_snap(&app, &mut terminal); assert!(!collapsed.contains("client_id=jcode-fixture")); app.handle_debug_command("login-progress-success-fixture"); let success = render_and_snap(&app, &mut terminal); assert!(success.contains("OpenAI login complete")); - assert!(!success.contains("▸ expand")); + assert!(!success.contains("⊞")); assert!(!success.contains("Open OpenAI sign-in")); assert!(!success.contains("Login only:")); println!("Observed successful login frame:\n{success}"); diff --git a/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs b/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs index e639beefa6e..6073007852a 100644 --- a/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs +++ b/crates/jcode-tui/src/tui/app/tests/scroll_copy_03.rs @@ -1505,7 +1505,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { "collapsed card must show the tldr:\n{collapsed}" ); assert!( - collapsed.contains("▸ expand"), + collapsed.contains("⊞"), "collapsed card must show the expand badge:\n{collapsed}" ); assert!( @@ -1522,7 +1522,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { for col in 0..area.width { line.push_str(buf[(col, row)].symbol()); } - if let Some(byte) = line.find("▸ expand") { + if let Some(byte) = line.find("⊞") { let col = line[..byte].chars().count() as u16; badge = Some((col, row)); break 'rows; @@ -1566,7 +1566,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { "expanded card must show the body:\n{expanded}" ); assert!( - expanded.contains("▾ collapse"), + expanded.contains("⊟"), "expanded card must show the collapse badge:\n{expanded}" ); @@ -1579,7 +1579,7 @@ fn test_click_on_swarm_expand_badge_toggles_tldr_collapse() { for col in 0..area.width { line.push_str(buf[(col, row)].symbol()); } - if let Some(byte) = line.find("▾ collapse") { + if let Some(byte) = line.find("⊟") { let col = line[..byte].chars().count() as u16; collapse_badge = Some((col, row)); break 'rows2; diff --git a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs index db4b95edc71..32afdb5c30e 100644 --- a/crates/jcode-tui/src/tui/app/tui_lifecycle.rs +++ b/crates/jcode-tui/src/tui/app/tui_lifecycle.rs @@ -525,6 +525,7 @@ impl App { side_pane_images_signature_cache: std::cell::Cell::new(None), remote_swarm_members: Vec::new(), remote_swarm_members_received_at: None, + swarm_last_message_at: std::collections::HashMap::new(), swarm_plan_items: Vec::new(), swarm_plan_version: None, swarm_plan_swarm_id: None, @@ -1030,6 +1031,7 @@ impl App { side_pane_images_signature_cache: std::cell::Cell::new(None), remote_swarm_members: Vec::new(), remote_swarm_members_received_at: None, + swarm_last_message_at: std::collections::HashMap::new(), swarm_plan_items: Vec::new(), swarm_plan_version: None, swarm_plan_swarm_id: None, diff --git a/crates/jcode-tui/src/tui/ui_messages.rs b/crates/jcode-tui/src/tui/ui_messages.rs index 27f65ccc2ac..1a80b1b2184 100644 --- a/crates/jcode-tui/src/tui/ui_messages.rs +++ b/crates/jcode-tui/src/tui/ui_messages.rs @@ -3139,8 +3139,8 @@ fn swarm_notification_style(title: Option<&str>) -> (&'static str, Color, Color) /// Trailing badge text appended to a collapsed swarm tldr line. Kept as /// constants so click hit-testing and rendering stay in sync. -pub(crate) const SWARM_EXPAND_BADGE: &str = "▸ expand"; -pub(crate) const SWARM_COLLAPSE_BADGE: &str = "▾ collapse"; +pub(crate) const SWARM_EXPAND_BADGE: &str = "⊞"; +pub(crate) const SWARM_COLLAPSE_BADGE: &str = "⊟"; pub(crate) const SWARM_DIFF_EXPAND_BADGE: &str = "▸ diff"; pub(crate) const SWARM_DIFF_COLLAPSE_BADGE: &str = "▾ hide"; pub(crate) const SWARM_AGENT_SNAPSHOT_TITLE: &str = "swarm-agent-snapshot"; @@ -3254,6 +3254,7 @@ fn compact_swarm_notification(title: &str) -> Option, width: u16, ) -> Option>> { let notification = compact_swarm_notification(title)?; @@ -3321,9 +3322,20 @@ fn render_compact_agent_notification( }; // Keep the full click target only when it fits. At very narrow widths // the preview wins rather than letting a badge force a second row. + // Short plain notes stay clean: usage only appears on collapsible + // messages or when the time since this agent's last message is known. + let usage_text = (badge.is_some() || since_last_secs.is_some()) + .then(|| swarm_notification_usage_label(content, since_last_secs)) + .flatten() + .map(|usage| format!(" {usage}")) + .filter(|usage| usage.width() + 8 < max_width); + let usage_width = usage_text + .as_deref() + .map(UnicodeWidthStr::width) + .unwrap_or(0); let badge_text = badge .map(|badge| format!(" {badge}")) - .filter(|badge| badge.width() < max_width); + .filter(|badge| badge.width() + usage_width < max_width); let badge_width = badge_text .as_deref() .map(UnicodeWidthStr::width) @@ -3331,8 +3343,12 @@ fn render_compact_agent_notification( prefix.push(Span::styled(preview, body_style)); let mut line = super::truncate_line_with_ellipsis_to_width( &Line::from(prefix), - max_width.saturating_sub(badge_width).max(1), + max_width.saturating_sub(badge_width + usage_width).max(1), ); + if let Some(usage_text) = usage_text { + line.spans + .push(Span::styled(usage_text, Style::default().fg(dim_color()))); + } if let Some(badge_text) = badge_text { line.spans.push(Span::styled( badge_text, @@ -3396,6 +3412,28 @@ fn render_compact_agent_notification( Some(lines) } +/// "time since this agent's previous message, approx tokens" for a swarm +/// notification. Time is omitted when unknown (first message, replay). +fn swarm_notification_usage_label(content: &str, since_last_secs: Option) -> Option { + let body = jcode_tui_messages::parse_collapsible_swarm_content(content) + .map(|parsed| format!("{} {}", parsed.tldr, parsed.body)) + .unwrap_or_else(|| content.to_string()); + let tokens = crate::util::estimate_tokens(body.trim()); + if tokens == 0 { + return None; + } + let tokens = crate::util::format_approx_token_count(tokens); + Some( + match since_last_secs.filter(|secs| secs.is_finite() && *secs >= 0.0) { + Some(secs) => format!( + "{}, {tokens}", + crate::message::Message::format_duration((secs as f64 * 1000.0).round() as u64) + ), + None => tokens, + }, + ) +} + fn render_compact_swarm_await( title: &str, content: &str, @@ -3490,7 +3528,9 @@ pub(crate) fn render_swarm_message( let centered = markdown::center_code_blocks(); let title = msg.title.as_deref().unwrap_or("Swarm").trim(); - if let Some(lines) = render_compact_agent_notification(title, &msg.content, width) { + if let Some(lines) = + render_compact_agent_notification(title, &msg.content, msg.duration_secs, width) + { return lines; } if let Some(lines) = render_compact_swarm_await(title, &msg.content, width) { From a46d055fce69cdbf20ad63694891fcc6b1a1dd77 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:08:08 -0800 Subject: [PATCH 040/236] Optimize mobile sync unchanged reconciliation and no-op commits --- crates/jcode-base/src/mobile_sync.rs | 58 ++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index c2010af9beb..66905dbd07f 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -259,7 +259,11 @@ pub fn save_state(state: &MobileSyncState) -> Result<()> { storage::write_json_secret(&state_path()?, &next) } -fn merge_progress(state: &mut MobileSyncState, updates: &BTreeMap) { +fn merge_progress( + state: &mut MobileSyncState, + updates: &BTreeMap, +) -> bool { + let mut changed = false; for (id, progress) in updates { let version = |p: &SessionProgress| { p.revision @@ -267,20 +271,27 @@ fn merge_progress(state: &mut MobileSyncState, updates: &BTreeMap) -> Result<()> { + if updates.is_empty() { + return Ok(()); + } let _lock = state_lock()?; let mut current = load_state(); if current.enabled && current.generation == generation { - merge_progress(&mut current, updates); - storage::write_json_secret(&state_path()?, ¤t)?; + if merge_progress(&mut current, updates) { + storage::write_json_secret(&state_path()?, ¤t)?; + } } Ok(()) } @@ -552,7 +563,7 @@ async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result Result Result s, Err(_) => { @@ -616,7 +632,7 @@ async fn sync_pass(client: &reqwest::Client, reconcile: bool) -> Result Result Result Date: Sun, 8 Nov 2026 00:09:31 -0800 Subject: [PATCH 041/236] Test mobile sync no-op writes and in-flight generation fencing --- crates/jcode-base/src/mobile_sync_tests.rs | 169 +++++++++++++++++++-- 1 file changed, 160 insertions(+), 9 deletions(-) diff --git a/crates/jcode-base/src/mobile_sync_tests.rs b/crates/jcode-base/src/mobile_sync_tests.rs index 1941831bfa2..9f934710768 100644 --- a/crates/jcode-base/src/mobile_sync_tests.rs +++ b/crates/jcode-base/src/mobile_sync_tests.rs @@ -8,24 +8,33 @@ async fn benchmark_unchanged_reconciliation() { let home = tempfile::tempdir().unwrap(); let _env = TestEnv::set(&[ ("JCODE_HOME", home.path().as_os_str()), - ("JCODE_API_KEY", std::ffi::OsStr::new("benchmark-no-network")), + ( + "JCODE_API_KEY", + std::ffi::OsStr::new("benchmark-no-network"), + ), ("JCODE_API_BASE", std::ffi::OsStr::new("http://127.0.0.1:1")), ]); DIRTY_SESSIONS.lock().unwrap().clear(); let dir = home.path().join("sessions"); std::fs::create_dir_all(&dir).unwrap(); - let mut state = MobileSyncState { enabled: true, ..Default::default() }; + let mut state = MobileSyncState { + enabled: true, + ..Default::default() + }; for index in 0..1300 { let id = format!("benchmark-{index:04}"); let path = dir.join(format!("{id}.json")); // An unchanged revision must never need to deserialize a transcript. std::fs::write(&path, b"{}").unwrap(); - state.sessions.insert(id, SessionProgress { - uploaded: 100, - mtime_ms: session_mtime_ms(&path), - revision: Some(session_revision(&path)), - fingerprint: Some(42), - }); + state.sessions.insert( + id, + SessionProgress { + uploaded: 100, + mtime_ms: session_mtime_ms(&path), + revision: Some(session_revision(&path)), + fingerprint: Some(42), + }, + ); } save_state(&state).unwrap(); // Enabling increments the fence, so seed progress in the current generation. @@ -39,7 +48,10 @@ async fn benchmark_unchanged_reconciliation() { elapsed.push(started.elapsed().as_micros()); } elapsed.sort_unstable(); - eprintln!("unchanged reconciliation: candidates=1300 passes=7 median_us={} max_us={} samples_us={elapsed:?}", elapsed[3], elapsed[6]); + eprintln!( + "unchanged reconciliation: candidates=1300 passes=7 median_us={} max_us={} samples_us={elapsed:?}", + elapsed[3], elapsed[6] + ); } #[test] @@ -87,6 +99,145 @@ async fn upload_failure_does_not_retain_private_server_response() { struct TestEnv(Vec<(&'static str, Option)>); +#[tokio::test] +async fn unchanged_pass_and_identical_commit_do_not_rewrite_state() { + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("no-network")), + ("JCODE_API_BASE", std::ffi::OsStr::new("http://127.0.0.1:1")), + ]); + DIRTY_SESSIONS.lock().unwrap().clear(); + let path = home.path().join("sessions/unchanged.json"); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, b"not a transcript").unwrap(); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let generation = load_state().generation; + let updates = BTreeMap::from([( + "unchanged".into(), + SessionProgress { + revision: Some(session_revision(&path)), + ..Default::default() + }, + )]); + commit_progress(generation, &updates).unwrap(); + let state = state_path().unwrap(); + std::fs::File::open(&state) + .unwrap() + .set_times(std::fs::FileTimes::new().set_modified(UNIX_EPOCH + Duration::from_secs(100))) + .unwrap(); + let before = session_revision(&state); + commit_progress(generation, &updates).unwrap(); + assert_eq!( + session_revision(&state), + before, + "identical merge must not write" + ); + std::fs::remove_file(home.path().join("mobile_sync.lock")).unwrap(); + for reconcile in [true, false] { + assert_eq!( + sync_pass(&reqwest::Client::new(), reconcile).await.unwrap(), + SyncReport::default() + ); + assert_eq!( + session_revision(&state), + before, + "no-op pass must not write" + ); + assert!( + !home.path().join("mobile_sync.lock").exists(), + "no-op pass must not acquire cross-process lock" + ); + } +} + +#[tokio::test] +async fn disable_reenable_during_upload_fences_remaining_chunks_and_progress() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let _lock = storage::lock_test_env(); + let home = tempfile::tempdir().unwrap(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let _env = TestEnv::set(&[ + ("JCODE_HOME", home.path().as_os_str()), + ("JCODE_API_KEY", std::ffi::OsStr::new("test-key")), + ("JCODE_API_BASE", std::ffi::OsStr::new(&base)), + ]); + DIRTY_SESSIONS.lock().unwrap().clear(); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + let mut session = Session::create_with_id("generation-cycle".into(), None, None); + session.messages = (0..MAX_MESSAGES_PER_UPLOAD + 1) + .map(|index| { + let mut message = msg( + Role::User, + vec![ContentBlock::Text { + text: "visible".into(), + cache_control: None, + }], + ); + message.id = format!("message-{index}"); + message + }) + .collect(); + session.save().unwrap(); + let server = tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0; 8192]; + loop { + let count = socket.read(&mut buffer).await.unwrap(); + assert_ne!(count, 0); + bytes.extend_from_slice(&buffer[..count]); + if let Some(start) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { + let header = String::from_utf8_lossy(&bytes[..start]); + let length: usize = header + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length:") + .map(|n| n.trim().parse().unwrap()) + }) + .unwrap(); + if bytes.len() >= start + 4 + length { + break; + } + } + } + save_state(&MobileSyncState::default()).unwrap(); + save_state(&MobileSyncState { + enabled: true, + ..Default::default() + }) + .unwrap(); + socket + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}") + .await + .unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(250), listener.accept()) + .await + .is_err(), + "old generation must not send its second chunk" + ); + }); + let report = sync_once(&reqwest::Client::new()).await.unwrap(); + assert_eq!(report.messages_uploaded, MAX_MESSAGES_PER_UPLOAD); + assert_eq!(report.sessions_uploaded, 0); + server.await.unwrap(); + assert!(load_state().enabled); + assert!(load_state().sessions.is_empty()); + DIRTY_SESSIONS.lock().unwrap().clear(); +} + impl TestEnv { fn set(values: &[(&'static str, &std::ffi::OsStr)]) -> Self { Self( From bbddd3d45bb4c051a3017e73b4372aae2acc243d Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:11:35 -0800 Subject: [PATCH 042/236] Collapse mobile sync progress commit guard for strict Clippy --- crates/jcode-base/src/mobile_sync.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index 66905dbd07f..b0c3a0c6863 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -288,10 +288,8 @@ fn commit_progress(generation: u64, updates: &BTreeMap) } let _lock = state_lock()?; let mut current = load_state(); - if current.enabled && current.generation == generation { - if merge_progress(&mut current, updates) { - storage::write_json_secret(&state_path()?, ¤t)?; - } + if current.enabled && current.generation == generation && merge_progress(&mut current, updates) { + storage::write_json_secret(&state_path()?, ¤t)?; } Ok(()) } From c7b896f3c5b3fb569ab0522e6386a4f1d792184e Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:13:17 -0800 Subject: [PATCH 043/236] Keep cloud latency correlations scoped to their connection --- crates/jcode-tui/src/tui/backend.rs | 14 +- crates/jcode-tui/src/tui/cloud_latency.rs | 181 +++++++++++++----- .../jcode-tui/src/tui/cloud_latency_tests.rs | 138 +++++++++++-- docs/CLOUD_LATENCY.md | 23 ++- 4 files changed, 281 insertions(+), 75 deletions(-) diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index 2e1a7ba888a..8c66c7a0a7b 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -252,9 +252,9 @@ pub struct RemoteConnection { client_instance_id: Option, next_request_id: u64, pending_runtime_commands: std::collections::HashSet, - // Bootstrap Done acknowledgments are not completions of a detached turn. - // Retain recent ids because target Subscribe can acknowledge twice. + // Bootstrap Done is not turn completion. Retain ids for duplicate acks. control_done_ids: std::sync::Mutex>, + request_timings: super::cloud_latency::RequestTimings, tool_diff: RemoteDiffTracker, /// Bytes pulled from the socket that have not yet been split into complete /// newline-delimited protocol lines. This buffer is persistent across @@ -421,12 +421,11 @@ impl RemoteConnection { cloud_authority: None, cloud_callbacks: Vec::new(), client_instance_id: client_instance_id.map(str::to_string), - // A reattached turn carries the old connection's request id on - // local sockets as well as SSH. Keep its Done distinct from this - // connection's Subscribe/GetHistory acknowledgments. + // Keep reattached turn Done distinct from bootstrap acknowledgments. next_request_id: (rand::random::() & ((1_u64 << 62) - 1)) | (1_u64 << 62), pending_runtime_commands: Default::default(), control_done_ids: Default::default(), + request_timings: Default::default(), tool_diff: RemoteDiffTracker::default(), read_buffer: Vec::new(), read_buffer_scan_start: 0, @@ -639,7 +638,7 @@ impl RemoteConnection { timing.finish(if result.is_ok() { "ok" } else { "error" }); } result?; - super::cloud_latency::request_sent(&request); + self.request_timings.request_sent(&request); Ok(()) } @@ -1449,7 +1448,7 @@ impl RemoteConnection { } return LineOutcome::Skip; } - match super::cloud_latency::parse_event(&text) { + match self.request_timings.parse_event(&text) { Ok(ServerEvent::Done { id }) if self .control_done_ids @@ -1517,6 +1516,7 @@ impl RemoteConnection { next_request_id: 1, pending_runtime_commands: Default::default(), control_done_ids: Default::default(), + request_timings: Default::default(), tool_diff: RemoteDiffTracker::default(), read_buffer: Vec::new(), read_buffer_scan_start: 0, diff --git a/crates/jcode-tui/src/tui/cloud_latency.rs b/crates/jcode-tui/src/tui/cloud_latency.rs index aa0745a9643..64746bfe8ae 100644 --- a/crates/jcode-tui/src/tui/cloud_latency.rs +++ b/crates/jcode-tui/src/tui/cloud_latency.rs @@ -2,6 +2,7 @@ use crate::protocol::{Request, ServerEvent}; use std::cell::RefCell; use std::collections::VecDeque; +use std::sync::Mutex; use std::time::Instant; #[cfg(test)] @@ -10,10 +11,23 @@ mod tests; const MAX_PENDING: usize = 64; thread_local! { - static REQUESTS: RefCell> = const { RefCell::new(VecDeque::new()) }; static PAINT: RefCell> = const { RefCell::new(None) }; } +// Async connection futures can migrate between executor threads. Keep request +// correlation on the connection, not the thread or a process-global request map. +#[derive(Default)] +pub(super) struct RequestTimings { + pending: Mutex>, +} +struct PendingRequest { + id: u64, + operation: &'static str, + started: Instant, + history_expected: bool, + todos_seen: bool, +} + pub(super) struct Span { stage: &'static str, started: Instant, @@ -71,23 +85,125 @@ pub(super) fn begin_request(request: &Request) -> Option { }) } -pub(super) fn request_sent(request: &Request) { - let Some((id, kind)) = request_key(request) else { - return; - }; - REQUESTS.with_borrow_mut(|pending| { - pending.retain(|(previous, operation, at)| { - if at.elapsed().as_secs() >= 120 { - crate::logging::info(&format!("[CLOUD_TIMING] stage=response_wait operation={operation} request_id={previous} outcome=expired duration_ms={:.3}", at.elapsed().as_secs_f64() * 1000.0)); +impl RequestTimings { + pub(super) fn request_sent(&self, request: &Request) { + let Some((id, kind)) = request_key(request) else { + return; + }; + let mut pending = self + .pending + .lock() + .unwrap_or_else(|error| error.into_inner()); + pending.retain(|previous| { + if previous.started.elapsed().as_secs() >= 120 { + crate::logging::info(&format!("[CLOUD_TIMING] stage=response_wait operation={} request_id={} outcome=expired duration_ms={:.3}", previous.operation, previous.id, previous.started.elapsed().as_secs_f64() * 1000.0)); false - } else { *previous != id } + } else { previous.id != id } }); - if pending.len() == MAX_PENDING { pending.pop_front(); } - pending.push_back((id, kind, Instant::now())); - }); - crate::logging::info(&format!( - "[CLOUD_TIMING] stage=request_sent operation={kind} request_id={id} outcome=ok duration_ms=0" - )); + if pending.len() == MAX_PENDING { + pending.pop_front(); + } + pending.push_back(PendingRequest { + id, + operation: kind, + started: Instant::now(), + // Fresh Subscribe completes with Done, followed by a separately + // requested History. A target-aware attach returns History itself. + history_expected: !matches!( + request, + Request::Subscribe { + target_session_id: None, + .. + } + ), + todos_seen: false, + }); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=request_sent operation={kind} request_id={id} outcome=ok duration_ms=0" + )); + } + + fn response_wait(&self, event: &ServerEvent) -> Option<(&'static str, u64, f64)> { + if !matches!( + event, + ServerEvent::Todos { .. } + | ServerEvent::History { .. } + | ServerEvent::CompactedHistory { .. } + | ServerEvent::Error { .. } + | ServerEvent::Done { .. } + ) { + return None; + } + let mut pending = self + .pending + .lock() + .unwrap_or_else(|error| error.into_inner()); + let index = match event { + ServerEvent::Todos { .. } => { + // Explicit polls take priority over an attach's early snapshot. + // A bootstrap snapshot must not consume History correlation or + // keep matching every unsolicited snapshot for that connection. + pending + .iter() + .position(|request| request.operation == "todos") + .or_else(|| { + pending.iter().position(|request| { + !request.todos_seen + && matches!(request.operation, "subscribe" | "resume") + }) + })? + } + ServerEvent::History { id, .. } + | ServerEvent::CompactedHistory { id, .. } + | ServerEvent::Error { id, .. } => { + pending.iter().position(|request| request.id == *id)? + } + ServerEvent::Done { id } => pending.iter().position(|request| { + request.id == *id && request.operation == "subscribe" && !request.history_expected + })?, + _ => return None, + }; + let request = &mut pending[index]; + let result = ( + request.operation, + request.id, + request.started.elapsed().as_secs_f64() * 1000.0, + ); + if matches!(event, ServerEvent::Todos { .. }) && request.operation != "todos" { + request.todos_seen = true; + } else { + pending.remove(index); + } + Some(result) + } + + pub(super) fn parse_event(&self, text: &str) -> serde_json::Result { + let started = Instant::now(); + let result = serde_json::from_str(text); + let (key, wait) = match &result { + Ok(event) => (event_key(event), self.response_wait(event)), + Err(_) => (None, None), + }; + if key.is_some() + || wait.is_some() + || result.is_err() + || started.elapsed().as_millis() >= 100 + { + let (kind, id) = key + .or_else(|| wait.map(|(operation, id, _)| (operation, Some(id)))) + .unwrap_or(("other", None)); + let failed = result.is_err() || matches!(&result, Ok(ServerEvent::Error { .. })); + crate::logging::info(&format!( + "[CLOUD_TIMING] stage=event_parse operation={kind} request_id={id:?} outcome={} duration_ms={:.3} bytes={} request_wait_ms={}", + if failed { "error" } else { "ok" }, + started.elapsed().as_secs_f64() * 1000.0, + text.len(), + wait.map(|(_, _, ms)| format!("{ms:.3}")) + .unwrap_or_else(|| "none".into()) + )); + } + result + } } pub(super) fn event_key(event: &ServerEvent) -> Option<(&'static str, Option)> { @@ -99,39 +215,6 @@ pub(super) fn event_key(event: &ServerEvent) -> Option<(&'static str, Option serde_json::Result { - let started = Instant::now(); - let result = serde_json::from_str(text); - let key = match &result { - Ok(event) => event_key(event), - Err(_) => None, - }; - if key.is_some() || result.is_err() || started.elapsed().as_millis() >= 100 { - let (kind, id) = key.unwrap_or(("other", None)); - let wait = REQUESTS.with_borrow_mut(|pending| { - let index = pending.iter().position(|(request, operation, _)| { - id == Some(*request) - || (kind == "todos" && matches!(*operation, "todos" | "subscribe" | "resume")) - })?; - // Early Todos must not consume the History correlation for Subscribe. - let elapsed = pending[index].2.elapsed(); - if kind != "todos" || pending[index].1 == "todos" { - pending.remove(index); - } - Some(elapsed.as_secs_f64() * 1000.0) - }); - crate::logging::info(&format!( - "[CLOUD_TIMING] stage=event_parse operation={kind} request_id={id:?} outcome={} duration_ms={:.3} bytes={} request_wait_ms={}", - if result.is_ok() { "ok" } else { "error" }, - started.elapsed().as_secs_f64() * 1000.0, - text.len(), - wait.map(|ms| format!("{ms:.3}")) - .unwrap_or_else(|| "none".into()) - )); - } - result -} - pub(super) fn applied(kind: &'static str, started: Instant, redraw: bool) { crate::logging::info(&format!( "[CLOUD_TIMING] stage=event_apply operation={kind} outcome=ok duration_ms={:.3} redraw={redraw}", diff --git a/crates/jcode-tui/src/tui/cloud_latency_tests.rs b/crates/jcode-tui/src/tui/cloud_latency_tests.rs index 4d5e1bc09be..bbe6fbf047f 100644 --- a/crates/jcode-tui/src/tui/cloud_latency_tests.rs +++ b/crates/jcode-tui/src/tui/cloud_latency_tests.rs @@ -1,35 +1,140 @@ use super::*; +fn subscribe(id: u64, target: Option<&str>) -> Request { + serde_json::from_value(serde_json::json!({ + "type": "subscribe", "id": id, "target_session_id": target, + })) + .unwrap() +} + +fn todo_event() -> ServerEvent { + ServerEvent::Todos { + session_id: "fixture".into(), + payload: "private transcript must not be logged".into(), + } +} + #[test] fn cloud_latency_pending_requests_are_bounded_and_responses_remove_them() { - REQUESTS.with_borrow_mut(VecDeque::clear); + let timings = RequestTimings::default(); for id in 0..100 { - request_sent(&Request::GetHistory { id }); + timings.request_sent(&Request::GetHistory { id }); } - REQUESTS.with_borrow(|pending| { + { + let pending = timings.pending.lock().unwrap(); assert_eq!(pending.len(), MAX_PENDING); - assert_eq!(pending.front().map(|entry| entry.0), Some(36)); - }); - let event = parse_event(r#"{"type":"compacted_history","id":99,"session_id":"fixture","messages":[],"images":[],"compacted_total":0,"compacted_visible":0,"compacted_remaining":0,"compacted_hidden_prompts":0}"#).unwrap(); + assert_eq!(pending.front().map(|entry| entry.id), Some(36)); + } + let event = timings.parse_event(r#"{"type":"compacted_history","id":99,"session_id":"fixture","messages":[],"images":[],"compacted_total":0,"compacted_visible":0,"compacted_remaining":0,"compacted_hidden_prompts":0}"#).unwrap(); assert_eq!(event_key(&event), Some(("history_page", Some(99)))); - REQUESTS.with_borrow(|pending| assert!(!pending.iter().any(|entry| entry.0 == 99))); - REQUESTS.with_borrow_mut(VecDeque::clear); + assert!( + !timings + .pending + .lock() + .unwrap() + .iter() + .any(|entry| entry.id == 99) + ); } #[test] fn cloud_latency_todos_preserve_attach_correlation_and_payload() { - REQUESTS.with_borrow_mut(|pending| { - pending.clear(); - pending.push_back((42, "subscribe", Instant::now())); - }); - let event = parse_event(r#"{"type":"todos","session_id":"fixture","payload":"private transcript must not be logged"}"#).unwrap(); + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(42, Some("fixture"))); + let event = timings.parse_event(r#"{"type":"todos","session_id":"fixture","payload":"private transcript must not be logged"}"#).unwrap(); assert_eq!(event_key(&event), Some(("todos", None))); - REQUESTS.with_borrow(|pending| assert_eq!(pending.len(), 1)); + assert_eq!(timings.pending.lock().unwrap().len(), 1); assert!( matches!(event, ServerEvent::Todos { payload, .. } if payload == "private transcript must not be logged") ); - REQUESTS.with_borrow_mut(VecDeque::clear); - assert!(parse_event("not json").is_err()); + // Repeated unsolicited snapshots are not additional bootstrap responses. + assert!(timings.response_wait(&todo_event()).is_none()); + assert!(timings.parse_event("not json").is_err()); +} + +#[test] +fn cloud_latency_polls_take_priority_over_attach_and_unsolicited_todos() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(42, Some("fixture"))); + timings.request_sent(&Request::GetTodos { id: 43 }); + assert_eq!( + timings.response_wait(&todo_event()).map(|entry| entry.1), + Some(43) + ); + assert_eq!( + timings.response_wait(&todo_event()).map(|entry| entry.1), + Some(42) + ); + assert!(timings.response_wait(&todo_event()).is_none()); + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].id, 42); +} + +#[test] +fn cloud_latency_fresh_subscribe_done_does_not_expire_waiting_for_history() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(10, None)); + timings.request_sent(&Request::GetHistory { id: 11 }); + let event = timings.parse_event(r#"{"type":"done","id":10}"#).unwrap(); + assert!(matches!(event, ServerEvent::Done { id: 10 })); + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].id, 11); +} + +#[test] +fn cloud_latency_target_subscribe_done_retains_history_and_error_clears_it() { + let timings = RequestTimings::default(); + timings.request_sent(&subscribe(10, Some("fixture"))); + assert!( + timings + .response_wait(&ServerEvent::Done { id: 10 }) + .is_none() + ); + assert_eq!(timings.pending.lock().unwrap().len(), 1); + let event = timings + .parse_event(r#"{"type":"error","id":10,"message":"PRIVATE"}"#) + .unwrap(); + assert!(matches!(event, ServerEvent::Error { id: 10, .. })); + assert!(timings.pending.lock().unwrap().is_empty()); +} + +#[test] +fn cloud_latency_correlation_survives_thread_migration_and_isolates_connections() { + let first = std::sync::Arc::new(RequestTimings::default()); + let second = RequestTimings::default(); + let sender = first.clone(); + std::thread::spawn(move || sender.request_sent(&Request::GetTodos { id: 1 })) + .join() + .unwrap(); + assert!(second.response_wait(&todo_event()).is_none()); + second.request_sent(&Request::GetTodos { id: 1 }); + let receiver = first.clone(); + let response = std::thread::spawn(move || receiver.response_wait(&todo_event())) + .join() + .unwrap(); + assert_eq!(response.map(|entry| entry.1), Some(1)); + assert!(first.pending.lock().unwrap().is_empty()); + assert_eq!(second.pending.lock().unwrap().len(), 1); + assert_eq!( + second.response_wait(&todo_event()).map(|entry| entry.1), + Some(1) + ); +} + +#[test] +fn cloud_latency_expiry_remains_bounded_and_reused_ids_replace_old_requests() { + let timings = RequestTimings::default(); + timings.request_sent(&Request::GetTodos { id: 1 }); + timings.pending.lock().unwrap()[0].started = + Instant::now() - std::time::Duration::from_secs(121); + timings.request_sent(&Request::GetHistory { id: 2 }); + timings.request_sent(&Request::GetTodos { id: 2 }); + let pending = timings.pending.lock().unwrap(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].id, 2); + assert_eq!(pending[0].operation, "todos"); } #[test] @@ -42,5 +147,4 @@ fn cloud_latency_paint_milestone_is_consumed_once() { PAINT.with_borrow(|pending| assert_eq!(pending.as_ref().map(|entry| entry.0), Some("todos"))); rendered(); PAINT.with_borrow(|pending| assert!(pending.is_none())); - rendered(); } diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index 1c6f53e40ca..dbaa57fa7d1 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -49,13 +49,21 @@ not an authorization mechanism. `none` means a metric was not available. JSON parsing. It includes remote queue/work, transfer and client scheduling, not just network RTT. Server writer-lock versus write versus encode timings help separate those causes. This measurement survives a cancelled socket-read future -because it is attached to the outstanding request, not a particular read attempt. +because it is attached to the connection's outstanding request, not a particular +read attempt or executor thread. Separate connections never share correlation +queues. A fresh Subscribe completes with Done, while a target-aware Subscribe +retains its correlation until History. Explicit todo polls take precedence over +bootstrap snapshots. Todo events have no wire request ID, so their association is +best-effort FIFO, not proof that a specific request caused a pushed snapshot. The correlation queue is bounded; old entries expire when subsequent requests arrive. Expiry is a diagnostic observation, not an application timeout. `frame_ready` measures from completed event application until a composed TUI frame is ready. It is not a physical display/terminal flush timestamp. Updates may share -a frame. Existing rendering diagnostics give detailed draw costs when needed. +a frame. Unfocused idle terminals intentionally coalesce redraws for up to five +seconds, and hibernation can defer them further. This wall-clock wait is not the +CPU cost of rendering. Existing rendering diagnostics give detailed draw costs +when needed. Fast streaming tokens are deliberately not logged individually. Key sync milestones are logged consistently, while unrelated protocol events only receive a parse record if slow or malformed, avoiding per-token logging overhead. @@ -67,6 +75,17 @@ credentials, keys, paths, transcript contents, payloads or raw server error bodi Keep ordinary diagnostic log files private nevertheless, since other existing subsystems may include information beyond these timing records. +Unchanged mobile reconciliation candidates are counted in a single +`mobile_sync candidates=... unchanged=... progress_updates=...` summary rather +than timed and logged individually. Changed-session and HTTP timings remain. +A repeatable no-network filesystem benchmark is available with: + +```bash +justrust test --remote -p jcode-base --lib benchmark_unchanged_reconciliation -- --ignored --nocapture +``` + +It measures 1,300 unchanged sessions, not upload or end-to-end cloud latency. + A high total alone is not proof of the cause. Compare cache hits/misses, success versus failure, cold versus warm connections and local versus daemon stage timing. Do not claim a cold VM wake or a full end-to-end cloud resume is subsecond just From e6d9cb47cda423d85691183e5085c07504d6bbb5 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:18:19 -0800 Subject: [PATCH 044/236] Fix queued follow-up dropped after reload when combined with delivered recovery reminder --- .../src/server/client_processing.rs | 81 +++++++++++++++---- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/crates/jcode-app-core/src/server/client_processing.rs b/crates/jcode-app-core/src/server/client_processing.rs index c5658261343..00706e78888 100644 --- a/crates/jcode-app-core/src/server/client_processing.rs +++ b/crates/jcode-app-core/src/server/client_processing.rs @@ -156,6 +156,33 @@ pub(super) async fn append_context_message( } } +/// A send whose reload continuation reminder was already delivered is a pure +/// replay only when it carries no user content. A queued follow-up combined +/// with that reminder must still run (only the reminder is dropped). +fn delivered_replay_is_reminder_only(content: &str, image_count: usize) -> bool { + content.trim().is_empty() && image_count == 0 +} + +#[cfg(test)] +mod delivered_replay_tests { + use super::delivered_replay_is_reminder_only; + + #[test] + fn reminder_only_replay_is_suppressed() { + assert!(delivered_replay_is_reminder_only("", 0)); + assert!(delivered_replay_is_reminder_only(" \n", 0)); + } + + #[test] + fn queued_followup_with_delivered_reminder_is_not_suppressed() { + assert!(!delivered_replay_is_reminder_only( + "please also fix the tests", + 0 + )); + assert!(!delivered_replay_is_reminder_only("", 1)); + } +} + #[allow(clippy::too_many_arguments)] pub(super) async fn start_processing_message( message: ProcessingMessage, @@ -171,7 +198,7 @@ pub(super) async fn start_processing_message( id, content, images, - system_reminder, + mut system_reminder, active_skill, server_failover, } = message; @@ -207,23 +234,45 @@ pub(super) async fn start_processing_message( return; } }; - if let Some(reminder) = system_reminder.as_deref() { - match super::super::reload_recovery::is_delivered_continuation(client_session_id, reminder) - { - Ok(true) => { - crate::logging::info(&format!( - "Suppressing delivered reload recovery replay session={} id={}", - client_session_id, id - )); - let _ = client_event_tx.send(ServerEvent::Done { id }); - return; + let reminder_already_delivered = match system_reminder.as_deref() { + Some(reminder) => { + match super::super::reload_recovery::is_delivered_continuation( + client_session_id, + reminder, + ) { + Ok(delivered) => delivered, + Err(error) => { + crate::logging::warn(&format!( + "Failed to inspect reload recovery replay session={}: {}", + client_session_id, error + )); + false + } } - Ok(false) => {} - Err(error) => crate::logging::warn(&format!( - "Failed to inspect reload recovery replay session={}: {}", - client_session_id, error - )), } + None => false, + }; + if reminder_already_delivered { + if delivered_replay_is_reminder_only(&content, images.len()) { + crate::logging::info(&format!( + "Suppressing delivered reload recovery replay session={} id={}", + client_session_id, id + )); + let _ = client_event_tx.send(ServerEvent::Done { id }); + return; + } + // After a reload the client combines a queued user follow-up with the + // reload continuation reminder into one send. Only the reminder is a + // replay. Suppressing the whole message silently dropped the user's + // queued prompt, so strip the reminder and run the real content. + crate::logging::info(&format!( + "Stripping delivered reload recovery reminder but keeping queued content session={} id={} content_chars={} images={}", + client_session_id, + id, + content.chars().count(), + images.len() + )); + system_reminder = None; } if !agent_guard.set_remote_active_skill(active_skill.clone()) { let skill_name = active_skill.as_deref().unwrap_or_default(); From 7869b1b5db40a1baf35da018258bef9da73b7ffa Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:19:56 -0800 Subject: [PATCH 045/236] cloud runtime: push --from-source/--reload, runtime reload, /cloud reload --- .../jcode-tui/src/tui/app/commands_cloud.rs | 22 ++- .../src/tui/app/commands_cloud_tests.rs | 12 ++ .../src/tui/app/state_ui_input_helpers.rs | 4 + src/cli/args/cloud_runtime.rs | 44 +++++- src/cli/cloud_runtime.rs | 149 +++++++++++++++++- 5 files changed, 223 insertions(+), 8 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/commands_cloud.rs b/crates/jcode-tui/src/tui/app/commands_cloud.rs index 2d3b17be3d8..bb6943b52db 100644 --- a/crates/jcode-tui/src/tui/app/commands_cloud.rs +++ b/crates/jcode-tui/src/tui/app/commands_cloud.rs @@ -41,6 +41,10 @@ pub(super) enum CloudCommand { Prepare { host: Option, }, + /// Restart the managed cloud daemon onto its selected runtime once idle. + Reload { + force: bool, + }, } pub(super) fn parse_cloud_command(trimmed: &str) -> Option { @@ -58,6 +62,11 @@ pub(super) fn parse_cloud_command(trimmed: &str) -> Option { Some("cancel") => Some(CloudCommand::Cancel), Some("status") | Some("where") => Some(CloudCommand::Where), Some("back") | Some("return") => Some(CloudCommand::Return), + Some("reload") => match (words.next(), words.next()) { + (None, _) => Some(CloudCommand::Reload { force: false }), + (Some("--force"), None) => Some(CloudCommand::Reload { force: true }), + _ => Some(CloudCommand::MoveUsage), + }, Some("replace") => { let (mut host, mut yes, mut dry_run) = (None, false, false); while let Some(word) = words.next() { @@ -227,7 +236,7 @@ pub(super) fn handle_cloud_command(app: &mut App, trimmed: &str, session_id: &st ))); return true; } - if !matches!(command, CloudCommand::Where) { + if !matches!(command, CloudCommand::Where | CloudCommand::Reload { .. }) { if !check_cloud_handoff_input(app, trimmed) { return true; } @@ -363,6 +372,12 @@ fn start_cloud_command( CloudCommand::Where => { cmd.args(["where", "--session", session_id]); } + CloudCommand::Reload { force } => { + cmd.args(["runtime", "reload"]); + if *force { + cmd.arg("--force"); + } + } } if app.cloud_job.is_some() { app.push_display_message(DisplayMessage::error( @@ -1185,6 +1200,11 @@ fn finish_cloud_command(app: &mut App, job: CloudJob, output: std::process::Outp | CloudCommand::MoveUsage => { unreachable!("handled before transfer completion") } + CloudCommand::Reload { .. } => { + app.push_display_message(DisplayMessage::system( + "Cloud daemon reloaded onto its selected runtime. Cloud sessions resume when their clients reconnect.", + )); + } CloudCommand::Where => { let text = if stdout.trim().is_empty() { "This session has never moved.".to_string() diff --git a/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs b/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs index 76babfbd0cd..ddc76bd0485 100644 --- a/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs +++ b/crates/jcode-tui/src/tui/app/commands_cloud_tests.rs @@ -1256,6 +1256,18 @@ fn parses_cloud_commands() { parse_cloud_command("/cloud status"), Some(CloudCommand::Where) ); + assert_eq!( + parse_cloud_command("/cloud reload"), + Some(CloudCommand::Reload { force: false }) + ); + assert_eq!( + parse_cloud_command("/cloud reload --force"), + Some(CloudCommand::Reload { force: true }) + ); + assert_eq!( + parse_cloud_command("/cloud reload now"), + Some(CloudCommand::MoveUsage) + ); assert_eq!( parse_cloud_command("/cloud cancel"), Some(CloudCommand::Cancel) diff --git a/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs b/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs index 2f29505d4da..98d77fbb0d6 100644 --- a/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs +++ b/crates/jcode-tui/src/tui/app/state_ui_input_helpers.rs @@ -521,6 +521,10 @@ impl App { "Preview replacing cloud code with local files", ), ("/cloud status".into(), "Show session location"), + ( + "/cloud reload".into(), + "Restart the cloud daemon onto its new runtime once idle", + ), ("/cloud cancel".into(), "Cancel a pending cloud command"), ]; // Typing the command or a trailing space must never add consent. diff --git a/src/cli/args/cloud_runtime.rs b/src/cli/args/cloud_runtime.rs index 152291b972d..fe6328d0d04 100644 --- a/src/cli/args/cloud_runtime.rs +++ b/src/cli/args/cloud_runtime.rs @@ -6,12 +6,33 @@ use std::path::PathBuf; pub(crate) enum CloudRuntimeCommand { /// Show the installed runtime, running daemon, and update policy. Status, - /// Upload an explicit Linux x86_64 runtime package and select dev policy. - /// Active daemons keep their executable until their next normal start. + /// Upload a Linux x86_64 runtime package and select dev policy. + /// Active daemons keep their executable until their next start, or until + /// `--reload` restarts the daemon once no session is mid-turn. + #[command(group(clap::ArgGroup::new("source").required(true).args(["artifact", "from_source"])))] Push { /// Compatible jcode-linux-x86_64.tar.gz package, not a laptop executable. #[arg(long)] - artifact: PathBuf, + artifact: Option, + /// Build a portable Linux package from this jcode checkout first. + #[arg(long)] + from_source: bool, + /// After upload, restart the cloud daemon onto it once no session is mid-turn. + #[arg(long)] + reload: bool, + /// Seconds to wait for running turns before giving up (with --reload). + #[arg(long, default_value_t = 1800)] + wait_secs: u64, + }, + /// Restart the cloud daemon onto the selected runtime once no session is + /// mid-turn. Sessions are saved and resume when clients reconnect. + Reload { + /// Restart immediately even if turns are running (they are interrupted). + #[arg(long)] + force: bool, + /// Seconds to wait for running turns before giving up. + #[arg(long, default_value_t = 1800)] + wait_secs: u64, }, /// Return to automatic managed stable updates (may select an older release). Stable { @@ -52,9 +73,24 @@ mod tests { assert!(matches!( parsed.command, Some(Command::Cloud(CloudCommand::Runtime { - action: CloudRuntimeCommand::Push { artifact } + action: CloudRuntimeCommand::Push { artifact: Some(artifact), from_source: false, reload: false, .. } })) if artifact == std::path::Path::new("portable.tar.gz") )); + assert!(Args::try_parse_from([ + "jcode", "cloud", "runtime", "push", "--artifact", "a.tar.gz", "--from-source" + ]) + .is_err()); + let parsed = Args::try_parse_from([ + "jcode", "cloud", "runtime", "push", "--from-source", "--reload" + ]) + .unwrap(); + assert!(matches!( + parsed.command, + Some(Command::Cloud(CloudCommand::Runtime { + action: CloudRuntimeCommand::Push { artifact: None, from_source: true, reload: true, wait_secs: 1800 } + })) + )); + assert!(Args::try_parse_from(["jcode", "cloud", "runtime", "reload", "--force"]).is_ok()); } #[test] diff --git a/src/cli/cloud_runtime.rs b/src/cli/cloud_runtime.rs index 4cff4bdaadf..08cc320f106 100644 --- a/src/cli/cloud_runtime.rs +++ b/src/cli/cloud_runtime.rs @@ -35,6 +35,132 @@ fn remote_command(verb: &str, value: Option<&str>) -> Result { Ok(command) } +/// Remote reload: wait until no session is streaming, then restart the +/// managed service. `ExecStartPre` activates any pending runtime and +/// `ExecStopPost` rolls back a runtime that fails to start. Only an integer +/// and a fixed flag are interpolated. +fn reload_command(wait_secs: u64, force: bool) -> String { + let force = if force { 1 } else { 0 }; + format!( + r#"set -u +export XDG_RUNTIME_DIR=/run/user/$(id -u) +say() {{ echo "[cloud reload] $*" >&2; }} +streaming() {{ /opt/jcode-cloud/jcode menubar --json 2>/dev/null | sed -n 's/.*"streaming":\([0-9]*\).*/\1/p'; }} +deadline=$(( $(date +%s) + {wait_secs} )) +last="" +if [ {force} = 0 ]; then + while :; do + n=$(streaming); n=${{n:-0}} + [ "$n" = 0 ] && break + if [ "$(date +%s)" -ge "$deadline" ]; then say "still $n session(s) mid-turn after {wait_secs}s; not restarting. Retry later or use --force."; exit 3; fi + [ "$n" != "$last" ] && say "waiting for $n session(s) to finish their turn" + last=$n; sleep 5 + done +fi +before=$(readlink /opt/jcode-cloud/jcode) +say "restarting cloud daemon" +systemctl --user restart jcode-cloud.service || {{ say "restart failed"; exit 1; }} +for i in $(seq 1 60); do + pid=$(systemctl --user show jcode-cloud.service --property=MainPID --value) + if [ "${{pid:-0}}" != 0 ] && [ -S "$XDG_RUNTIME_DIR/jcode.sock" ]; then + sleep 3 + pid2=$(systemctl --user show jcode-cloud.service --property=MainPID --value) + if [ "$pid2" = "$pid" ]; then + after=$(readlink /opt/jcode-cloud/jcode) + [ "$after" = "$before" ] || say "activated new runtime" + say "daemon ready: $(/opt/jcode-cloud/jcode --version 2>/dev/null)" + exit 0 + fi + fi + sleep 1 +done +say "daemon did not become ready; see: journalctl --user -u jcode-cloud.service" +sudo -n /usr/local/sbin/jcode-cloud-runtime status >&2 || true +exit 1 +"# + ) +} + +fn run_remote(script: &str, input: Option) -> Result { + let args = cloud_managed::ssh_args()?; + Command::new("ssh") + .args(args) + .args(["-T", "-o", "BatchMode=yes", "-o", "ConnectTimeout=30"]) + .arg(cloud_managed::HOST) + .arg(script) + .stdin(input.map(Stdio::from).unwrap_or_else(Stdio::null)) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .status() + .context("connect to the managed cloud host over SSH") +} + +fn reload(wait_secs: u64, force: bool) -> Result<()> { + let status = run_remote(&reload_command(wait_secs, force), None)?; + match status.code() { + Some(0) => Ok(()), + Some(3) => bail!("cloud reload deferred: turns were still running. Nothing was restarted."), + _ => bail!("cloud reload failed ({status})"), + } +} + +/// Build a portable Linux x86_64 package from the jcode checkout containing +/// the current directory. +fn build_from_source() -> Result { + let root = Command::new("git") + .args(["rev-parse", "--show-toplevel"]) + .output() + .context("run git")?; + if !root.status.success() { + bail!("--from-source must run inside a jcode checkout"); + } + let root = std::path::PathBuf::from(String::from_utf8_lossy(&root.stdout).trim()); + let script = root.join("scripts/package_cloud_runtime.sh"); + if !script.is_file() { + bail!("{} is not a jcode checkout", root.display()); + } + let target = "x86_64-unknown-linux-gnu"; + eprintln!("[cloud build] building portable Linux runtime in {}", root.display()); + let builder = if Command::new("justrust").arg("--version").output().is_ok() { + "justrust" + } else { + "cargo" + }; + let status = Command::new(builder) + .current_dir(&root) + .args([ + "zigbuild", + "--profile", + "selfdev", + "-p", + "jcode", + "--bin", + "jcode", + "--target", + &format!("{target}.2.17"), + "--features", + "linux-compat-vendored-openssl", + ]) + .status() + .with_context(|| format!("run {builder} zigbuild"))?; + if !status.success() { + bail!("portable Linux build failed ({status})"); + } + let binary = root.join("target").join(target).join("selfdev").join("jcode"); + let output = root.join("target/cloud-runtime/jcode-linux-x86_64.tar.gz"); + eprintln!("[cloud build] packaging"); + let status = Command::new(&script) + .arg(&binary) + .arg(&output) + .stdout(Stdio::null()) + .status() + .context("run package_cloud_runtime.sh")?; + if !status.success() { + bail!("packaging the cloud runtime failed ({status})"); + } + Ok(output) +} + fn valid_release_tag(value: &str) -> bool { let Some(version) = value.strip_prefix('v') else { return false; @@ -112,13 +238,30 @@ pub(crate) fn run_cli(action: CloudRuntimeCommand) -> Result<()> { }; invoke("pin", Some(&version), None) } - CloudRuntimeCommand::Push { artifact } => { + CloudRuntimeCommand::Push { + artifact, + from_source, + reload: then_reload, + wait_secs, + } => { + let artifact = match (artifact, from_source) { + (Some(path), false) => path, + (None, true) => build_from_source()?, + _ => bail!("pass exactly one of --artifact or --from-source"), + }; let (file, digest) = package(&artifact)?; eprintln!( - "Uploading explicit cloud runtime package ({digest}). This selects dev policy for the whole host. Active daemons are not restarted." + "[cloud push] uploading runtime package {digest}. This selects dev policy for the whole host." ); - invoke("push", Some(&digest), Some(file)) + invoke("push", Some(&digest), Some(file))?; + if then_reload { + reload(wait_secs, false) + } else { + eprintln!("Active daemons are not restarted. Run `jcode cloud runtime reload` to switch."); + Ok(()) + } } + CloudRuntimeCommand::Reload { force, wait_secs } => reload(wait_secs, force), } } From 52fbe9d72bbaa98571b747c766aa071caeae518e Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:25:16 -0800 Subject: [PATCH 046/236] Preserve HTTP/1 mobile upload connections with bounded response draining --- crates/jcode-base/src/mobile_sync.rs | 44 +++- .../jcode-base/src/mobile_sync_http_tests.rs | 233 ++++++++++++++++++ docs/CLOUD_LATENCY.md | 15 ++ 3 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 crates/jcode-base/src/mobile_sync_http_tests.rs diff --git a/crates/jcode-base/src/mobile_sync.rs b/crates/jcode-base/src/mobile_sync.rs index b0c3a0c6863..195747e14df 100644 --- a/crates/jcode-base/src/mobile_sync.rs +++ b/crates/jcode-base/src/mobile_sync.rs @@ -288,7 +288,8 @@ fn commit_progress(generation: u64, updates: &BTreeMap) } let _lock = state_lock()?; let mut current = load_state(); - if current.enabled && current.generation == generation && merge_progress(&mut current, updates) { + if current.enabled && current.generation == generation && merge_progress(&mut current, updates) + { storage::write_json_secret(&state_path()?, ¤t)?; } Ok(()) @@ -533,10 +534,48 @@ async fn upload( // Response bodies can echo private content or credentials. anyhow::bail!("mobile sync upload returned {status}"); } + // Dropping an unread HTTP/1 body closes the pooled connection. HTTP/2 + // streams can be cancelled without sacrificing their shared connection. + if matches!( + response.version(), + reqwest::Version::HTTP_10 | reqwest::Version::HTTP_11 + ) { + drain_upload_response(response).await; + } timing.outcome = "success"; Ok(()) } +const UPLOAD_RESPONSE_DRAIN_LIMIT: usize = 64 * 1024; +const UPLOAD_RESPONSE_DRAIN_TIMEOUT: Duration = Duration::from_millis(100); + +async fn drain_upload_response(mut response: reqwest::Response) -> &'static str { + let mut timing = SyncTiming::new("http_upload_body"); + // The server has already accepted the upload. A broken, oversized or slow + // acknowledgment must not turn success into a retry of accepted messages. + timing.outcome = match tokio::time::timeout(UPLOAD_RESPONSE_DRAIN_TIMEOUT, async { + let mut bytes = 0usize; + loop { + match response.chunk().await { + Ok(Some(chunk)) => { + bytes = bytes.saturating_add(chunk.len()); + if bytes > UPLOAD_RESPONSE_DRAIN_LIMIT { + return "size_limit"; + } + } + Ok(None) => return "success", + Err(_) => return "read_error", + } + } + }) + .await + { + Ok(outcome) => outcome, + Err(_) => "timeout", + }; + timing.outcome +} + fn valid_session_id(id: &str) -> bool { !id.is_empty() && id.len() <= 128 @@ -783,6 +822,9 @@ async fn background_sync() { } } +#[cfg(test)] +#[path = "mobile_sync_http_tests.rs"] +mod http_tests; #[cfg(test)] #[path = "mobile_sync_tests.rs"] mod tests; diff --git a/crates/jcode-base/src/mobile_sync_http_tests.rs b/crates/jcode-base/src/mobile_sync_http_tests.rs new file mode 100644 index 00000000000..474dcda1020 --- /dev/null +++ b/crates/jcode-base/src/mobile_sync_http_tests.rs @@ -0,0 +1,233 @@ +use super::*; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +struct KeepAliveFixture { + base: String, + connections: Arc, + task: tokio::task::JoinHandle<()>, +} +impl Drop for KeepAliveFixture { + fn drop(&mut self) { + self.task.abort(); + } +} + +async fn keep_alive_fixture() -> KeepAliveFixture { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let connections = Arc::new(AtomicUsize::new(0)); + let count = connections.clone(); + let task = tokio::spawn(async move { + let mut clients = tokio::task::JoinSet::new(); + loop { + tokio::select! { + accepted = listener.accept() => { + let (mut stream, _) = accepted.unwrap(); + stream.set_nodelay(true).unwrap(); + count.fetch_add(1, Ordering::SeqCst); + clients.spawn(async move { + // Controlled connection setup cost, not a WAN measurement. + tokio::time::sleep(Duration::from_millis(30)).await; + let mut buffered = Vec::new(); + loop { + let header_end = loop { + if let Some(end) = buffered.windows(4).position(|part| part == b"\r\n\r\n") { + break end + 4; + } + let mut chunk = [0; 1024]; + let Ok(n) = stream.read(&mut chunk).await else { return; }; + if n == 0 { return; } + buffered.extend_from_slice(&chunk[..n]); + assert!(buffered.len() <= 1024 * 1024); + }; + let headers = String::from_utf8_lossy(&buffered[..header_end]); + let length = headers.lines().find_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("content-length").then(|| value.trim().parse::().unwrap()) + }).unwrap_or(0); + while buffered.len() < header_end + length { + let mut chunk = [0; 1024]; + let Ok(n) = stream.read(&mut chunk).await else { return; }; + if n == 0 { return; } + buffered.extend_from_slice(&chunk[..n]); + } + buffered.drain(..header_end + length); + if stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 16\r\nConnection: keep-alive\r\n\r\n").await.is_err() { + return; + } + // Ensure success headers and body arrive separately. + tokio::time::sleep(Duration::from_millis(8)).await; + if stream.write_all(b"{\"success\":true}").await.is_err() { + return; + } + } + }); + } + Some(result) = clients.join_next(), if !clients.is_empty() => { result.unwrap(); } + } + } + }); + KeepAliveFixture { + base, + connections, + task, + } +} + +fn body() -> UploadBody<'static> { + UploadBody { + title: None, + source: "local", + host: None, + cwd: None, + model: None, + created_at: String::new(), + updated_at: String::new(), + messages: &[], + } +} + +#[tokio::test] +#[ignore = "controlled HTTP keep-alive benchmark with injected connection/body delays"] +async fn benchmark_mobile_upload_keep_alive() { + for _ in 0..3 { + let fixture = keep_alive_fixture().await; + let client = reqwest::Client::new(); + let started = Instant::now(); + for _ in 0..8 { + upload(&client, &fixture.base, "fixture", "test", &body()) + .await + .unwrap(); + } + eprintln!( + "upload keep-alive fixture: uploads=8 duration_ms={:.3} connections={}", + started.elapsed().as_secs_f64() * 1000.0, + fixture.connections.load(Ordering::SeqCst) + ); + } +} + +#[tokio::test] +async fn mobile_upload_reuses_connection_after_separately_arriving_success_body() { + let fixture = keep_alive_fixture().await; + let client = reqwest::Client::new(); + for _ in 0..4 { + upload(&client, &fixture.base, "fixture", "test", &body()) + .await + .unwrap(); + } + assert_eq!(fixture.connections.load(Ordering::SeqCst), 1); +} + +async fn body_fixture( + headers: &str, + bytes: Vec, + keep_open: bool, +) -> (String, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let response = format!("HTTP/1.1 200 OK\r\n{headers}\r\n\r\n"); + let task = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + stream.set_nodelay(true).unwrap(); + let mut request = Vec::new(); + let header_end = loop { + if let Some(end) = request.windows(4).position(|part| part == b"\r\n\r\n") { + break end + 4; + } + let mut chunk = [0; 1024]; + let count = stream.read(&mut chunk).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&chunk[..count]); + }; + let length = String::from_utf8_lossy(&request[..header_end]) + .lines() + .find_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("content-length") + .then(|| value.trim().parse::().unwrap()) + }) + .unwrap_or(0); + while request.len() < header_end + length { + let mut chunk = [0; 1024]; + let count = stream.read(&mut chunk).await.unwrap(); + assert_ne!(count, 0); + request.extend_from_slice(&chunk[..count]); + } + stream.write_all(response.as_bytes()).await.unwrap(); + stream.write_all(&bytes).await.unwrap(); + if keep_open { + // Much longer than the drain budget. The caller aborts this task. + tokio::time::sleep(Duration::from_secs(30)).await; + } + }); + (base, task) +} + +#[tokio::test] +async fn mobile_upload_body_drain_is_time_bounded() { + let (base, server) = body_fixture("Content-Length: 1", Vec::new(), true).await; + let response = reqwest::get(&base).await.unwrap(); + let outcome = tokio::time::timeout(Duration::from_secs(2), drain_upload_response(response)) + .await + .unwrap(); + assert_eq!(outcome, "timeout"); + server.abort(); +} + +#[tokio::test] +async fn mobile_upload_body_drain_bounds_unknown_chunked_body() { + let payload = vec![b'x'; UPLOAD_RESPONSE_DRAIN_LIMIT + 1]; + let mut chunked = format!("{:x}\r\n", payload.len()).into_bytes(); + chunked.extend(payload); + chunked.extend_from_slice(b"\r\n"); + // No terminating chunk: without the byte cap this would wait for timeout. + let (base, server) = body_fixture("Transfer-Encoding: chunked", chunked, true).await; + let response = reqwest::get(&base).await.unwrap(); + assert_eq!(drain_upload_response(response).await, "size_limit"); + server.abort(); +} + +#[tokio::test] +async fn mobile_upload_body_drain_preserves_exact_boundary_and_empty_success() { + for bytes in [Vec::new(), vec![b'x'; UPLOAD_RESPONSE_DRAIN_LIMIT]] { + let (base, server) = + body_fixture(&format!("Content-Length: {}", bytes.len()), bytes, false).await; + let response = reqwest::get(&base).await.unwrap(); + assert_eq!(drain_upload_response(response).await, "success"); + server.await.unwrap(); + } +} + +#[tokio::test] +async fn mobile_upload_body_drain_classifies_truncation_without_error_text() { + let (base, server) = body_fixture("Content-Length: 100", b"PRIVATE".to_vec(), false).await; + let response = reqwest::get(&base).await.unwrap(); + assert_eq!(drain_upload_response(response).await, "read_error"); + server.await.unwrap(); +} + +#[tokio::test] +async fn mobile_upload_accepted_status_is_not_retried_for_bad_or_stalled_body() { + for (headers, bytes, keep_open) in [ + ("Content-Length: 100", b"PRIVATE".to_vec(), false), + ("Content-Length: 100", Vec::new(), true), + ( + "Content-Length: 1000000", + vec![b'x'; UPLOAD_RESPONSE_DRAIN_LIMIT + 1], + true, + ), + ] { + let (base, server) = body_fixture(headers, bytes, keep_open).await; + tokio::time::timeout( + Duration::from_secs(2), + upload(&reqwest::Client::new(), &base, "fixture", "test", &body()), + ) + .await + .unwrap() + .unwrap(); + server.abort(); + } +} diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index dbaa57fa7d1..a9c65172d1c 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -86,6 +86,21 @@ justrust test --remote -p jcode-base --lib benchmark_unchanged_reconciliation -- It measures 1,300 unchanged sessions, not upload or end-to-end cloud latency. +Successful HTTP/1 mobile uploads drain small acknowledgment bodies to preserve +connection reuse. `http_upload_body` records success, timeout, size-limit or +read-error outcomes, bounded to 100ms and 64KiB. An already accepted upload remains +successful if its acknowledgment body is malformed or slow, avoiding duplicate +retries. HTTP/2 and later multiplexed responses skip this HTTP/1-specific drain. +`http_upload` includes this bounded cleanup, so its timing boundary differs from +older builds that stopped at response headers. The controlled keep-alive benchmark: + +```bash +justrust test --remote -p jcode-base --lib benchmark_mobile_upload_keep_alive -- --ignored --nocapture +``` + +It injects a 30ms per-connection delay and an 8ms body delay. Connection counts +demonstrate reuse, but its elapsed times are not production API measurements. + A high total alone is not proof of the cause. Compare cache hits/misses, success versus failure, cold versus warm connections and local versus daemon stage timing. Do not claim a cold VM wake or a full end-to-end cloud resume is subsecond just From 8e57a89439e9be0b73d51c8fde017c4df8226c30 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:37:49 -0800 Subject: [PATCH 047/236] Filter cloud latency reports by explicit local time window --- docs/CLOUD_LATENCY.md | 6 +++++ scripts/cloud_latency_report.py | 36 +++++++++++++++++++++++++++- scripts/test_cloud_latency_report.py | 21 ++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index a9c65172d1c..1a92aac169f 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -14,6 +14,7 @@ From the repository root: python3 scripts/cloud_latency_report.py --slow-ms 1000 python3 scripts/cloud_latency_report.py ~/.jcode/logs/jcode-2026-11-07.log python3 scripts/cloud_latency_report.py downloaded-cloud-daemon.log --slow-ms 250 +python3 scripts/cloud_latency_report.py --since 2026-11-08T00:28:00 --until 2026-11-08T00:38:00 ``` The report ranks stage/operation/metric groups by maximum observed duration and @@ -23,6 +24,11 @@ logs, so narrow the files when comparing before/after builds. Percentiles descri only the selected observed samples, not a statistically controlled benchmark. The report prints only allowlisted label syntax and numeric fields, never raw log lines. Do not combine overlapping stage/total metrics by summing them. +Time windows use the local wall-clock timestamps printed in the selected logs, +with an inclusive start and exclusive end. Do not pass UTC offsets. Undated or +invalid timestamp records are excluded when a window is selected. A time window +does not identify a binary: several old and new processes can write to one log, +so a post-reload window alone is not proof that every sample used the new build. Most records use `[CLOUD_TIMING]`; mobile records use `mobile_sync stage=...`. Common fields are `stage`, `operation`, `outcome`, and `duration_ms`. Additional diff --git a/scripts/cloud_latency_report.py b/scripts/cloud_latency_report.py index 2220c23f5f8..609f6c9d7f5 100644 --- a/scripts/cloud_latency_report.py +++ b/scripts/cloud_latency_report.py @@ -2,6 +2,7 @@ """Summarize content-free cloud timing metrics from Jcode log files.""" import argparse from collections import defaultdict +from datetime import datetime import glob import math import os @@ -12,6 +13,35 @@ FIELD = re.compile(r"\b([a-z_]+)=([^\s,]+)") NUMBER = re.compile(r"^\d+(?:\.\d+)?$") SAFE_LABEL = re.compile(r"^[a-zA-Z0-9_.:-]{1,96}$") +TIMESTAMP = re.compile(r"^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?)\]") + + +def local_timestamp(value): + """Parse a wall-clock timestamp in the log's local timezone, not UTC.""" + try: + parsed = datetime.fromisoformat(value) + except ValueError as error: + raise argparse.ArgumentTypeError("expected local YYYY-MM-DDTHH:MM:SS") from error + if parsed.tzinfo is not None: + raise argparse.ArgumentTypeError("use the log's local time without a timezone offset") + return parsed + + +def in_window(lines, since=None, until=None): + for line in lines: + if since is not None or until is not None: + match = TIMESTAMP.match(line) + if match is None: + continue + try: + timestamp = datetime.fromisoformat(match[1]) + except ValueError: + continue + if since is not None and timestamp < since: + continue + if until is not None and timestamp >= until: + continue + yield line def collect(lines): @@ -65,7 +95,11 @@ def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("files", nargs="*", help="Log files or glob patterns. '-' reads stdin. Default: all retained Jcode logs.") parser.add_argument("--slow-ms", type=float, default=0, help="Only show stages whose maximum reaches this duration.") + parser.add_argument("--since", type=local_timestamp, help="Inclusive local log time, YYYY-MM-DDTHH:MM:SS. Undated records are excluded.") + parser.add_argument("--until", type=local_timestamp, help="Exclusive local log time, YYYY-MM-DDTHH:MM:SS.") args = parser.parse_args() + if args.since is not None and args.until is not None and args.since >= args.until: + parser.error("--since must be earlier than --until") files = args.files or [str(Path(os.environ.get("JCODE_HOME", Path.home() / ".jcode")) / "logs" / "jcode-*.log")] def lines(): @@ -83,7 +117,7 @@ def lines(): yield from handle except OSError as error: print(f"Could not read log file: {error.strerror}", file=sys.stderr) - groups, failures = collect(lines()) + groups, failures = collect(in_window(lines(), args.since, args.until)) report(groups, failures, args.slow_ms) diff --git a/scripts/test_cloud_latency_report.py b/scripts/test_cloud_latency_report.py index 70e98e2e6ba..77cc18424e9 100644 --- a/scripts/test_cloud_latency_report.py +++ b/scripts/test_cloud_latency_report.py @@ -10,6 +10,27 @@ class CloudLatencyReportTests(unittest.TestCase): + def test_window_is_inclusive_exclusive_and_handles_mixed_logs(self): + lines = [ + "[2026-11-07 23:59:59.999] [CLOUD_TIMING] stage=old duration_ms=9000", + "[2026-11-08 00:00:00.000] [CLOUD_TIMING] stage=new duration_ms=20", + "[2026-11-08 00:01:00.000] [CLOUD_TIMING] stage=later duration_ms=30", + "[CLOUD_TIMING] stage=undated duration_ms=9999", + "[2026-13-08 00:00:00] [CLOUD_TIMING] stage=invalid duration_ms=9999", + ] + since = reporter.local_timestamp("2026-11-08T00:00:00") + until = reporter.local_timestamp("2026-11-08T00:01:00") + self.assertEqual(list(reporter.in_window(lines, since, until)), [lines[1]]) + self.assertEqual(list(reporter.in_window(lines)), lines) + self.assertEqual(list(reporter.in_window(lines[:3], since=since)), lines[1:3]) + self.assertEqual(list(reporter.in_window(lines[:3], until=until)), lines[:2]) + + def test_window_rejects_timezone_ambiguity_and_invalid_dates(self): + import argparse + for value in ["invalid", "2026-11-08T00:00:00Z", "2026-11-08T00:00:00-08:00"]: + with self.assertRaises(argparse.ArgumentTypeError): + reporter.local_timestamp(value) + def test_collects_failure_and_secondary_wait_metrics(self): groups, failures = reporter.collect([ "[INFO] [CLOUD_TIMING] stage=event_parse operation=history outcome=ok duration_ms=12.5 request_wait_ms=1400 bytes=261000\n", From 05d778f37ef52dbbcbfa0b6453c30892760a93c4 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:38:56 -0800 Subject: [PATCH 048/236] Count bounded cloud transport failures in latency reports --- scripts/cloud_latency_report.py | 7 ++++++- scripts/test_cloud_latency_report.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/cloud_latency_report.py b/scripts/cloud_latency_report.py index 609f6c9d7f5..265fe36f47e 100644 --- a/scripts/cloud_latency_report.py +++ b/scripts/cloud_latency_report.py @@ -14,6 +14,11 @@ NUMBER = re.compile(r"^\d+(?:\.\d+)?$") SAFE_LABEL = re.compile(r"^[a-zA-Z0-9_.:-]{1,96}$") TIMESTAMP = re.compile(r"^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?)\]") +FAILURE_OUTCOMES = { + "error", "failed", "dropped", "timeout", "cancelled", "expired", + "error_or_cancelled", "http_error", "load_error", "read_error", + "size_limit", "oversized", "identity_mismatch", "exit_failure", +} def local_timestamp(value): @@ -68,7 +73,7 @@ def collect(lines): continue key = (name, metric) groups[key].append(number) - if outcome in {"error", "failed", "dropped", "timeout", "cancelled", "expired", "error_or_cancelled"}: + if outcome in FAILURE_OUTCOMES: failures[key] += 1 return groups, failures diff --git a/scripts/test_cloud_latency_report.py b/scripts/test_cloud_latency_report.py index 77cc18424e9..db5d0ec4142 100644 --- a/scripts/test_cloud_latency_report.py +++ b/scripts/test_cloud_latency_report.py @@ -53,6 +53,16 @@ def test_ignores_unrelated_invalid_and_nonfinite_values(self): ]) self.assertFalse(groups) + def test_counts_http_body_and_projection_failures_not_normal_skips(self): + bad = ["http_error", "load_error", "read_error", "size_limit", "oversized", "identity_mismatch", "exit_failure"] + normal = ["success", "miss", "skipped_absent", "skipped_disabled", "success_cache_unavailable"] + groups, failures = reporter.collect([ + f"[CLOUD_TIMING] stage=probe outcome={outcome} duration_ms=10" + for outcome in bad + normal + ]) + self.assertEqual(len(groups[("probe", "duration_ms")]), len(bad + normal)) + self.assertEqual(failures[("probe", "duration_ms")], len(bad)) + def test_report_is_ranked_filtered_and_content_free(self): groups, failures = reporter.collect([ "[CLOUD_TIMING] stage=fast outcome=ok duration_ms=20 payload=PRIVATE", From 8de9277905444558ae11c0efa46965e33bbc4fdb Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:42:07 -0800 Subject: [PATCH 049/236] Attribute bounded workspace commands to static operations --- crates/jcode-tui/src/tui/cloud_git.rs | 37 ++++++++++++- .../src/tui/cloud_git_stderr_tests.rs | 53 +++++++++++++++++++ crates/jcode-tui/src/tui/cloud_status.rs | 6 ++- .../src/tui/session_picker/cloud_preview.rs | 3 +- .../loading/mobile_inventory.rs | 23 +++++--- 5 files changed, 111 insertions(+), 11 deletions(-) diff --git a/crates/jcode-tui/src/tui/cloud_git.rs b/crates/jcode-tui/src/tui/cloud_git.rs index a09a78c97f0..b46a5647977 100644 --- a/crates/jcode-tui/src/tui/cloud_git.rs +++ b/crates/jcode-tui/src/tui/cloud_git.rs @@ -168,7 +168,7 @@ pub(crate) async fn probe_workspace( } async fn probe_with_timeout(command: tokio::process::Command, deadline: Duration) -> RepoState { - match bounded_output(command, deadline, 1024 * 1024).await { + match bounded_output_for(command, deadline, 1024 * 1024, WorkspaceOperation::GitProbe).await { Some((status, _)) if status.code() == Some(42) => RepoState::NoRepository, Some((status, bytes)) if status.success() => parse(&bytes) .map(RepoState::Ready) @@ -177,12 +177,47 @@ async fn probe_with_timeout(command: tokio::process::Command, deadline: Duration } } +/// Fixed vocabulary only: never include paths, commands, hosts, or session content. +#[derive(Clone, Copy)] +pub(crate) enum WorkspaceOperation { + GitProbe, + InventoryPrepare, + InventoryProjection, + Preview, + CloudStatus, + CloudStatusDirectories, +} + +impl WorkspaceOperation { + fn label(self) -> &'static str { + match self { + Self::GitProbe => "git_probe", + Self::InventoryPrepare => "inventory_prepare", + Self::InventoryProjection => "inventory_projection", + Self::Preview => "preview", + Self::CloudStatus => "cloud_status", + Self::CloudStatusDirectories => "cloud_status_directories", + } + } +} + +#[cfg(test)] pub(crate) async fn bounded_output( + command: tokio::process::Command, + deadline: Duration, + limit: usize, +) -> Option<(std::process::ExitStatus, Vec)> { + bounded_output_for(command, deadline, limit, WorkspaceOperation::GitProbe).await +} + +pub(crate) async fn bounded_output_for( mut command: tokio::process::Command, deadline: Duration, limit: usize, + operation: WorkspaceOperation, ) -> Option<(std::process::ExitStatus, Vec)> { let mut timing = super::cloud_latency::Span::new("workspace_command"); + timing.operation(operation.label()); #[cfg(unix)] { use std::os::unix::process::CommandExt; diff --git a/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs b/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs index 7676470d358..e80cf3769de 100644 --- a/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs +++ b/crates/jcode-tui/src/tui/cloud_git_stderr_tests.rs @@ -1,5 +1,58 @@ use super::*; use crate::tui::cloud_git::{bounded_output, script_command}; + +#[test] +fn workspace_operations_are_static_and_all_production_paths_are_assigned() { + use crate::tui::cloud_git::WorkspaceOperation::*; + assert!(include_str!("cloud_git.rs").contains("timing.operation(operation.label())")); + assert_eq!( + [ + GitProbe, + InventoryPrepare, + InventoryProjection, + Preview, + CloudStatus, + CloudStatusDirectories + ] + .map(|operation| operation.label()), + [ + "git_probe", + "inventory_prepare", + "inventory_projection", + "preview", + "cloud_status", + "cloud_status_directories" + ] + ); + // These source contracts cover cfg(not(test)) workers too, without making + // live SSH calls or putting any workspace/session content in timing labels. + for (source, operations) in [ + (include_str!("cloud_git.rs"), &["GitProbe"][..]), + ( + include_str!("cloud_status.rs"), + &["CloudStatus", "CloudStatusDirectories"][..], + ), + ( + include_str!("session_picker/cloud_preview.rs"), + &["Preview"][..], + ), + ( + include_str!("session_picker/loading/mobile_inventory.rs"), + &["InventoryPrepare", "InventoryProjection"][..], + ), + ] { + let boundary = if operations == ["Preview"] { + "fn parse_preview(" + } else { + "#[cfg(test)]" + }; + let production = source.split(boundary).next().unwrap(); + assert!(!production.contains("bounded_output(")); + for operation in operations { + assert!(production.contains(&format!("WorkspaceOperation::{operation}"))); + } + } +} use std::time::Duration; const SAFE: &str = "[CLOUD_TIMING] stage=projection outcome=success duration_ms=17"; diff --git a/crates/jcode-tui/src/tui/cloud_status.rs b/crates/jcode-tui/src/tui/cloud_status.rs index 7f3b84287d3..16702fdbf1f 100644 --- a/crates/jcode-tui/src/tui/cloud_status.rs +++ b/crates/jcode-tui/src/tui/cloud_status.rs @@ -265,7 +265,7 @@ pub(crate) fn gather(session_id: Option) -> Option { let mut previous = None; loop { let start = std::time::Instant::now(); - let response = super::cloud_git::bounded_output( + let response = super::cloud_git::bounded_output_for( super::cloud_git::script_command( Some(&host), config.as_deref(), @@ -274,6 +274,7 @@ pub(crate) fn gather(session_id: Option) -> Option { ), Duration::from_secs(12), 256 * 1024, + super::cloud_git::WorkspaceOperation::CloudStatus, ) .await; let parsed = response @@ -293,7 +294,7 @@ pub(crate) fn gather(session_id: Option) -> Option { .lock() .unwrap_or_else(|e| e.into_inner()) .clone(); - let directory_response = super::cloud_git::bounded_output( + let directory_response = super::cloud_git::bounded_output_for( super::cloud_git::script_command( Some(&host), config.as_deref(), @@ -302,6 +303,7 @@ pub(crate) fn gather(session_id: Option) -> Option { ), Duration::from_secs(8), 1024 * 1024, + super::cloud_git::WorkspaceOperation::CloudStatusDirectories, ) .await; let directories = directory_response diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs index 80d77c5a674..1df0e60914d 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview.rs @@ -179,10 +179,11 @@ fn fetch_bytes(session_id: &str, host: &str) -> Option> { }, "ssh_runtime", )?; - let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output( + let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output_for( command, Duration::from_secs(15), cache::MAX_BYTES, + crate::tui::cloud_git::WorkspaceOperation::Preview, ))?; if !status.success() { timer.outcome("exit_failure"); diff --git a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs index d6aec3956ec..60cc281aca0 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading/mobile_inventory.rs @@ -63,10 +63,14 @@ pub(super) fn refresh() -> Result { &super::super::cloud_preview::projection_script("inventory"), ); let mut fetch = StageTimer::new("inventory_ssh"); - let (status, bytes) = - crate::tui::cloud_git::bounded_output(command, Duration::from_secs(10), 1024 * 1024) - .await - .ok_or_else(|| anyhow::anyhow!("cloud inventory verification unavailable"))?; + let (status, bytes) = crate::tui::cloud_git::bounded_output_for( + command, + Duration::from_secs(10), + 1024 * 1024, + crate::tui::cloud_git::WorkspaceOperation::InventoryProjection, + ) + .await + .ok_or_else(|| anyhow::anyhow!("cloud inventory verification unavailable"))?; anyhow::ensure!(status.success(), "cloud inventory verification failed"); fetch.outcome("success"); drop(fetch); @@ -125,9 +129,14 @@ async fn prepare_ssh_config(config: &Path) -> Result<()> { .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::null()) .kill_on_drop(true); - let (status, _) = crate::tui::cloud_git::bounded_output(command, Duration::from_secs(8), 4096) - .await - .ok_or_else(|| anyhow::anyhow!("cloud SSH setup unavailable"))?; + let (status, _) = crate::tui::cloud_git::bounded_output_for( + command, + Duration::from_secs(8), + 4096, + crate::tui::cloud_git::WorkspaceOperation::InventoryPrepare, + ) + .await + .ok_or_else(|| anyhow::anyhow!("cloud SSH setup unavailable"))?; anyhow::ensure!( status.success() && config.is_file(), "cloud SSH setup failed" From e19759fd085bf037b1752f20747d14da025949e8 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:42:49 -0800 Subject: [PATCH 050/236] Avoid redundant cloud preview journal entry scans --- .../cloud_preview/projection_tests.rs | 51 +++++++++++++++++++ .../session_picker/cloud_preview/remote.py | 50 +++++++++--------- 2 files changed, 78 insertions(+), 23 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs index 7f987591234..fd67cf0a88a 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/projection_tests.rs @@ -28,6 +28,16 @@ // Inventory actual mmap ms 1019.11, 1507.74, 742.82: no consistent speedup. // Evidence: ~/.jcode/scratch/bounded-preview-final-scanner-results.jsonl and // paired-projection-final-results.txt, reproduced by paired-projection.py. +// Journal follow-up, 2026-11-08 UTC, synthetic LOCAL inputs only, no cloud reads: +// 34,662,320-byte frozen mmap journal, 220 entries x 120 ordinary messages with +// escaped quotes/backslashes, braces and Unicode. Alternating before/after order, +// one warmup then six measured runs per mode. Removing only the redundant outer +// journal-entry scan: preview medians 895.38 -> 660.65 ms (-26.2%), inventory +// 566.62 -> 284.27 ms (-49.8%). Every paired JSON output was byte-identical. +// Snapshot/journal size, mtime_ns and SHA256 were unchanged across all runs. +// Journal SHA256 aa9a2e05ed8a7500ed42fdf832b68995d488814752ec6abd69a5af2f91e4dafd. +// Local reproduction: ~/.jcode/scratch/journal-paired.py, journal-before.py and +// journal-paired-results.txt. This does not establish live-cloud/SSH latency. use super::*; use std::io::Write; @@ -109,6 +119,47 @@ encoded = b'{"messages":[{"id":"old"}],"messages":[]}' for key,a,b in fields(encoded, message_ranges=ranges): pass assert not ranges +# Compare exact field spans with the former outer-container scan, including +# whitespace, adjacent entries, duplicate keys, scalars and escaped braces. +def previous_journal_fields(data): + pos = 0 + while pos < len(data): + pos = skip_space(data, pos) + if pos == len(data): + break + end = value_end(data, pos) + yield from fields(data, pos) + pos = end +entries = [b'{}', b'{"meta":{"title":"first","title":"last"}}', + b'{"append_messages":[null,1,"escaped\\\"{}[]",{},[]],"append_messages":[]}', + json.dumps({'unknown': [{'nested': samples}]}).encode()] +for separator in (b'', b'\n', b' \r\n\t'): + encoded = separator + separator.join(entries) + separator + assert list(journal_fields(encoded)) == list(previous_journal_fields(encoded)) +for malformed in (b'{', b'{"meta":{}} {', b'{"x":[]]', b'{"x":"unterminated}'): + try: + list(journal_fields(malformed)) + except (ValueError, IndexError): + pass + else: + raise AssertionError(repr(malformed)) +import tempfile +with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'journal.json' + path.write_bytes(b'{"id":"journal","messages":[],"route_api_method":"original"}') + journal = path.with_suffix('.journal.jsonl') + journal.write_bytes(b'{"meta":{"title":"first","title":"last","route_api_method":"ignored"},"append_messages":[null,1,"scalar"]}' + b'{"meta":{"route_api_method_present":true,"route_api_method":"responses"},"append_messages":[]}' + b'{"meta":{"route_api_method_present":true,"route_api_method_present":false,"route_api_method":"ignored"}} \n') + before = [(p.stat().st_size, p.stat().st_mtime_ns) for p in (path, journal)] + result = project(path, True) + assert result['title'] == 'last' + assert result['route_api_method'] == 'responses' + assert result['messages'] == [None, 1, 'scalar'] + assert all(result[k] == [] for k in ('env_snapshots', 'memory_injections', 'replay_events')) + inventory = project(path, False) + assert inventory['title'] == 'last' and 'messages' not in inventory + assert before == [(p.stat().st_size, p.stat().st_mtime_ns) for p in (path, journal)] for malformed in (b'{', b'[', b'{"x":"unterminated}', b'["escaped\\"', b'{]', b'[{]}'): try: value_end(malformed, 0) diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py index 244951988f7..53dcbeee146 100644 --- a/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py +++ b/crates/jcode-tui/src/tui/session_picker/cloud_preview/remote.py @@ -94,7 +94,7 @@ def fields(data, pos=0, message_ranges=None): while True: pos = skip_space(data, pos) if data[pos] == 125: - return + return pos + 1 end = string_end(data, pos) key = json.loads(data[pos:end]) pos = skip_space(data, end) @@ -110,12 +110,23 @@ def fields(data, pos=0, message_ranges=None): yield key, pos, end pos = skip_space(data, end) if data[pos] == 125: - return + return pos + 1 if data[pos] != 44: raise ValueError('expected comma') pos += 1 +def journal_fields(data): + # fields already scans every value. Its return value is the object boundary, + # so journal entries need no redundant whole-container value_end scan. + pos = 0 + while pos < len(data): + pos = skip_space(data, pos) + if pos == len(data): + break + pos = yield from fields(data, pos) + + def recent_messages(data, start, end, messages, ranges=None): # Locate the tail without deserializing old message bodies. The range deque # is bounded even for snapshots with millions of messages. @@ -170,27 +181,20 @@ def project(path, preview): journal = path.with_suffix('.journal.jsonl') if journal.exists() and journal.stat().st_size: with journal.open('rb') as source, mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) as data: - pos = 0 - while pos < len(data): - pos = skip_space(data, pos) - if pos == len(data): - break - end = value_end(data, pos) - for key, start, stop in fields(data, pos): - if key == 'meta': - meta = {} - route_present = False - for name, a, b in fields(data, start): - if name == 'route_api_method_present': - route_present = data[a:b] == b'true' - if name in META_FIELDS and (preview or name in INVENTORY_FIELDS) and b - a <= 256 * 1024: - meta[name] = json.loads(data[a:b]) - if not route_present: - meta.pop('route_api_method', None) - session.update(meta) - elif key == 'append_messages' and preview: - recent_messages(data, start, stop, messages) - pos = end + for key, start, stop in journal_fields(data): + if key == 'meta': + meta = {} + route_present = False + for name, a, b in fields(data, start): + if name == 'route_api_method_present': + route_present = data[a:b] == b'true' + if name in META_FIELDS and (preview or name in INVENTORY_FIELDS) and b - a <= 256 * 1024: + meta[name] = json.loads(data[a:b]) + if not route_present: + meta.pop('route_api_method', None) + session.update(meta) + elif key == 'append_messages' and preview: + recent_messages(data, start, stop, messages) if preview: session['messages'] = list(messages) for key in ('env_snapshots', 'memory_injections', 'replay_events'): From 8c3cf2b7377f1b43b42bdfaf8ce5f67cb9c039f3 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:43:08 -0800 Subject: [PATCH 051/236] Document per-operation SSH latency interpretation --- docs/CLOUD_LATENCY.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index 1a92aac169f..0de766343d8 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -109,5 +109,12 @@ demonstrate reuse, but its elapsed times are not production API measurements. A high total alone is not proof of the cause. Compare cache hits/misses, success versus failure, cold versus warm connections and local versus daemon stage timing. +`workspace_command` records distinguish `git_probe`, `inventory_prepare`, +`inventory_projection`, `preview`, `cloud_status`, and `cloud_status_directories`. +Older `operation=none` samples cannot reliably identify which subprocess was slow. +These operations share transport code but have different workloads and deadlines, +so compare their distributions separately rather than attributing every slow SSH +command to inventory. The report counts HTTP/read/load errors, output bounds, +identity mismatches, and nonzero exits as failures, not normal cache misses/skips. Do not claim a cold VM wake or a full end-to-end cloud resume is subsecond just because one stage or a synthetic fixture is subsecond. From cb16698eb90ef4a54c8beb7dfe1fc86f40800ed3 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:51:50 -0800 Subject: [PATCH 052/236] Reuse shared mobile push HTTP transport and record safe timings --- crates/jcode-app-core/src/mobile_push.rs | 92 ++++++-- .../src/mobile_push_http_tests.rs | 211 ++++++++++++++++++ 2 files changed, 290 insertions(+), 13 deletions(-) create mode 100644 crates/jcode-app-core/src/mobile_push_http_tests.rs diff --git a/crates/jcode-app-core/src/mobile_push.rs b/crates/jcode-app-core/src/mobile_push.rs index 65f17f258da..feb78e08b17 100644 --- a/crates/jcode-app-core/src/mobile_push.rs +++ b/crates/jcode-app-core/src/mobile_push.rs @@ -17,6 +17,38 @@ pub const MIN_TURN_DURATION_FOR_PUSH: Duration = Duration::from_secs(20); /// Back-off after the API reports no registered devices. pub const NO_DEVICES_SUPPRESS: Duration = Duration::from_secs(10 * 60); const SEND_TIMEOUT: Duration = Duration::from_secs(5); + +/// Timing labels are deliberately static, never payloads, URLs or errors. +struct PushTiming { + stage: &'static str, + outcome: &'static str, + start: Instant, +} + +impl PushTiming { + fn new(stage: &'static str) -> Self { + Self { + stage, + outcome: "cancelled", + start: Instant::now(), + } + } + + fn finish(mut self, outcome: &'static str) { + self.outcome = outcome; + } +} + +impl Drop for PushTiming { + fn drop(&mut self) { + crate::logging::info(&format!( + "[CLOUD_TIMING] operation=mobile_push stage={} outcome={} duration_ms={}", + self.stage, + self.outcome, + self.start.elapsed().as_millis() + )); + } +} const BODY_MAX_CHARS: usize = 180; pub const OPT_OUT_ENV: &str = "JCODE_MOBILE_PUSH"; @@ -660,15 +692,31 @@ async fn send_mobile_resume( base: &str, payload: &PushPayload, ) -> anyhow::Result { - let response = reqwest::Client::new() + let total = PushTiming::new("total"); + let result = send_mobile_resume_inner(key, base, payload).await; + total.finish(if result.is_ok() { "delivered" } else { "error" }); + result +} + +async fn send_mobile_resume_inner( + key: &str, + base: &str, + payload: &PushPayload, +) -> anyhow::Result { + let http = PushTiming::new("http_send"); + let result = crate::provider::shared_http_client() .post(format!("{base}/push/send")) .bearer_auth(key) .json(&payload) .timeout(SEND_TIMEOUT) .send() - .await? - .error_for_status()?; - let reply = response.json::().await?; + .await; + http.finish(if result.is_ok() { "response" } else { "error" }); + let response = result?.error_for_status()?; + let body = PushTiming::new("body"); + let result = response.json::().await; + body.finish(if result.is_ok() { "decoded" } else { "error" }); + let reply = result?; match (reply.devices, reply.sent) { (Some(0), _) => anyhow::bail!( "No mobile devices registered. Open the Jcode app, sign in to the same account, and enable notifications, then retry /mobile." @@ -687,22 +735,33 @@ async fn send( base: &str, payload: &PushPayload, ) -> (&'static str, Option, Option) { - let client = match reqwest::Client::builder().timeout(SEND_TIMEOUT).build() { - Ok(c) => c, - Err(error) => { - return ("error", Some(format!("client build failed: {error}")), None); - } - }; - let result = client + let total = PushTiming::new("total"); + let result = send_inner(key, base, payload).await; + total.finish(result.0); + result +} + +async fn send_inner( + key: &str, + base: &str, + payload: &PushPayload, +) -> (&'static str, Option, Option) { + let http = PushTiming::new("http_send"); + let result = crate::provider::shared_http_client() .post(format!("{base}/push/send")) .bearer_auth(key) .json(payload) + .timeout(SEND_TIMEOUT) .send() .await; + http.finish(if result.is_ok() { "response" } else { "error" }); match result { Ok(resp) if resp.status().is_success() => { let status = resp.status().as_u16(); - let reply = match resp.json::().await { + let body = PushTiming::new("body"); + let result = resp.json::().await; + body.finish(if result.is_ok() { "decoded" } else { "error" }); + let reply = match result { Ok(reply) => reply, Err(error) => { return ( @@ -727,7 +786,10 @@ async fn send( Ok(resp) => { let status = resp.status().as_u16(); // Keep the API's error body: it says exactly which field was rejected. - let body = resp.text().await.unwrap_or_default(); + let timing = PushTiming::new("body"); + let result = resp.text().await; + timing.finish(if result.is_ok() { "read" } else { "error" }); + let body = result.unwrap_or_default(); crate::logging::warn(&format!( "mobile push {:?} rejected: HTTP {status}: {}", payload.kind, @@ -743,6 +805,10 @@ async fn send( } } +#[cfg(test)] +#[path = "mobile_push_http_tests.rs"] +mod http_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/jcode-app-core/src/mobile_push_http_tests.rs b/crates/jcode-app-core/src/mobile_push_http_tests.rs new file mode 100644 index 00000000000..25faa09b235 --- /dev/null +++ b/crates/jcode-app-core/src/mobile_push_http_tests.rs @@ -0,0 +1,211 @@ +use super::*; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +async fn fixture( + status: u16, + body: &'static str, + delay: Duration, +) -> (String, Arc, tokio::task::JoinHandle<()>) { + fixture_with_body_delay(status, body, delay, Duration::ZERO).await +} + +async fn fixture_with_body_delay( + status: u16, + body: &'static str, + delay: Duration, + body_delay: Duration, +) -> (String, Arc, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let connections = Arc::new(AtomicUsize::new(0)); + let count = connections.clone(); + let task = tokio::spawn(async move { + loop { + let (mut socket, _) = listener.accept().await.unwrap(); + socket.set_nodelay(true).unwrap(); + count.fetch_add(1, Ordering::SeqCst); + tokio::spawn(async move { + // Artificial connection setup cost, not an APNs latency model. + tokio::time::sleep(delay).await; + loop { + let mut request = Vec::new(); + let mut byte = [0]; + while !request.ends_with(b"\r\n\r\n") { + if socket.read_exact(&mut byte).await.is_err() { + return; + } + request.push(byte[0]); + } + let headers = String::from_utf8(request).unwrap(); + assert!(headers.starts_with("POST /push/send HTTP/1.1\r\n")); + assert!( + headers + .to_lowercase() + .contains("authorization: bearer fixture-key\r\n") + ); + let length: usize = headers + .lines() + .find_map(|line| { + line.to_lowercase() + .strip_prefix("content-length: ") + .map(str::to_owned) + }) + .unwrap() + .parse() + .unwrap(); + let mut bytes = vec![0; length]; + socket.read_exact(&mut bytes).await.unwrap(); + let actual: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(actual, serde_json::to_value(payload()).unwrap()); + let response = format!( + "HTTP/1.1 {status} Fixture\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n", + body.len() + ); + if socket.write_all(response.as_bytes()).await.is_err() { + return; + } + tokio::time::sleep(body_delay).await; + if socket.write_all(body.as_bytes()).await.is_err() { + return; + } + } + }); + } + }); + (base, connections, task) +} + +fn payload() -> PushPayload { + build_payload( + PushKind::Wake, + "fixture-session", + Some("Fixture"), + "Fixture body", + Some(42), + ) +} + +#[tokio::test] +async fn mobile_push_http_eight_push_connections() { + // Control mirrors the original per-notification client construction. + let (base, connections, task) = + fixture(200, r#"{"sent":1,"devices":1}"#, Duration::from_millis(60)).await; + let start = Instant::now(); + for _ in 0..8 { + let reply = reqwest::Client::builder() + .timeout(SEND_TIMEOUT) + .build() + .unwrap() + .post(format!("{base}/push/send")) + .bearer_auth("fixture-key") + .json(&payload()) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + assert_eq!((reply.sent, reply.devices), (Some(1), Some(1))); + } + eprintln!( + "artificial 60ms connection-delay fresh-client control: eight pushes elapsed_ms={} connections={}", + start.elapsed().as_millis(), + connections.load(Ordering::SeqCst) + ); + assert_eq!(connections.load(Ordering::SeqCst), 8); + task.abort(); + let (base, connections, task) = + fixture(200, r#"{"sent":1,"devices":1}"#, Duration::from_millis(60)).await; + let start = Instant::now(); + for _ in 0..8 { + let result = send("fixture-key", &base, &payload()).await; + assert_eq!( + result, + ( + "delivered", + Some("sent=Some(1) devices=Some(1)".into()), + Some(200) + ) + ); + } + eprintln!( + "artificial 60ms connection-delay fixture: eight pushes elapsed_ms={} connections={}", + start.elapsed().as_millis(), + connections.load(Ordering::SeqCst) + ); + assert_eq!(connections.load(Ordering::SeqCst), 1); + for _ in 0..8 { + assert!( + send_mobile_resume("fixture-key", &base, &payload()) + .await + .unwrap() + .contains("sent to 1 device") + ); + } + assert_eq!(connections.load(Ordering::SeqCst), 1); + task.abort(); +} + +#[tokio::test] +async fn mobile_push_http_reply_semantics() { + for (status, body, outcome) in [ + (200, r#"{"sent":0,"devices":0}"#, "no_devices"), + (200, r#"{"sent":0,"devices":1}"#, "undelivered"), + (200, "invalid", "error"), + (503, "fixture unavailable", "rejected"), + ] { + let (base, _, task) = fixture(status, body, Duration::ZERO).await; + let result = send("fixture-key", &base, &payload()).await; + assert_eq!(result.0, outcome); + assert_eq!(result.2, Some(status)); + task.abort(); + } +} + +#[tokio::test] +async fn mobile_push_http_deadline_and_cancellation() { + let (base, _, task) = fixture( + 200, + r#"{"sent":1,"devices":1}"#, + SEND_TIMEOUT + Duration::from_secs(1), + ) + .await; + let start = Instant::now(); + let result = send("fixture-key", &base, &payload()).await; + assert_eq!(result.0, "error"); + assert_eq!(result.2, None); + assert!(start.elapsed() >= SEND_TIMEOUT); + assert!(start.elapsed() < SEND_TIMEOUT + Duration::from_secs(1)); + let p = payload(); + let future = send("fixture-key", &base, &p); + assert!( + tokio::time::timeout(Duration::from_millis(20), future) + .await + .is_err() + ); + task.abort(); +} + +#[tokio::test] +async fn mobile_push_http_body_deadline() { + let (base, _, task) = fixture_with_body_delay( + 200, + r#"{"sent":1,"devices":1}"#, + Duration::ZERO, + SEND_TIMEOUT + Duration::from_secs(1), + ) + .await; + let p = payload(); + let (automatic, explicit) = tokio::join!( + send("fixture-key", &base, &p), + send_mobile_resume("fixture-key", &base, &p), + ); + assert_eq!(automatic.0, "error"); + assert_eq!(automatic.2, Some(200)); + assert!(explicit.is_err()); + task.abort(); +} From ae4c7af18193728c416960117dd51688537259ba Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:53:09 -0800 Subject: [PATCH 053/236] mobile push: per-kind emoji marker in notification title --- crates/jcode-app-core/src/mobile_push.rs | 31 +++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/jcode-app-core/src/mobile_push.rs b/crates/jcode-app-core/src/mobile_push.rs index feb78e08b17..badcd852ebc 100644 --- a/crates/jcode-app-core/src/mobile_push.rs +++ b/crates/jcode-app-core/src/mobile_push.rs @@ -108,6 +108,13 @@ pub fn build_payload( } else { truncate_words(&collapsed, BODY_MAX_CHARS) }; + // Lead the title with a per-kind marker so the kinds are told apart at a + // glance on the lock screen, not only by the small subtitle. + let title = Some(format!( + "{} {}", + kind_marker(kind), + title.as_deref().unwrap_or("jcode") + )); PushPayload { kind, session_id: session_id.to_string(), @@ -121,6 +128,15 @@ pub fn build_payload( const JS_SAFE_INT_MASK: u64 = (1 << 53) - 1; +pub fn kind_marker(kind: PushKind) -> &'static str { + match kind { + PushKind::TurnDone => "\u{2705}", // white check mark + PushKind::TurnError => "\u{274C}", // cross mark + PushKind::NeedsInput => "\u{2753}", // question mark + PushKind::Wake => "\u{23F0}", // alarm clock + } +} + fn lookup_session_title(session_id: &str) -> Option { if cfg!(test) || session_id.is_empty() { return None; @@ -918,7 +934,7 @@ mod tests { serde_json::json!({ "kind": "turn_done", "session_id": "session_x", - "session_title": "Fix login", + "session_title": "\u{2705} Fix login", "body": "Done. All tests pass", "turn_id": 42 }) @@ -930,7 +946,7 @@ mod tests { let p = build_payload(PushKind::NeedsInput, "s", Some(" "), "", None); let v = serde_json::to_value(&p).unwrap(); assert_eq!(v["kind"], "needs_input"); - assert!(v.get("session_title").is_none()); + assert_eq!(v["session_title"], "\u{2753} jcode"); assert!(v.get("turn_id").is_none()); assert_eq!(v["body"], "Waiting for your input"); let long = "a".repeat(500); @@ -963,7 +979,16 @@ mod tests { let answer = "## Summary\n\nFixed the **login** bug in `auth.rs`; all 12 tests pass.\n\n- detail one\n- detail two\n\n```rust\nfn x() {}\n```"; let p = build_payload(PushKind::TurnDone, "s", Some("Fix `login`"), answer, None); assert_eq!(p.body, "Fixed the login bug in auth.rs; all 12 tests pass."); - assert_eq!(p.session_title.as_deref(), Some("Fix login")); + assert_eq!(p.session_title.as_deref(), Some("\u{2705} Fix login")); + for (kind, body, marker) in [ + (PushKind::TurnDone, "done", "\u{2705}"), + (PushKind::TurnDone, "Should I push?", "\u{2753}"), + (PushKind::TurnError, "boom", "\u{274C}"), + (PushKind::Wake, "bash completed", "\u{23F0}"), + ] { + let t = build_payload(kind, "s", Some("T"), body, None).session_title.unwrap(); + assert_eq!(t, format!("{marker} T")); + } let bullets = "- Updated parser\n- Added tests\n- Shipped v2 to production servers today"; assert_eq!( build_payload(PushKind::TurnDone, "s", None, bullets, None).body, From 34225e3b7b02d498e4e0450e1d1860de3acdfbb1 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:53:51 -0800 Subject: [PATCH 054/236] cloud reload: accept a handed-off daemon as ready --- src/cli/cloud_runtime.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/cli/cloud_runtime.rs b/src/cli/cloud_runtime.rs index 08cc320f106..d29602e09f0 100644 --- a/src/cli/cloud_runtime.rs +++ b/src/cli/cloud_runtime.rs @@ -72,6 +72,19 @@ for i in $(seq 1 60); do exit 0 fi fi + # The old daemon may hand its sessions to a detached successor on stop. The + # unit then fails with "already running" while that successor serves. + if [ "$i" -ge 10 ] && [ -S "$XDG_RUNTIME_DIR/jcode.sock" ]; then + target=$(dirname "$(readlink -f /opt/jcode-cloud/jcode)") + for p in $(pgrep -u "$(id -u)" -f ' serve$'); do + exe=$(readlink -f /proc/$p/exe 2>/dev/null) + case "$exe" in "$target"/*) + systemctl --user reset-failed jcode-cloud.service 2>/dev/null + say "daemon ready outside systemd (pid $p, handed off): $(/opt/jcode-cloud/jcode --version 2>/dev/null)" + exit 0;; + esac + done + fi sleep 1 done say "daemon did not become ready; see: journalctl --user -u jcode-cloud.service" From 44abd164d961459ff2e6d6dc7ee610fcd796f621 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:54:09 -0800 Subject: [PATCH 055/236] Fix mobile runner stderr deadlock and supervise full child lifetime --- crates/jcode-base/src/mobile_runner.rs | 119 ++++----- .../jcode-base/src/mobile_runner_process.rs | 111 ++++++++ .../src/mobile_runner_process_tests.rs | 241 ++++++++++++++++++ 3 files changed, 407 insertions(+), 64 deletions(-) create mode 100644 crates/jcode-base/src/mobile_runner_process.rs create mode 100644 crates/jcode-base/src/mobile_runner_process_tests.rs diff --git a/crates/jcode-base/src/mobile_runner.rs b/crates/jcode-base/src/mobile_runner.rs index 558f5fd2a6f..2b376fcffb9 100644 --- a/crates/jcode-base/src/mobile_runner.rs +++ b/crates/jcode-base/src/mobile_runner.rs @@ -15,7 +15,33 @@ use serde::Deserialize; use std::path::PathBuf; use std::process::Stdio; use std::time::Duration; -use tokio::io::{AsyncBufReadExt, BufReader}; +#[path = "mobile_runner_process.rs"] +mod process; +#[cfg(test)] +#[path = "mobile_runner_process_tests.rs"] +mod process_tests; + +struct StageTimer { + stage: &'static str, + start: std::time::Instant, +} +impl StageTimer { + fn new(stage: &'static str) -> Self { + Self { + stage, + start: std::time::Instant::now(), + } + } +} +impl Drop for StageTimer { + fn drop(&mut self) { + crate::logging::info(&format!( + "mobile runner timing stage={} elapsed_ms={}", + self.stage, + self.start.elapsed().as_millis() + )); + } +} const POLL_INTERVAL: Duration = Duration::from_secs(5); const TURN_TIMEOUT: Duration = Duration::from_secs(40 * 60); @@ -183,6 +209,7 @@ async fn import_local_session( key: &str, id: &str, ) -> Result { + let _timing = StageTimer::new("transfer"); let (metadata, messages) = fetch_local_transcript(client, base, key, id).await?; let mut session = transfer_session(&metadata, messages, &workspace_dir())?; session.save()?; @@ -296,61 +323,15 @@ async fn run_turn(command: &MobileCommand, session_id: Option<&str>) -> Result match line? { - Some(line) => { - if session.is_none() { - session = session_id_from_ndjson_line(&line); - } - } - None => break, - }, - _ = touch.tick() => { - if let Some(m) = &marker - && let Err(error) = std::fs::write(m, b"") - { - crate::logging::warn(&format!("mobile runner busy marker update failed: {error}")); - } - } - _ = &mut deadline => { - if let Err(error) = child.kill().await { - crate::logging::warn(&format!("mobile runner timed-out child cleanup failed: {error}")); - } - anyhow::bail!("turn timed out"); - } - } - } - let output = child.wait_with_output().await?; - if let Some(m) = &marker - && let Err(error) = std::fs::remove_file(m) - && error.kind() != std::io::ErrorKind::NotFound - { - crate::logging::warn(&format!( - "mobile runner busy marker removal failed: {error}" - )); - } - if !output.status.success() { - let stderr = String::from_utf8_lossy(&output.stderr); - let tail: String = stderr - .chars() - .rev() - .take(600) - .collect::>() - .into_iter() - .rev() - .collect(); - anyhow::bail!("jcode run failed ({}): {}", output.status, tail.trim()); - } - session.context("jcode run did not report a session id") + let _timing = StageTimer::new("turn"); + process::run( + cmd, + session_id, + busy_marker(), + TURN_TIMEOUT, + BUSY_TOUCH_INTERVAL, + ) + .await } /// Claim and run at most one queued command. Returns whether one ran. @@ -360,13 +341,13 @@ pub async fn run_one(client: &reqwest::Client) -> Result { let base = subscription_api::configured_api_base() .trim_end_matches('/') .to_string(); - let Some(command) = claim(client, &base, &key).await? else { + let claimed = { + let _timing = StageTimer::new("claim"); + claim(client, &base, &key).await? + }; + let Some(command) = claimed else { return Ok(false); }; - crate::logging::info(&format!( - "mobile runner: {} command {}", - command.kind, command.command_id - )); let outcome = async { let session_id = match command.kind.as_str() { "transfer" => Some( @@ -393,10 +374,17 @@ pub async fn run_one(client: &reqwest::Client) -> Result { run_turn(&command, session_id.as_deref()).await } .await; - if let Err(error) = mobile_sync::sync_once(client).await { + let sync_result = { + let _timing = StageTimer::new("sync"); + mobile_sync::sync_once(client).await + }; + if let Err(error) = sync_result { crate::logging::warn(&format!("mobile runner transcript sync failed: {error:#}")); } - complete(client, &base, &key, &command.command_id, &outcome).await?; + { + let _timing = StageTimer::new("complete"); + complete(client, &base, &key, &command.command_id, &outcome).await?; + } outcome.map(|_| true) } @@ -420,7 +408,10 @@ pub fn spawn_cloud_runner() { tokio::spawn(async move { let client = reqwest::Client::new(); loop { - tokio::time::sleep(POLL_INTERVAL).await; + { + let _timing = StageTimer::new("pollwait"); + tokio::time::sleep(POLL_INTERVAL).await; + } if let Err(err) = run_one(&client).await { crate::logging::warn(&format!("mobile runner: {err:#}")); tokio::time::sleep(Duration::from_secs(20)).await; diff --git a/crates/jcode-base/src/mobile_runner_process.rs b/crates/jcode-base/src/mobile_runner_process.rs new file mode 100644 index 00000000000..d10b58cc1ef --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_process.rs @@ -0,0 +1,111 @@ +//! Structured child supervision. Both pipes and process exit share one deadline. +use anyhow::{Context, Result}; +use std::{path::PathBuf, time::Duration}; +use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader}; + +const STDERR_TAIL_BYTES: usize = 600 * 4 + 4; + +struct BusyMarker(Option); +impl BusyMarker { + fn touch(&self) { + if let Some(path) = &self.0 + && let Err(error) = std::fs::write(path, b"") + { + crate::logging::warn(&format!("mobile runner busy marker update failed: {error}")); + } + } +} +impl Drop for BusyMarker { + fn drop(&mut self) { + if let Some(path) = &self.0 + && let Err(error) = std::fs::remove_file(path) + && error.kind() != std::io::ErrorKind::NotFound + { + crate::logging::warn(&format!( + "mobile runner busy marker removal failed: {error}" + )); + } + } +} + +async fn stderr_tail(mut stderr: tokio::process::ChildStderr) -> Result { + let mut tail = Vec::with_capacity(STDERR_TAIL_BYTES); + let mut chunk = [0; 8192]; + loop { + let n = stderr.read(&mut chunk).await?; + if n == 0 { + break; + } + tail.extend_from_slice(&chunk[..n]); + if tail.len() > STDERR_TAIL_BYTES { + tail.drain(..tail.len() - STDERR_TAIL_BYTES); + } + } + // Keep enough raw bytes for 600 four-byte scalars, then trim by characters. + // Lossy decoding also preserves the former behavior for non-UTF-8 stderr. + let text = String::from_utf8_lossy(&tail); + Ok(text + .chars() + .rev() + .take(600) + .collect::>() + .into_iter() + .rev() + .collect()) +} + +pub(super) async fn run( + mut command: tokio::process::Command, + session_id: Option<&str>, + marker: Option, + timeout: Duration, + touch_interval: Duration, +) -> Result { + let marker = BusyMarker(marker); + command.kill_on_drop(true); + let mut child = command.spawn().context("starting jcode run")?; + let stdout = child.stdout.take().context("no stdout")?; + let stderr = child.stderr.take().context("no stderr")?; + marker.touch(); + let mut touch = tokio::time::interval(touch_interval); + let result = { + let output = async { + let read_stdout = async { + let mut session = session_id.map(str::to_owned); + let mut lines = BufReader::new(stdout).lines(); + while let Some(line) = lines.next_line().await? { + if session.is_none() { + session = super::session_id_from_ndjson_line(&line); + } + } + Ok::<_, anyhow::Error>(session) + }; + let (session, tail, status) = + tokio::try_join!(read_stdout, stderr_tail(stderr), async { + child.wait().await.context("waiting for jcode run") + },)?; + anyhow::ensure!( + status.success(), + "jcode run failed ({}): {}", + status, + tail.trim() + ); + session.context("jcode run did not report a session id") + }; + let deadline = tokio::time::timeout(timeout, output); + tokio::pin!(deadline); + loop { + tokio::select! { + result = &mut deadline => break result.unwrap_or_else(|_| Err(anyhow::anyhow!("turn timed out"))), + _ = touch.tick() => marker.touch(), + } + } + }; + // No spawned reader tasks: cancellation drops both readers before reaping. + if result.is_err() + && let Err(error) = child.kill().await + { + crate::logging::warn(&format!("mobile runner child cleanup failed: {error}")); + } + result +} diff --git a/crates/jcode-base/src/mobile_runner_process_tests.rs b/crates/jcode-base/src/mobile_runner_process_tests.rs new file mode 100644 index 00000000000..20e5b95869f --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_process_tests.rs @@ -0,0 +1,241 @@ +//! Only launches this test executable as an isolated synthetic child, never Jcode turns. +use super::*; +use std::io::Write; +use tokio::io::{AsyncBufReadExt, BufReader}; + +#[cfg(unix)] +#[tokio::test] +async fn cancelling_supervision_removes_marker_and_leaves_no_child() { + let temp = tempfile::tempdir().unwrap(); + let marker = temp.path().join("busy"); + let pid_file = temp.path().join("fixture-pid"); + { + let mut command = fixture("hang"); + command.env("JCODE_MOBILE_FIXTURE_PID_FILE", &pid_file); + let future = process::run( + command, + None, + Some(marker.clone()), + Duration::from_secs(5), + Duration::from_millis(20), + ); + tokio::pin!(future); + let ready = async { + while std::fs::read_to_string(&pid_file) + .ok() + .and_then(|s| s.parse::().ok()) + .is_none() + { + tokio::time::sleep(Duration::from_millis(10)).await; + } + }; + tokio::select! { + result = &mut future => panic!("fixture unexpectedly finished: {result:?}"), + result = tokio::time::timeout(Duration::from_secs(2), ready) => result.unwrap(), + } + assert!(marker.exists()); + // Dropping supervision cancels both inline reader futures and kills the child. + } + assert!(!marker.exists()); + let pid: libc::pid_t = std::fs::read_to_string(pid_file).unwrap().parse().unwrap(); + tokio::time::timeout(Duration::from_secs(2), async { + loop { + // SAFETY: signal zero observes only the isolated fixture process. + if unsafe { libc::kill(pid, 0) } == -1 { + assert_eq!( + std::io::Error::last_os_error().raw_os_error(), + Some(libc::ESRCH) + ); + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("cancelled fixture must be killed and reaped"); +} + +#[test] +#[ignore = "subprocess fixture, invoked only by process regression tests"] +fn fake_child() { + let Ok(mode) = std::env::var("JCODE_MOBILE_PROCESS_FIXTURE") else { + return; + }; + if let Some(path) = std::env::var_os("JCODE_MOBILE_FIXTURE_PID_FILE") { + std::fs::write(path, std::process::id().to_string()).unwrap(); + } + if mode == "hang" || mode == "eof" { + if mode == "eof" { + // This fixture is its own process, so closing its pipe descriptors cannot + // affect the parent test harness. Keep running after both pipe EOFs. + #[cfg(unix)] + unsafe { + libc::close(1); + libc::close(2); + } + } + std::thread::sleep(Duration::from_secs(30)); + return; + } + let mut stderr = std::io::stderr().lock(); + stderr.write_all("🦀界".repeat(200_000).as_bytes()).unwrap(); + if mode == "error" { + stderr.write_all("界🦀".repeat(300).as_bytes()).unwrap(); + stderr.flush().unwrap(); + std::process::exit(7); + } + drop(stderr); + if mode != "missing" { + println!("not-json"); + println!("{{\"type\":\"start\",\"session_id\":\"synthetic-session\"}}"); + } +} + +fn fixture(mode: &str) -> tokio::process::Command { + let mut command = tokio::process::Command::new(std::env::current_exe().unwrap()); + command + .args([ + "--exact", + "mobile_runner::process_tests::fake_child", + "--ignored", + "--nocapture", + ]) + .env("JCODE_MOBILE_PROCESS_FIXTURE", mode) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .kill_on_drop(true); + command +} + +#[tokio::test] +async fn sequential_baseline_times_out_but_concurrent_drain_completes() { + let mut child = fixture("success").spawn().unwrap(); + let stdout = child.stdout.take().unwrap(); + let baseline = std::time::Instant::now(); + let result = tokio::time::timeout(Duration::from_millis(300), async { + let mut lines = BufReader::new(stdout).lines(); + while lines.next_line().await.unwrap().is_some() {} + }) + .await; + assert!( + result.is_err(), + "undrained stderr must block before stdout EOF" + ); + let baseline_elapsed = baseline.elapsed(); + child.kill().await.unwrap(); + let fixed = std::time::Instant::now(); + let session = process::run( + fixture("success"), + None, + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap(); + assert_eq!(session, "synthetic-session"); + eprintln!( + "synthetic child: sequential baseline timeout={}ms, concurrent completion={}ms", + baseline_elapsed.as_millis(), + fixed.elapsed().as_millis() + ); +} + +#[tokio::test] +async fn unicode_error_is_exactly_last_600_characters() { + let error = process::run( + fixture("error"), + None, + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap_err() + .to_string(); + let (_, tail) = error.split_once("): ").unwrap(); + assert_eq!(tail, "界🦀".repeat(300)); + assert_eq!(tail.chars().count(), 600); +} + +#[tokio::test] +async fn resumed_session_wins_and_missing_session_is_an_error() { + let result = process::run( + fixture("success"), + Some("resumed"), + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap(); + assert_eq!(result, "resumed"); + let error = process::run( + fixture("missing"), + None, + None, + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await + .unwrap_err(); + assert!(error.to_string().contains("did not report a session id")); +} + +#[tokio::test] +async fn busy_marker_touched_and_removed_on_timeout_including_after_stdout_eof() { + for mode in ["hang", "eof"] { + if mode == "eof" && !cfg!(unix) { + continue; + } + let temp = tempfile::tempdir().unwrap(); + let marker = temp.path().join("busy"); + let future = process::run( + fixture(mode), + None, + Some(marker.clone()), + Duration::from_millis(250), + Duration::from_millis(20), + ); + tokio::pin!(future); + tokio::select! { + result = &mut future => panic!("finished before marker observation: {result:?}"), + _ = tokio::time::sleep(Duration::from_millis(80)) => {} + } + assert!(marker.exists()); + // Removing it verifies that a later heartbeat recreates it. + std::fs::remove_file(&marker).unwrap(); + tokio::select! { + result = &mut future => panic!("finished before heartbeat observation: {result:?}"), + _ = tokio::time::sleep(Duration::from_millis(60)) => {} + } + assert!(marker.exists()); + assert!( + future + .await + .unwrap_err() + .to_string() + .contains("turn timed out") + ); + assert!(!marker.exists()); + } +} + +#[tokio::test] +async fn busy_marker_removed_on_success_and_failure() { + for mode in ["success", "error", "missing"] { + let temp = tempfile::tempdir().unwrap(); + let marker = temp.path().join("busy"); + let result = process::run( + fixture(mode), + None, + Some(marker.clone()), + Duration::from_secs(5), + Duration::from_millis(20), + ) + .await; + assert_eq!(result.is_ok(), mode == "success"); + assert!(!marker.exists()); + } +} From b4da0774509b9806e1eeaa163bd5e48c131ffb25 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:56:35 -0800 Subject: [PATCH 056/236] Optimize authoritative cloud directory session metadata scans --- .../src/tui/cloud_directories_probe.py | 231 +++++++++++++----- .../src/tui/cloud_directories_probe_test.py | 194 +++++++++++++++ .../src/tui/cloud_directories_probe_tests.rs | 23 ++ crates/jcode-tui/src/tui/cloud_status.rs | 4 + 4 files changed, 387 insertions(+), 65 deletions(-) create mode 100644 crates/jcode-tui/src/tui/cloud_directories_probe_test.py create mode 100644 crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs diff --git a/crates/jcode-tui/src/tui/cloud_directories_probe.py b/crates/jcode-tui/src/tui/cloud_directories_probe.py index 663cf43c1a7..56b1b5c5a1d 100644 --- a/crates/jcode-tui/src/tui/cloud_directories_probe.py +++ b/crates/jcode-tui/src/tui/cloud_directories_probe.py @@ -14,10 +14,144 @@ MAX_ITEMS = 4096 MAX_JSON_BYTES = 32 * 1024 * 1024 -# JSON strings are skipped in C, including escaped quotes and embedded tool JSON. -# The parser only decodes top-level scalar metadata, never transcript contents. -TOKENS = re.compile(rb'"[^"\\]*(?:\\.[^"\\]*)*"|[{}\[\],:]|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?') FIELDS = {'working_dir', 'is_debug', 'is_canary'} +# Validate skipped strings, including UTF-8, without materializing their text. +UTF8 = (rb'[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|' + rb'[\xe1-\xec\xee-\xef][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|' + rb'\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|' + rb'\xf4[\x80-\x8f][\x80-\xbf]{2}') +ASCII = rb'[\x20-\x21\x23-\x5b\x5d-\x7f]*' +STRING = rb'"' + ASCII + rb'(?:(?:\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})|' + UTF8 + rb')' + ASCII + rb')*"' +WS = rb'[ \t\r\n]*' +SCALAR = STRING + rb'|true|false|null|-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?' + + +def value_pattern(depth): + scalar = rb'(?:' + SCALAR + rb')' + if not depth: + return scalar + child = value_pattern(depth - 1) + pair = STRING + WS + b':' + WS + child + return (rb'(?:' + scalar + rb'|\[' + WS + rb'(?:' + child + WS + + rb'(?:,' + WS + child + WS + rb')*)?\]|\{' + WS + rb'(?:' + pair + + WS + rb'(?:,' + WS + pair + WS + rb')*)?\})') + + +# A shallow complete value (typically one transcript message) is validated in C. +# Limit each match's window so an arbitrarily large vector cannot monopolize the +# deadline or allocate regex backtracking state proportional to the transcript. +VALUE = re.compile(value_pattern(3)) +ASCII_RUN = re.compile(ASCII) +ESCAPE = re.compile(rb'\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})') +UTF8_TOKEN = re.compile(UTF8) +SPACE = re.compile(WS) +SCAN_WINDOW = 64 * 1024 + + +def metadata(data, check_time, journal=False): + size = len(data) + + def space(pos): + while True: + limit = min(size, pos + SCAN_WINDOW) + pos = SPACE.match(data, pos, limit).end() + if pos < limit or pos == size: + return pos + check_time() + + def string(pos): + if data[pos:pos + 1] != b'"': + raise ValueError('invalid JSON string') + pos += 1 + while True: + check_time() + limit = min(size, pos + SCAN_WINDOW) + pos = ASCII_RUN.match(data, pos, limit).end() + if pos == limit and pos < size: + continue + first = data[pos:pos + 1] + if first == b'"': + return pos + 1 + token = (ESCAPE if first == b'\\' else UTF8_TOKEN).match(data, pos) + if token is None: + raise ValueError('invalid JSON string') + pos = token.end() + + def skip(pos, depth=0): + check_time() + if depth > 128: + raise ValueError('session nesting limit') + token = VALUE.match(data, pos, min(size, pos + SCAN_WINDOW)) + if token is not None: + return token.end() + first = data[pos:pos + 1] + if first == b'"': + end = string(pos) + check_time() + return end + if first not in (b'{', b'['): + raise ValueError('invalid JSON value') + closing = b'}' if first == b'{' else b']' + pos = space(pos + 1) + if data[pos:pos + 1] == closing: + return pos + 1 + while True: + if first == b'{': + pos = space(string(pos)) + if data[pos:pos + 1] != b':': + raise ValueError('missing JSON colon') + pos = space(pos + 1) + pos = space(skip(pos, depth + 1)) + if data[pos:pos + 1] == closing: + return pos + 1 + if data[pos:pos + 1] != b',': + raise ValueError('missing JSON separator') + pos = space(pos + 1) + + def obj(pos, wanted): + if data[pos:pos + 1] != b'{': + raise ValueError('session must be an object') + result = {} + pos = space(pos + 1) + if data[pos:pos + 1] == b'}': + return result, pos + 1 + while True: + check_time() + end = string(pos) + # Unrecognized long keys need validation, not decoding/allocation. + key = json.loads(data[pos:end]) if end - pos <= 1024 else None + pos = space(end) + if data[pos:pos + 1] != b':': + raise ValueError('missing JSON colon') + pos = space(pos + 1) + if journal and wanted == {'meta'} and key == 'meta' and data[pos:pos + 1] == b'{': + result[key], pos = obj(pos, FIELDS) + else: + end = skip(pos) + if key in wanted: + if journal and wanted == {'meta'}: + # Only object-valued meta participates in journal replay. + result[key] = None + else: + if not journal and end - pos > 16384: + raise ValueError('oversized session field') + # Containers must replace a prior duplicate scalar, + # matching JSON's last-key-wins semantics. + result[key] = json.loads(data[pos:end]) + pos = end + pos = space(pos) + if data[pos:pos + 1] == b'}': + return result, pos + 1 + if data[pos:pos + 1] != b',': + raise ValueError('missing JSON separator') + pos = space(pos + 1) + + check_time() + result, end = obj(space(0), {'meta'} if journal else FIELDS) + if space(end) != size: + raise ValueError('trailing session data') + check_time() + return result def session_fields(path, check_time): @@ -25,24 +159,35 @@ def session_fields(path, check_time): # complete record is authoritative, exactly as load_startup_stub handles it. journal = path.with_suffix('.journal.jsonl') try: - latest = None with open(journal, 'rb') as stream: - while True: - check_time() - line = stream.readline(MAX_JSON_BYTES + 1) - if not line: - break - if len(line) > MAX_JSON_BYTES: - raise ValueError('oversized journal record') - try: - entry = json.loads(line) - meta = entry.get('meta') - if isinstance(meta, dict): - latest = {key: meta.get(key) for key in FIELDS} - except (ValueError, AttributeError): - continue # tolerate a partial final append, like journal replay - if latest is not None: - return latest + if os.fstat(stream.fileno()).st_size: + with mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ) as data: + # Check every record's byte bound, including superseded ones. + # Only the newest valid metadata record needs JSON parsing. + start = cursor = 0 + while cursor < len(data): + check_time() + limit = min(len(data), cursor + SCAN_WINDOW) + newline = data.find(b'\n', cursor, limit) + end = newline + 1 if newline >= 0 else limit + if end - start > MAX_JSON_BYTES: + raise ValueError('oversized journal record') + if newline >= 0: + start = end + cursor = end + end = len(data) + while end: + check_time() + # Include the newline in the same bound as readline. + start = data.rfind(b'\n', 0, end - 1) + 1 + try: + entry = metadata(data[start:end], check_time, journal=True) + meta = entry.get('meta') + if isinstance(meta, dict): + return {key: meta.get(key) for key in FIELDS} + except (ValueError, AttributeError): + pass # tolerate partial/malformed appends, like replay + end = start except FileNotFoundError: pass @@ -50,51 +195,7 @@ def session_fields(path, check_time): if os.fstat(stream.fileno()).st_size == 0: raise ValueError('empty session snapshot') with mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ) as data: - stack = [] - result = {} - key = None - expect_key = False - expect_value = False - end = 0 - for index, token in enumerate(TOKENS.finditer(data)): - if index % 256 == 0: - check_time() - if data[end:token.start()].strip(): - raise ValueError('invalid session snapshot') - end = token.end() - first = data[token.start():token.start() + 1] - if first in (b'{', b'['): - stack.append(first) - if len(stack) == 1: - if first != b'{': - raise ValueError('session must be an object') - expect_key = True - expect_value = False - elif first in (b'}', b']'): - if not stack or stack.pop() != (b'{' if first == b'}' else b'['): - raise ValueError('unbalanced session snapshot') - if not stack: - break - elif len(stack) == 1: - if first == b',': - expect_key = True - elif first == b':': - expect_value = True - elif expect_key: - if first != b'"' or token.end() - token.start() > 1024: - raise ValueError('invalid session field') - key = json.loads(token.group()) - expect_key = False - elif expect_value: - if key in FIELDS: - if token.end() - token.start() > 16384: - raise ValueError('oversized session field') - result[key] = json.loads(token.group()) - expect_value = False - check_time() - if stack or data[end:].strip(): - raise ValueError('incomplete session snapshot') - return result + return metadata(data, check_time) def collect(current, home=None, state=None, alive=None, current_session_id=None): diff --git a/crates/jcode-tui/src/tui/cloud_directories_probe_test.py b/crates/jcode-tui/src/tui/cloud_directories_probe_test.py new file mode 100644 index 00000000000..49db67053ac --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_directories_probe_test.py @@ -0,0 +1,194 @@ +"""Focused, synthetic-only checks. Run: python3 cloud_directories_probe_test.py. + +--benchmark BASELINE_PATH compares identical frozen synthetic files, never user state. +""" +import importlib.util +import json +import pathlib +import sys +import tempfile +import time +import unittest +from unittest import mock + + +def load(path): + spec = importlib.util.spec_from_file_location('probe', path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +probe = load(pathlib.Path(__file__).with_name('cloud_directories_probe.py')) + + +class ProbeTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.root = pathlib.Path(self.tmp.name) + self.path = self.root / 'session.json' + + def fields(self, raw): + self.path.write_bytes(raw) + return probe.session_fields(self.path, lambda: None) + + def test_duplicate_escaped_metadata(self): + self.assertEqual(self.fields(br'{"working_dir":"/old","messages":[{"working_dir":"/fake"}],"working\u005fdir":"/new","is_debug":false,"is_canary":true}'), + dict(working_dir='/new', is_debug=False, is_canary=True)) + + def test_container_duplicate_clears_scalar(self): + result = self.fields(b'{"working_dir":"/old","working_dir":{},"is_debug":[]}') + self.assertNotEqual(result.get('working_dir'), '/old') + self.assertIn('is_debug', result) + + def test_invalid_snapshots(self): + for raw in (b'', b'[]', b'{"messages":[1 2]}', b'{"messages":[1,]}', + b'{"a":true "b":false}', b'{"a":01}', b'{"a":"\\q"}', + b'{"a":1}junk', b'{"a":{"b":}}', b'{"a":true,}', + b'{"messages":["\xff"]}'): + with self.subTest(raw=raw), self.assertRaises(ValueError): + self.fields(raw) + + def test_latest_complete_journal_and_partial(self): + self.path.write_bytes(b'{"working_dir":"/snapshot"}') + journal = self.path.with_suffix('.journal.jsonl') + journal.write_bytes(b'{"meta":{"working_dir":"/old"}}\n' + b'{"messages":[1],"meta":{"working_dir":"/new","is_debug":true}}\n' + b'{"meta":{"working_dir":"/partial"}') + self.assertEqual(probe.session_fields(self.path, lambda: None), + dict(working_dir='/new', is_debug=True, is_canary=None)) + journal.write_bytes(b'{bad}\n{"meta":null}\n') + self.assertEqual(probe.session_fields(self.path, lambda: None), {'working_dir': '/snapshot'}) + + def test_duplicate_meta_and_invalid_journal(self): + self.path.write_bytes(b'{"working_dir":"/snapshot"}') + self.path.with_suffix('.journal.jsonl').write_bytes( + b'{"meta":{"working_dir":"/old"},"meta":{"working_dir":"/last"}}\n' + b'{"meta":{"working_dir":"/bad"},"messages":[1 2]}\n') + self.assertEqual(probe.session_fields(self.path, lambda: None)['working_dir'], '/last') + + def test_deadline(self): + self.path.write_bytes(b'{"messages":[] }') + def expired(): + raise TimeoutError('synthetic deadline') + with self.assertRaises(TimeoutError): + probe.session_fields(self.path, expired) + + def test_long_strings_unicode_nested_values_and_no_transcript_decoding(self): + transcript = 'TRANSCRIPT_SENTINEL' + ('abc\\\"[]{}' * 20000) + '\U0001f98a\u00e9' + raw = json.dumps({'messages': [{'content': transcript}], + 'working_dir': '/exact/sub', 'is_debug': False}, + ensure_ascii=False).encode() + original = json.loads + def scalar_only(value): + self.assertNotIn(b'TRANSCRIPT_SENTINEL', bytes(value)) + return original(value) + with mock.patch.object(probe.json, 'loads', side_effect=scalar_only): + self.assertEqual(self.fields(raw), dict(working_dir='/exact/sub', is_debug=False)) + self.path.with_suffix('.journal.jsonl').write_bytes(json.dumps( + {'messages': [{'content': transcript}], 'meta': {'working_dir': '/journal/sub'}}, + ensure_ascii=False).encode() + b'\n{"meta":') + self.assertEqual(probe.session_fields(self.path, lambda: None), + dict(working_dir='/journal/sub', is_debug=None, is_canary=None)) + self.path.with_suffix('.journal.jsonl').unlink() + for length in (probe.SCAN_WINDOW - 1, probe.SCAN_WINDOW, probe.SCAN_WINDOW + 1): + self.assertEqual(self.fields(b'{"ignored":"' + b'a' * length + b'","working_dir":"/ok"}'), + {'working_dir': '/ok'}) + for offset in (-2, -1, 0, 1): + for tail in ('\U0001f98a"\\tail', '\\"\u00e9'): + raw = json.dumps({'ignored': 'a' * (probe.SCAN_WINDOW + offset) + tail, + 'working_dir': '/boundary'}, ensure_ascii=False).encode() + self.assertEqual(self.fields(raw), {'working_dir': '/boundary'}) + nested = 20 * b'[' + b'"ok"' + 20 * b']' + self.assertEqual(self.fields(b'{"ignored":' + nested + b',"working_dir":"/ok"}'), {'working_dir': '/ok'}) + with self.assertRaises(ValueError): + self.fields(b'{"ignored":' + 140 * b'[' + b'0' + 140 * b']' + b'}') + + def test_checkpoint_inside_large_string_and_whitespace(self): + for raw in (b'{"messages":["' + b'x' * (probe.SCAN_WINDOW * 8) + b'"]}', + b' ' * (probe.SCAN_WINDOW * 8) + b'{}'): + self.path.write_bytes(raw) + calls = 0 + def expire_during_scan(): + nonlocal calls + calls += 1 + if calls == 6: + raise TimeoutError('bounded checkpoint') + with self.assertRaises(TimeoutError): + probe.session_fields(self.path, expire_during_scan) + + def test_journal_bounds_include_older_records_and_newlines(self): + self.path.write_bytes(b'{"working_dir":"/snapshot"}') + journal = self.path.with_suffix('.journal.jsonl') + with mock.patch.object(probe, 'MAX_JSON_BYTES', 40): + for raw in (b' ' * 41 + b'\n{"meta":{}}\n', b' ' * 40 + b'\n', b'x' * 41): + journal.write_bytes(raw) + with self.subTest(raw=raw), self.assertRaises(ValueError): + probe.session_fields(self.path, lambda: None) + journal.write_bytes(b'{"meta":{"working_dir":"/first"}}\n{"meta":null}\n\n') + self.assertEqual(probe.session_fields(self.path, lambda: None)['working_dir'], '/first') + + def test_pending_session_and_current_metadata_fallback(self): + home = self.root / 'home' + home.mkdir() + state = self.root / 'state' + (state / 'active_pids').mkdir(parents=True) + (state / 'streaming_pids').mkdir() + (state / 'active_pids' / 'new').write_text('1') + (state / 'streaming_pids' / 'new').write_text('1') + result = probe.collect(str(home), home, state, alive=lambda _: True) + self.assertIn(dict(path='(directory pending)', agents=1, busy=1), result['entries']) + result = probe.collect(str(home), home, state, alive=lambda _: True, current_session_id='new') + self.assertEqual(result['entries'], [dict(path='~', agents=1, busy=1)]) + + def test_collect_retains_four_second_deadline(self): + with mock.patch.object(probe.time, 'monotonic', side_effect=[0, 4.000001]): + with self.assertRaises(TimeoutError): + probe.collect(str(self.root), self.root, self.root / 'synthetic-state') + + def test_collect_exact_counts_flags_and_alive(self): + home = self.root / 'home' + repo = home / 'repo' + sub = repo / 'sub' + sub.mkdir(parents=True) + (repo / '.git').mkdir() + state = self.root / 'state' + for name in ('sessions', 'active_pids', 'streaming_pids'): + (state / name).mkdir(parents=True) + for sid, pid, flags in [('idle', 1, {}), ('busy', 2, {}), ('debug', 3, {'is_debug': True}), + ('canary', 4, {'is_canary': True}), ('dead', 9, {})]: + (state / 'active_pids' / sid).write_text(str(pid)) + (state / 'sessions' / (sid + '.json')).write_text(json.dumps(dict(working_dir=str(sub), **flags))) + (state / 'streaming_pids' / 'busy').write_text('2') + result = probe.collect(str(sub), home, state, alive=lambda pid: pid != 9) + self.assertEqual(result, dict(current_path='~/repo', entries=[dict(path='~/repo', agents=2, busy=1)], truncated=False)) + + +def benchmark(baseline): + old = load(baseline) + with tempfile.TemporaryDirectory(prefix='jcode-probe-synthetic-') as temp: + path = pathlib.Path(temp) / 'session.json' + # Fixed transcript shape with nested tool payloads and escaped punctuation. + message = json.dumps({'role': 'assistant', 'content': [{'type': 'tool', 'text': 'hello \\\"[]{} metadata ' * 12}], 'usage': {'input': 42, 'output': 17}}, separators=(',', ':')).encode() + payload = b'{"working_dir":"/synthetic/exact/sub","is_debug":false,"is_canary":false,"messages":[' + b','.join([message] * 256000) + b']}' + path.write_bytes(payload) + journal = path.with_suffix('.journal.jsonl') + for kind in ('snapshot', 'journal'): + if kind == 'journal': + entry = b'{"meta":{"working_dir":"/synthetic/exact/sub","is_debug":false,"is_canary":false},"messages":[' + b','.join([message] * 16000) + b']}\n' + journal.write_bytes(entry * 16 + b'{"meta":') + results = [] + for label, module in [('before', old), ('after', probe)]: + started = time.perf_counter() + result = module.session_fields(path, lambda: None) + results.append(result) + print(json.dumps(dict(kind=kind, implementation=label, bytes=(path if kind == 'snapshot' else journal).stat().st_size, seconds=round(time.perf_counter()-started, 6), output=result), sort_keys=True)) + assert results[0] == results[1], 'before/after metadata mismatch' + + +if __name__ == '__main__': + if len(sys.argv) == 3 and sys.argv[1] == '--benchmark': + benchmark(sys.argv[2]) + else: + unittest.main() diff --git a/crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs b/crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs new file mode 100644 index 00000000000..43a6d70a9dd --- /dev/null +++ b/crates/jcode-tui/src/tui/cloud_directories_probe_tests.rs @@ -0,0 +1,23 @@ +//! Synthetic-only regression harness for the embedded directory probe. + +#[test] +fn cloud_directories_probe_metadata_regressions() { + let dir = tempfile::tempdir().expect("synthetic probe directory"); + let probe = dir.path().join("cloud_directories_probe.py"); + let tests = dir.path().join("cloud_directories_probe_test.py"); + std::fs::write(&probe, include_str!("cloud_directories_probe.py")) + .expect("write synthetic probe copy"); + std::fs::write(&tests, include_str!("cloud_directories_probe_test.py")) + .expect("write synthetic probe tests"); + let output = std::process::Command::new("python3") + .arg(&tests) + .env("PYTHONDONTWRITEBYTECODE", "1") + .output() + .expect("python3 is required for directory probe regression tests"); + assert!( + output.status.success(), + "directory probe regressions failed:\n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr), + ); +} diff --git a/crates/jcode-tui/src/tui/cloud_status.rs b/crates/jcode-tui/src/tui/cloud_status.rs index 16702fdbf1f..74a3bf7f34f 100644 --- a/crates/jcode-tui/src/tui/cloud_status.rs +++ b/crates/jcode-tui/src/tui/cloud_status.rs @@ -328,6 +328,10 @@ pub(crate) fn gather(_session_id: Option) -> Option { None } +#[cfg(test)] +#[path = "cloud_directories_probe_tests.rs"] +mod directories_probe_tests; + #[cfg(test)] mod tests { use super::*; From ec1b37986dd4b910a1c6d37d3fa1956dd1bef867 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:58:32 -0800 Subject: [PATCH 057/236] Align mobile runner timings with shared cloud latency report --- crates/jcode-base/src/mobile_runner.rs | 110 ++++++++++++++++++------- 1 file changed, 80 insertions(+), 30 deletions(-) diff --git a/crates/jcode-base/src/mobile_runner.rs b/crates/jcode-base/src/mobile_runner.rs index 2b376fcffb9..8f09f8a4045 100644 --- a/crates/jcode-base/src/mobile_runner.rs +++ b/crates/jcode-base/src/mobile_runner.rs @@ -23,26 +23,41 @@ mod process_tests; struct StageTimer { stage: &'static str, + outcome: &'static str, start: std::time::Instant, } impl StageTimer { fn new(stage: &'static str) -> Self { Self { stage, + outcome: "error_or_cancelled", start: std::time::Instant::now(), } } + + fn record(&self, duration_ms: u128) -> String { + format!( + "[CLOUD_TIMING] operation=mobile_runner stage={} outcome={} duration_ms={}", + self.stage, self.outcome, duration_ms + ) + } } impl Drop for StageTimer { fn drop(&mut self) { - crate::logging::info(&format!( - "mobile runner timing stage={} elapsed_ms={}", - self.stage, - self.start.elapsed().as_millis() - )); + crate::logging::info(&self.record(self.start.elapsed().as_millis())); } } +async fn timed_stage( + stage: &'static str, + future: impl std::future::Future>, +) -> Result { + let mut timing = StageTimer::new(stage); + let result = future.await; + timing.outcome = if result.is_ok() { "ok" } else { "error" }; + result +} + const POLL_INTERVAL: Duration = Duration::from_secs(5); const TURN_TIMEOUT: Duration = Duration::from_secs(40 * 60); const BUSY_TOUCH_INTERVAL: Duration = Duration::from_secs(60); @@ -209,11 +224,13 @@ async fn import_local_session( key: &str, id: &str, ) -> Result { - let _timing = StageTimer::new("transfer"); - let (metadata, messages) = fetch_local_transcript(client, base, key, id).await?; - let mut session = transfer_session(&metadata, messages, &workspace_dir())?; - session.save()?; - Ok(session.id) + timed_stage("transfer", async { + let (metadata, messages) = fetch_local_transcript(client, base, key, id).await?; + let mut session = transfer_session(&metadata, messages, &workspace_dir())?; + session.save()?; + Ok(session.id) + }) + .await } /// True on a managed Jcode Cloud machine. @@ -323,13 +340,15 @@ async fn run_turn(command: &MobileCommand, session_id: Option<&str>) -> Result Result { let base = subscription_api::configured_api_base() .trim_end_matches('/') .to_string(); - let claimed = { - let _timing = StageTimer::new("claim"); - claim(client, &base, &key).await? - }; + let claimed = timed_stage("claim", claim(client, &base, &key)).await?; let Some(command) = claimed else { return Ok(false); }; @@ -374,17 +390,15 @@ pub async fn run_one(client: &reqwest::Client) -> Result { run_turn(&command, session_id.as_deref()).await } .await; - let sync_result = { - let _timing = StageTimer::new("sync"); - mobile_sync::sync_once(client).await - }; + let sync_result = timed_stage("sync", mobile_sync::sync_once(client)).await; if let Err(error) = sync_result { crate::logging::warn(&format!("mobile runner transcript sync failed: {error:#}")); } - { - let _timing = StageTimer::new("complete"); - complete(client, &base, &key, &command.command_id, &outcome).await?; - } + timed_stage( + "complete", + complete(client, &base, &key, &command.command_id, &outcome), + ) + .await?; outcome.map(|_| true) } @@ -409,8 +423,9 @@ pub fn spawn_cloud_runner() { let client = reqwest::Client::new(); loop { { - let _timing = StageTimer::new("pollwait"); + let mut timing = StageTimer::new("pollwait"); tokio::time::sleep(POLL_INTERVAL).await; + timing.outcome = "ok"; } if let Err(err) = run_one(&client).await { crate::logging::warn(&format!("mobile runner: {err:#}")); @@ -424,6 +439,41 @@ pub fn spawn_cloud_runner() { mod tests { use super::*; + #[test] + fn stage_timing_uses_content_free_shared_report_fields() { + for stage in ["claim", "transfer", "turn", "sync", "complete", "pollwait"] { + let mut timing = StageTimer::new(stage); + assert_eq!( + timing.record(17), + format!( + "[CLOUD_TIMING] operation=mobile_runner stage={stage} outcome=error_or_cancelled duration_ms=17" + ) + ); + for outcome in ["ok", "error"] { + timing.outcome = outcome; + let record = timing.record(23); + assert_eq!( + record, + format!( + "[CLOUD_TIMING] operation=mobile_runner stage={stage} outcome={outcome} duration_ms=23" + ) + ); + // These are exactly the fixed-label fields consumed by cloud_latency_report. + assert_eq!(record.split_whitespace().count(), 5); + assert!(!record.contains("elapsed_ms=")); + } + } + } + + #[tokio::test] + async fn timed_stage_preserves_success_and_error_results() { + assert_eq!(timed_stage("claim", async { Ok(7) }).await.unwrap(), 7); + let error = timed_stage::<()>("claim", async { anyhow::bail!("synthetic failure") }) + .await + .unwrap_err(); + assert_eq!(error.to_string(), "synthetic failure"); + } + async fn transcript_server( pages: Vec, ) -> (String, tokio::task::JoinHandle>) { From d2813b4c52df1d3ef04b4798c02fb700a3f705c0 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:59:19 -0800 Subject: [PATCH 058/236] Document mobile latency measurements and count push delivery failures --- docs/CLOUD_LATENCY.md | 37 ++++++++++++++++++++++++++++ scripts/cloud_latency_report.py | 1 + scripts/test_cloud_latency_report.py | 4 +-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index 0de766343d8..d86892fe71f 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -118,3 +118,40 @@ command to inventory. The report counts HTTP/read/load errors, output bounds, identity mismatches, and nonzero exits as failures, not normal cache misses/skips. Do not claim a cold VM wake or a full end-to-end cloud resume is subsecond just because one stage or a synthetic fixture is subsecond. + +## Mobile and workspace follow-up measurements + +Read-only samples over an existing managed SSH master on November 8, 2026 +measured machine status at 6.4–7.1ms, directory status at 6.85–6.90ms (27 +entries), and the selected active repository's git probe at 179–226ms. These +did not reproduce the historical 8-second workspace outliers. They are remote +stage timings, not cold connection or end-to-end UI latency. + +The bounded authoritative directory scanner also has a worst-case synthetic +benchmark: an identical frozen 97MB snapshot fell from 3.575s to 1.190s, and +journal metadata scanning from 0.675s to 0.078s, with identical metadata. This +does not establish that scanning caused the observed live outliers. + +Mobile push now reuses the shared HTTP client while retaining its five-second +request deadline and delivery semantics. Eight loopback pushes with an +artificial 60ms connection setup delay improved from 622ms/eight connections +to 71ms/one connection. This is not an APNs delivery measurement. Static +`operation=mobile_push` timings cover total, HTTP send and body processing. +The report counts `rejected` and `undelivered` as failures, but not `no_devices`. + +Mobile command execution drains stdout and bounded stderr concurrently and +supervises the whole child lifetime. An isolated child emitting 1.4MB of +Unicode stderr previously hit the fixture's 301ms timeout and now completes +in 3ms. This demonstrates removal of a pipe deadlock, not faster model turns. +`operation=mobile_runner` timings cover claim, transfer, turn, sync, complete +and poll wait, including errors and cancellation. The five-second poll cadence +is unchanged. + +The separate `jcode-mobile` repository's phone transcript transport now pools +HTTP connections too (commit `a98b496`). Eight actual list calls in a loopback +fixture with 30ms connection setup and 8ms body delay improved from median +315.039ms/eight connections to 97.462ms/one connection. All 85 phone-core tests +passed. See that repository's `docs/MOBILE_SYNC_LATENCY.md` for reproduction, +account/origin isolation and timing boundaries. A new mobile app build and +installation are required before this source change affects a device. No live +phone, push service, or production API was used for these benchmarks. diff --git a/scripts/cloud_latency_report.py b/scripts/cloud_latency_report.py index 265fe36f47e..89d0ec93c44 100644 --- a/scripts/cloud_latency_report.py +++ b/scripts/cloud_latency_report.py @@ -18,6 +18,7 @@ "error", "failed", "dropped", "timeout", "cancelled", "expired", "error_or_cancelled", "http_error", "load_error", "read_error", "size_limit", "oversized", "identity_mismatch", "exit_failure", + "rejected", "undelivered", } diff --git a/scripts/test_cloud_latency_report.py b/scripts/test_cloud_latency_report.py index db5d0ec4142..9efe1cd6da0 100644 --- a/scripts/test_cloud_latency_report.py +++ b/scripts/test_cloud_latency_report.py @@ -54,8 +54,8 @@ def test_ignores_unrelated_invalid_and_nonfinite_values(self): self.assertFalse(groups) def test_counts_http_body_and_projection_failures_not_normal_skips(self): - bad = ["http_error", "load_error", "read_error", "size_limit", "oversized", "identity_mismatch", "exit_failure"] - normal = ["success", "miss", "skipped_absent", "skipped_disabled", "success_cache_unavailable"] + bad = ["http_error", "load_error", "read_error", "size_limit", "oversized", "identity_mismatch", "exit_failure", "rejected", "undelivered"] + normal = ["success", "miss", "skipped_absent", "skipped_disabled", "success_cache_unavailable", "no_devices"] groups, failures = reporter.collect([ f"[CLOUD_TIMING] stage=probe outcome={outcome} duration_ms=10" for outcome in bad + normal From e4eac5b8911880853b598d597655dd9e8e953787 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:02:40 -0800 Subject: [PATCH 059/236] feat(voice): expose lightweight shared Nari transport --- crates/jcode-voice/Cargo.toml | 25 +++++++++++++++---------- crates/jcode-voice/src/lib.rs | 26 ++++++++++++++++++++++++-- crates/jcode-voice/src/nari.rs | 21 ++++++++++++++++++--- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/crates/jcode-voice/Cargo.toml b/crates/jcode-voice/Cargo.toml index c349548972b..380f6f50c7a 100644 --- a/crates/jcode-voice/Cargo.toml +++ b/crates/jcode-voice/Cargo.toml @@ -15,16 +15,17 @@ name = "jcode_voice" path = "src/lib.rs" [dependencies] -jcode-base = { path = "../jcode-base", default-features = false } -anyhow = "1" +jcode-base = { path = "../jcode-base", default-features = false, optional = true } +jcode-provider-env = { path = "../jcode-provider-env" } +anyhow = { version = "1", optional = true } base64 = "0.22" futures = "0.3" -libc = "0.2" +libc = { version = "0.2", optional = true } regex = "1" -reqwest = { version = "0.12", default-features = false, features = ["json", "multipart", "stream", "rustls-tls", "rustls-tls-native-roots"] } -serde = { version = "1", features = ["derive"] } +reqwest = { version = "0.12", optional = true, default-features = false, features = ["json", "multipart", "stream", "rustls-tls", "rustls-tls-native-roots"] } +serde = { version = "1", optional = true, features = ["derive"] } serde_json = "1" -tokio = { version = "1", features = ["io-util", "macros", "net", "process", "rt-multi-thread", "sync", "time"] } +tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync", "time"] } tokio-tungstenite = { version = "0.24", default-features = false, features = ["connect", "rustls-tls-native-roots"] } rustls = { version = "0.23", default-features = false, features = ["aws_lc_rs"] } @@ -33,14 +34,18 @@ cpal = { version = "0.15", optional = true } [target.'cfg(target_os = "linux")'.dependencies] # logind session check for background microphone use. -zbus = "5.19" -async-io = "2.6" +zbus = { version = "5.19", optional = true } +async-io = { version = "2.6", optional = true } [features] -voice-capture = ["dep:cpal"] +default = ["desktop-config"] +# Desktop settings, subscriptions, hotkeys, and recorder-process integration. +desktop-config = ["dep:jcode-base", "dep:reqwest", "dep:serde", "dep:anyhow", "dep:libc", "dep:zbus", "dep:async-io", "tokio/process", "tokio/rt-multi-thread"] +# Auth sandbox coverage is opt-in so lightweight tests never pull in jcode-base. +desktop-test-support = ["desktop-config", "jcode-base/test-support"] +voice-capture = ["desktop-config", "dep:cpal"] [dev-dependencies] -jcode-base = { path = "../jcode-base", default-features = false, features = ["test-support"] } tempfile = "3" tokio = { version = "1", features = ["test-util"] } diff --git a/crates/jcode-voice/src/lib.rs b/crates/jcode-voice/src/lib.rs index b14189926f6..3d59d0a31f9 100644 --- a/crates/jcode-voice/src/lib.rs +++ b/crates/jcode-voice/src/lib.rs @@ -6,11 +6,19 @@ //! clients. `NariSession` and `nari_pcm_channel` accept bounded mono 16 kHz PCM16 //! from any source without the capture feature. Nari requires a caller-supplied //! provider key. The existing subscription APIs still need no provider key. +//! +//! Disable default features for a transport-only client with caller-owned PCM. +//! The default `desktop-config` feature adds desktop settings, subscription APIs, +//! hotkeys, and recorder processes. `voice-capture` also enables `desktop-config`. +#[cfg(feature = "desktop-config")] use jcode_base::{subscription_api, subscription_catalog}; +#[cfg(feature = "desktop-config")] use serde::Deserialize; use std::{fmt, time::Duration}; +#[cfg(feature = "desktop-config")] pub mod dictation; +#[cfg(feature = "desktop-config")] pub mod global; mod nari; pub mod timing; @@ -18,11 +26,15 @@ pub use nari::{ NARI_PCM_CHUNK_SAMPLES, NARI_USD_PER_AUDIO_HOUR, NariEvent, NariSession, correct_transcript, estimated_transcription_usd, nari_api_key, nari_pcm_channel, recognition_prompt, }; +#[cfg(feature = "desktop-config")] mod command_capture; #[cfg(any(feature = "voice-capture", test))] mod resample; +#[cfg(feature = "desktop-config")] mod streaming_capture; +#[cfg(feature = "desktop-config")] pub use command_capture::{RecorderCommand, detect_recorders}; +#[cfg(feature = "desktop-config")] pub use streaming_capture::{NariRecording, PcmRecording}; /// Native (cpal) microphone capture is compiled in. Without it, voice input @@ -89,6 +101,7 @@ impl fmt::Display for VoiceError { } impl std::error::Error for VoiceError {} +#[cfg(feature = "desktop-config")] fn network_error(error: reqwest::Error) -> VoiceError { if error.is_timeout() { VoiceError::Timeout @@ -97,6 +110,7 @@ fn network_error(error: reqwest::Error) -> VoiceError { } } +#[cfg(feature = "desktop-config")] fn client() -> Result { reqwest::Client::builder() .redirect(reqwest::redirect::Policy::none()) @@ -106,6 +120,7 @@ fn client() -> Result { .map_err(network_error) } +#[cfg(feature = "desktop-config")] async fn response_bytes(mut response: reqwest::Response) -> Result, VoiceError> { let status = response.status(); if !status.is_success() { @@ -130,6 +145,7 @@ async fn response_bytes(mut response: reqwest::Response) -> Result, Voic Ok(bytes) } +#[cfg(feature = "desktop-config")] #[derive(Deserialize, Default)] struct CapabilityResponse { #[serde(default)] @@ -138,6 +154,7 @@ struct CapabilityResponse { /// Fail closed for missing keys, old servers, offline errors, and denied requests. /// This does not capture audio, request microphone access, or cache account secrets. +#[cfg(feature = "desktop-config")] pub async fn subscription_voice_available() -> bool { let Some(key) = subscription_catalog::configured_api_key().filter(|key| !key.trim().is_empty()) else { @@ -149,6 +166,7 @@ pub async fn subscription_voice_available() -> bool { .unwrap_or(false) } +#[cfg(feature = "desktop-config")] async fn voice_available_with( client: &reqwest::Client, base: &str, @@ -170,6 +188,7 @@ async fn voice_available_with( /// The server is authoritative about entitlement, so no preflight check is required. /// `language`, if supplied, is a lowercase ISO 639-1 code (for example `en`). /// Dropping the future cancels the request. Neither audio nor transcripts are logged. +#[cfg(feature = "desktop-config")] pub async fn transcribe_wav(wav: Vec, language: Option<&str>) -> Result { let key = subscription_catalog::configured_api_key() .filter(|key| !key.trim().is_empty()) @@ -185,6 +204,7 @@ pub async fn transcribe_wav(wav: Vec, language: Option<&str>) -> Result Result<(), VoiceError> { let invalid = || VoiceError::InvalidAudio; if bytes.len() < 44 @@ -286,7 +307,7 @@ fn validate_wav(bytes: &[u8]) -> Result<(), VoiceError> { Ok(()) } -#[cfg(any(feature = "voice-capture", test))] +#[cfg(all(feature = "desktop-config", any(feature = "voice-capture", test)))] fn encode_wav(samples: &[i16], sample_rate: u32) -> Result, VoiceError> { let len = samples .len() @@ -647,7 +668,7 @@ mod capture { } } -#[cfg(test)] +#[cfg(all(test, feature = "desktop-config"))] mod tests { use super::*; use std::io::{Read, Write}; @@ -724,6 +745,7 @@ mod tests { .await } + #[cfg(feature = "desktop-test-support")] #[tokio::test] async fn configured_public_api_requires_key_and_uses_configured_base() { let _sandbox = jcode_base::auth::test_sandbox::AuthTestSandbox::new().unwrap(); diff --git a/crates/jcode-voice/src/nari.rs b/crates/jcode-voice/src/nari.rs index 76612c9dc88..8dbbaf1599c 100644 --- a/crates/jcode-voice/src/nari.rs +++ b/crates/jcode-voice/src/nari.rs @@ -238,10 +238,17 @@ fn is_prompt_echo(text: &str) -> bool { /// Conservative bound on the recognition context sent per session. const MAX_PROMPT_CHARS: usize = 1000; -/// Recognition context: built-in names plus `[dictation] vocabulary`, +/// Recognition context: built-in names plus desktop `[dictation] vocabulary`, /// deduplicated case-insensitively and bounded without splitting a term. pub fn recognition_prompt() -> String { - build_prompt(&jcode_base::config::config().dictation.vocabulary) + #[cfg(feature = "desktop-config")] + { + build_prompt(&jcode_base::config::config().dictation.vocabulary) + } + #[cfg(not(feature = "desktop-config"))] + { + build_prompt(&[]) + } } fn build_prompt(extra: &[String]) -> String { @@ -275,7 +282,7 @@ fn build_prompt(extra: &[String]) -> String { } pub fn nari_api_key() -> Option { - jcode_base::provider_catalog::load_api_key_from_env_or_config("NARI_API_KEY", "nari.env") + jcode_provider_env::load_api_key_from_env_or_config("NARI_API_KEY", "nari.env") .filter(|key| !key.trim().is_empty()) } @@ -287,6 +294,7 @@ pub fn nari_pcm_channel() -> (mpsc::Sender>, mpsc::Receiver>) /// Microphone-owned channel. Holds 100 ms chunks for longer than the setup /// timeout so audio captured during the handshake is buffered, never dropped. +#[cfg(feature = "desktop-config")] pub(super) fn capture_pcm_channel() -> (mpsc::Sender>, mpsc::Receiver>) { mpsc::channel((IO_TIMEOUT.as_secs() as usize + 5) * 10) } @@ -666,6 +674,13 @@ mod tests { assert!((estimated_transcription_usd(Duration::from_secs(30)) - 0.001).abs() < 1e-12); } + #[cfg(not(feature = "desktop-config"))] + #[test] + fn lightweight_prompt_uses_builtin_vocabulary() { + assert_eq!(recognition_prompt(), build_prompt(&[])); + assert!(recognition_prompt().contains("Jcode")); + } + #[test] fn prompt_merges_user_terms_dedupes_and_stays_bounded() { let prompt = build_prompt(&[ From a9e0c9c111303f7716e73e936cf5e3db0312a3b8 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:11:09 -0800 Subject: [PATCH 060/236] tui: drop to spinner cadence while revealed stream text waits on the turn A client that had streamed text and was then thinking or running a tool kept redrawing at 60fps with zero changed cells (~0.2 cores per waiting session). Tick at the 80ms spinner cadence when the reveal backlog is empty; new deltas still wake the loop immediately. --- crates/jcode-tui/src/tui/app/tui_state.rs | 4 +++ crates/jcode-tui/src/tui/mod.rs | 15 ++++++++-- crates/jcode-tui/src/tui/redraw_schedule.rs | 21 +++++++++++++ .../src/tui/ui_tests/basic/redraw_cadence.rs | 30 +++++++++++++++++++ crates/jcode-tui/src/tui/ui_tests/mod.rs | 5 ++++ 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/tui_state.rs b/crates/jcode-tui/src/tui/app/tui_state.rs index 4231c7b1112..bde0e9be799 100644 --- a/crates/jcode-tui/src/tui/app/tui_state.rs +++ b/crates/jcode-tui/src/tui/app/tui_state.rs @@ -1079,6 +1079,10 @@ impl crate::tui::TuiState for App { self.mouse_scroll_queue != 0 } + fn stream_reveal_pending(&self) -> bool { + !self.stream_buffer.is_empty() + } + fn total_session_tokens(&self) -> Option<(u64, u64)> { // In remote mode, use tokens from server // Independent mode doesn't currently track total tokens diff --git a/crates/jcode-tui/src/tui/mod.rs b/crates/jcode-tui/src/tui/mod.rs index 79f1e4e4698..815cf4f7805 100644 --- a/crates/jcode-tui/src/tui/mod.rs +++ b/crates/jcode-tui/src/tui/mod.rs @@ -59,9 +59,9 @@ mod redraw_schedule; #[allow(unused_imports)] pub(crate) use redraw_schedule::{ REDRAW_DEEP_IDLE, REDRAW_DEEP_IDLE_AFTER, REDRAW_IDLE, REDRAW_PASSIVE_LIVENESS, - REDRAW_REMOTE_STARTUP, REDRAW_SWARM_SPINNER, current_full_frame_redraw_reason, - idle_donut_active, last_full_frame_redraw_reason, periodic_redraw_required, - periodic_redraw_required_excluding_idle_animation, redraw_interval, + REDRAW_REMOTE_STARTUP, REDRAW_STATIC_STREAM, REDRAW_SWARM_SPINNER, + current_full_frame_redraw_reason, idle_donut_active, last_full_frame_redraw_reason, + periodic_redraw_required, periodic_redraw_required_excluding_idle_animation, redraw_interval, redraw_interval_with_policy, }; mod remote_diff; @@ -961,6 +961,15 @@ pub trait TuiState { fn has_pending_mouse_scroll_animation(&self) -> bool { false } + /// Whether the paced stream buffer still holds text that later ticks will + /// reveal. While streamed text sits on screen with no backlog (e.g. the + /// model is thinking or running a tool after emitting text), nothing in the + /// transcript changes per tick, so the animation cadence is pure waste. + /// Defaults to true so state impls that do not track a backlog keep the + /// conservative fast cadence. + fn stream_reveal_pending(&self) -> bool { + true + } /// Optional configured keybinding label for external dictation. fn dictation_key_label(&self) -> Option; /// Time since app started (for startup animations) diff --git a/crates/jcode-tui/src/tui/redraw_schedule.rs b/crates/jcode-tui/src/tui/redraw_schedule.rs index 5fbb55c01c3..3d7c0073ea2 100644 --- a/crates/jcode-tui/src/tui/redraw_schedule.rs +++ b/crates/jcode-tui/src/tui/redraw_schedule.rs @@ -20,6 +20,9 @@ pub(crate) const REDRAW_PASSIVE_LIVENESS: Duration = Duration::from_millis(1000) /// Tick cadence while a drag-held edge autoscroll runs (one line per tick). pub(crate) const REDRAW_COPY_AUTOSCROLL: Duration = Duration::from_millis(30); pub(crate) const REDRAW_DEEP_IDLE_AFTER: Duration = Duration::from_secs(30); +/// Tick cadence while a turn is live but its streamed text is fully revealed. +/// Matches the status spinner glyph cadence so the spinner stays smooth. +pub(crate) const REDRAW_STATIC_STREAM: Duration = Duration::from_millis(80); /// Whether this session has been left alone long enough to be treated as /// dormant (deep idle): no stream activity *and* no user interaction for @@ -527,6 +530,24 @@ pub(crate) fn redraw_interval_with_policy_and_animation( return REDRAW_PASSIVE_LIVENESS; } + // Streamed text is on screen but fully revealed while the turn continues + // (thinking or running a tool after emitting text). New deltas wake the + // loop themselves, so ticks only need to keep the status spinner and timer + // alive. Measured on a live client: this state held 66 full frames/s with + // zero changed cells, about 0.2 cores per waiting session. + if state.is_processing() + && !state.streaming_text().is_empty() + && !state.stream_reveal_pending() + && !state.has_pending_mouse_scroll_animation() + && !state.copy_selection_edge_autoscroll_active() + && !rate_limit_countdown_redraw_active(state) + { + return match policy.tier { + crate::perf::PerformanceTier::Minimal => REDRAW_IDLE, + _ => REDRAW_STATIC_STREAM, + }; + } + if state.is_processing() || !state.streaming_text().is_empty() || state.has_pending_mouse_scroll_animation() diff --git a/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs b/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs index ff4722498c7..87283c20c1a 100644 --- a/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs +++ b/crates/jcode-tui/src/tui/ui_tests/basic/redraw_cadence.rs @@ -320,3 +320,33 @@ fn a_notice_screen_left_alone_still_reaches_deep_idle() { "a dormant notice screen must tick at the deep-idle crawl" ); } + +/// A turn that already streamed text and is now thinking or running a tool +/// shows a fully revealed transcript. Repainting it at the fast cadence redrew +/// identical cells 66 times a second on a live client (about 0.2 cores per +/// waiting session). Only the spinner and timer change, so it must drop to the +/// spinner cadence, while a pending reveal backlog keeps the fast cadence. +#[test] +fn fully_revealed_stream_waiting_on_the_turn_drops_to_spinner_cadence() { + let policy = full_tier_policy(); + let fast_interval = Duration::from_millis(1000 / u64::from(policy.redraw_fps.max(1))); + + let mut waiting = static_chrome_state(None); + waiting.status = ProcessingStatus::Streaming; + waiting.streaming_text = "partial assistant answer".to_string(); + waiting.stream_reveal_pending = Some(false); + // TestState::is_processing derives from status. + assert_eq!( + crate::tui::redraw_interval_with_policy(&waiting, &policy), + crate::tui::REDRAW_STATIC_STREAM, + "fully revealed streamed text must not repaint at the fast cadence" + ); + + let mut revealing = waiting.clone(); + revealing.stream_reveal_pending = Some(true); + assert_eq!( + crate::tui::redraw_interval_with_policy(&revealing, &policy), + fast_interval, + "a reveal backlog must keep the smooth fast cadence" + ); +} diff --git a/crates/jcode-tui/src/tui/ui_tests/mod.rs b/crates/jcode-tui/src/tui/ui_tests/mod.rs index fbc3ef7b1c7..05022d36c48 100644 --- a/crates/jcode-tui/src/tui/ui_tests/mod.rs +++ b/crates/jcode-tui/src/tui/ui_tests/mod.rs @@ -151,6 +151,8 @@ struct TestState { anim_elapsed: f32, now_ms: u64, time_since_activity: Option, + /// `Some(pending)` overrides the trait's conservative default. + stream_reveal_pending: Option, remote_startup_phase_active: bool, inline_view_state: Option, inline_interactive_state: Option, @@ -302,6 +304,9 @@ impl crate::tui::TuiState for TestState { fn time_since_activity(&self) -> Option { self.time_since_activity } + fn stream_reveal_pending(&self) -> bool { + self.stream_reveal_pending.unwrap_or(true) + } fn total_session_tokens(&self) -> Option<(u64, u64)> { None } From 64add79e32e9fcd8f568bba16cc24c655c86ed74 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:12:09 -0800 Subject: [PATCH 061/236] tui: drop superseded streaming frames from the full-prep cache Frames keyed on partial streaming text can never be hit again but pinned full transcript snapshots (4 entries, 13.6 MB measured on one client). --- crates/jcode-tui/src/tui/ui.rs | 6 +++ .../src/tui/ui_tests/basic/body_cache.rs | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/crates/jcode-tui/src/tui/ui.rs b/crates/jcode-tui/src/tui/ui.rs index 2ea3ee845cd..ab1f961303b 100644 --- a/crates/jcode-tui/src/tui/ui.rs +++ b/crates/jcode-tui/src/tui/ui.rs @@ -1382,6 +1382,12 @@ impl FullPrepCacheState { if let Some(pos) = self.entries.iter().position(|entry| entry.key == key) { self.entries.remove(pos); } + // A frame keyed on partial streaming text can never be hit again once + // the stream advances: text only grows, and the turn ending bumps + // `messages_version`. Holding them just pins multi-MB snapshots of the + // whole transcript (measured: 4 entries, 13.6 MB on one client). + self.entries + .retain(|entry| entry.key.streaming_text_len == 0); self.entries.push_front(FullPrepCacheEntry { key, prepared, diff --git a/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs b/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs index 8a042e672f6..4b53364633b 100644 --- a/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs +++ b/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs @@ -1043,3 +1043,57 @@ fn test_prepare_body_collapses_anchored_images_when_inline_images_hidden() { #[path = "cache_equivalence_tests.rs"] mod cache_equivalence; use cache_equivalence::assert_prepared_equivalent; + +/// Frames keyed on partial streaming text can never be hit again (the stream +/// only grows and the end of the turn bumps `messages_version`), so a new +/// insert must not keep them pinned. Settled frames stay cached. +#[test] +fn test_full_prep_cache_drops_superseded_streaming_frames() { + let mut cache = FullPrepCacheState::default(); + let key_for = |streaming_text_len: usize, width: u16| FullPrepCacheKey { + width, + height: 20, + diff_mode: crate::config::DiffDisplayMode::Off, + messages_version: 1, + diagram_mode: crate::config::DiagramDisplayMode::Pinned, + centered: false, + compact_thinking: false, + mermaid_aspect_bucket: None, + is_processing: streaming_text_len > 0, + streaming_text_len, + streaming_text_hash: streaming_text_len as u64, + batch_progress_hash: 0, + inline_images_signature: (0, 0), + expanded_images_version: 0, + swarm_members_signature: 0, + latex_rendering: crate::config::LatexRenderingMode::Image, + show_link_urls: false, + inline_images_visible: true, + }; + let prepared = || { + make_prepared_chat_frame(Arc::new(PreparedMessages { + wrapped_lines: vec![Line::from("x")], + wrapped_plain_lines: Arc::new(vec!["x".to_string()]), + wrapped_copy_offsets: Arc::new(vec![0]), + raw_plain_lines: Arc::new(Vec::new()), + wrapped_line_map: Arc::new(Vec::new()), + wrapped_user_indices: Vec::new(), + wrapped_user_prompt_starts: Vec::new(), + wrapped_user_prompt_ends: Vec::new(), + user_prompt_texts: Vec::new(), + image_regions: Vec::new(), + edit_tool_ranges: Vec::new(), + copy_targets: Vec::new(), + message_boundaries: Vec::new(), + mermaid_pending_epoch: None, + })) + }; + + cache.insert(key_for(0, 80), prepared()); + cache.insert(key_for(10, 80), prepared()); + cache.insert(key_for(20, 80), prepared()); + + assert!(cache.get_exact(&key_for(0, 80)).is_some(), "settled frame kept"); + assert!(cache.get_exact(&key_for(10, 80)).is_none(), "stale stream frame dropped"); + assert!(cache.get_exact(&key_for(20, 80)).is_some(), "newest stream frame kept"); +} From dba35555dd52b44f584e27c31068e2d7adaf6265 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:14:52 -0800 Subject: [PATCH 062/236] debug: show tail catchup and reveal backlog in draw-stats redraw_schedule --- crates/jcode-tui/src/tui/app/debug_cmds.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/jcode-tui/src/tui/app/debug_cmds.rs b/crates/jcode-tui/src/tui/app/debug_cmds.rs index 72177ca8514..ca723fa47c2 100644 --- a/crates/jcode-tui/src/tui/app/debug_cmds.rs +++ b/crates/jcode-tui/src/tui/app/debug_cmds.rs @@ -1146,6 +1146,8 @@ fn attach_redraw_schedule_debug(payload: &mut serde_json::Value, app: &App) { "redraw_schedule".to_string(), serde_json::json!({ "interval_ms": crate::tui::redraw_interval(app).as_millis() as u64, + "tail_catchup_active": crate::tui::ui::tail_catchup_active(), + "stream_reveal_pending": crate::tui::TuiState::stream_reveal_pending(app), "animation_fps": policy.animation_fps, "redraw_fps": policy.redraw_fps, "tier": format!("{:?}", policy.tier), From ef38d0c8feaf751e6a0498b14e2d83d48342f5dd Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:17:15 -0800 Subject: [PATCH 063/236] cloud reload: stop detached handoff daemons before start; report rollback --- src/cli/cloud_runtime.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/cli/cloud_runtime.rs b/src/cli/cloud_runtime.rs index d29602e09f0..35d3a39c759 100644 --- a/src/cli/cloud_runtime.rs +++ b/src/cli/cloud_runtime.rs @@ -58,8 +58,19 @@ if [ {force} = 0 ]; then done fi before=$(readlink /opt/jcode-cloud/jcode) +pending=0 +sudo -n /usr/local/sbin/jcode-cloud-runtime status 2>/dev/null | grep -q '"pending": {{' && pending=1 say "restarting cloud daemon" -systemctl --user restart jcode-cloud.service || {{ say "restart failed"; exit 1; }} +# A daemon may have handed itself off outside systemd on an earlier stop. If it +# keeps the socket, the unit start fails and the new runtime is falsely rolled +# back. Turns are idle here (or --force), so stop every daemon first. +systemctl --user stop jcode-cloud.service 2>/dev/null +for p in $(pgrep -u "$(id -u)" -f '/opt/jcode-cloud/.* serve$'); do say "stopping detached daemon pid $p"; kill -TERM "$p" 2>/dev/null; done +for i in $(seq 1 20); do pgrep -u "$(id -u)" -f '/opt/jcode-cloud/.* serve$' >/dev/null || break; sleep 1; done +pkill -KILL -u "$(id -u)" -f '/opt/jcode-cloud/.* serve$' 2>/dev/null +rm -f "$XDG_RUNTIME_DIR/jcode.sock" +systemctl --user reset-failed jcode-cloud.service 2>/dev/null +systemctl --user start jcode-cloud.service || {{ say "start failed"; exit 1; }} for i in $(seq 1 60); do pid=$(systemctl --user show jcode-cloud.service --property=MainPID --value) if [ "${{pid:-0}}" != 0 ] && [ -S "$XDG_RUNTIME_DIR/jcode.sock" ]; then @@ -67,24 +78,15 @@ for i in $(seq 1 60); do pid2=$(systemctl --user show jcode-cloud.service --property=MainPID --value) if [ "$pid2" = "$pid" ]; then after=$(readlink /opt/jcode-cloud/jcode) + if [ "$pending" = 1 ] && [ "$after" = "$before" ]; then + say "new runtime failed to start and was rolled back; daemon is on: $(/opt/jcode-cloud/jcode --version 2>/dev/null)" + exit 1 + fi [ "$after" = "$before" ] || say "activated new runtime" say "daemon ready: $(/opt/jcode-cloud/jcode --version 2>/dev/null)" exit 0 fi fi - # The old daemon may hand its sessions to a detached successor on stop. The - # unit then fails with "already running" while that successor serves. - if [ "$i" -ge 10 ] && [ -S "$XDG_RUNTIME_DIR/jcode.sock" ]; then - target=$(dirname "$(readlink -f /opt/jcode-cloud/jcode)") - for p in $(pgrep -u "$(id -u)" -f ' serve$'); do - exe=$(readlink -f /proc/$p/exe 2>/dev/null) - case "$exe" in "$target"/*) - systemctl --user reset-failed jcode-cloud.service 2>/dev/null - say "daemon ready outside systemd (pid $p, handed off): $(/opt/jcode-cloud/jcode --version 2>/dev/null)" - exit 0;; - esac - done - fi sleep 1 done say "daemon did not become ready; see: journalctl --user -u jcode-cloud.service" From e8db4647fb5cca9ad408083b20dc238f983d5ebd Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:22:15 -0800 Subject: [PATCH 064/236] Lock lightweight shared voice credential dependency --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index deb0c8c326a..ab9bec50151 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4907,6 +4907,7 @@ dependencies = [ "cpal", "futures", "jcode-base", + "jcode-provider-env", "libc", "regex", "reqwest 0.12.28", From 88a2fbd05f267137ecd5c3d99371def7a6961bac Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:29:42 -0800 Subject: [PATCH 065/236] Remove mobile runner startup and backlog polling gaps --- crates/jcode-base/src/mobile_runner.rs | 111 +++++++++- .../src/mobile_runner_poll_tests.rs | 197 ++++++++++++++++++ docs/CLOUD_LATENCY.md | 17 ++ 3 files changed, 314 insertions(+), 11 deletions(-) create mode 100644 crates/jcode-base/src/mobile_runner_poll_tests.rs diff --git a/crates/jcode-base/src/mobile_runner.rs b/crates/jcode-base/src/mobile_runner.rs index 8f09f8a4045..21e1568c87c 100644 --- a/crates/jcode-base/src/mobile_runner.rs +++ b/crates/jcode-base/src/mobile_runner.rs @@ -15,6 +15,9 @@ use serde::Deserialize; use std::path::PathBuf; use std::process::Stdio; use std::time::Duration; +#[cfg(test)] +#[path = "mobile_runner_poll_tests.rs"] +mod poll_tests; #[path = "mobile_runner_process.rs"] mod process; #[cfg(test)] @@ -68,6 +71,27 @@ pub struct MobileCommand { pub kind: String, pub session_id: Option, pub text: String, + #[serde(default)] + pub created_at: Option, + #[serde(default)] + pub claimed_at: Option, +} + +impl MobileCommand { + // Both timestamps originate at the queue API, avoiding client clock skew. + // This includes wake/backlog/poll delay, not claim response transport time. + fn queue_wait_ms(&self) -> Option { + let (Ok(created), Ok(claimed)) = ( + chrono::DateTime::parse_from_rfc3339(self.created_at.as_deref()?), + chrono::DateTime::parse_from_rfc3339(self.claimed_at.as_deref()?), + ) else { + return None; + }; + if claimed < created { + return None; + } + Some(claimed.signed_duration_since(created).num_milliseconds() as u64) + } } #[derive(Deserialize)] @@ -364,6 +388,11 @@ pub async fn run_one(client: &reqwest::Client) -> Result { let Some(command) = claimed else { return Ok(false); }; + if let Some(duration_ms) = command.queue_wait_ms() { + crate::logging::info(&format!( + "[CLOUD_TIMING] operation=mobile_runner stage=queue_wait outcome=ok duration_ms={duration_ms}" + )); + } let outcome = async { let session_id = match command.kind.as_str() { "transfer" => Some( @@ -413,6 +442,28 @@ pub fn ensure_sync_enabled() { } } +async fn poll_commands(mut run: F) +where + F: FnMut() -> Fut, + Fut: std::future::Future>, +{ + loop { + let delay = match run().await { + // A completed command is evidence of activity. Drain existing work + // without another five-second gap, then return to normal idle load. + Ok(true) => continue, + Ok(false) => POLL_INTERVAL, + Err(error) => { + crate::logging::warn(&format!("mobile runner: {error:#}")); + Duration::from_secs(20) + } + }; + let mut timing = StageTimer::new("pollwait"); + tokio::time::sleep(delay).await; + timing.outcome = "ok"; + } +} + /// Background loop for the daemon on a Jcode Cloud machine. pub fn spawn_cloud_runner() { if !is_cloud_machine() { @@ -421,17 +472,7 @@ pub fn spawn_cloud_runner() { ensure_sync_enabled(); tokio::spawn(async move { let client = reqwest::Client::new(); - loop { - { - let mut timing = StageTimer::new("pollwait"); - tokio::time::sleep(POLL_INTERVAL).await; - timing.outcome = "ok"; - } - if let Err(err) = run_one(&client).await { - crate::logging::warn(&format!("mobile runner: {err:#}")); - tokio::time::sleep(Duration::from_secs(20)).await; - } - } + poll_commands(|| run_one(&client)).await; }); } @@ -439,6 +480,32 @@ pub fn spawn_cloud_runner() { mod tests { use super::*; + #[test] + fn queue_wait_uses_server_timestamps_and_accepts_legacy_commands() { + let legacy = serde_json::json!({ + "command_id": "fixture", "kind": "create", "text": "test" + }); + let command: MobileCommand = serde_json::from_value(legacy.clone()).unwrap(); + assert_eq!(command.queue_wait_ms(), None); + for (created, claimed, expected) in [ + ( + "2026-11-08T09:00:00Z", + "2026-11-08T09:00:04.750Z", + Some(4750), + ), + ("2026-11-08T01:00:00-08:00", "2026-11-08T09:00:00Z", Some(0)), + ("2026-11-08T09:00:00.0005Z", "2026-11-08T09:00:00Z", None), + ("invalid", "2026-11-08T09:00:00Z", None), + ("2026-11-08T09:00:00Z", "invalid", None), + ] { + let mut wire = legacy.clone(); + wire["created_at"] = created.into(); + wire["claimed_at"] = claimed.into(); + let command: MobileCommand = serde_json::from_value(wire).unwrap(); + assert_eq!(command.queue_wait_ms(), expected); + } + } + #[test] fn stage_timing_uses_content_free_shared_report_fields() { for stage in ["claim", "transfer", "turn", "sync", "complete", "pollwait"] { @@ -514,6 +581,28 @@ mod tests { serde_json::json!({"session": {"session_id":"local-1", "source":"local"}, "messages":messages, "more":more}) } + #[tokio::test] + async fn claim_preserves_server_queue_age_without_logging_command_content() { + let (base, server) = transcript_server(vec![serde_json::json!({ + "command": {"command_id":"fixture", "kind":"create", "text":"private fixture", + "created_at":"2026-11-08T09:00:00Z", "claimed_at":"2026-11-08T09:00:04.750Z"} + })]) + .await; + let command = claim(&reqwest::Client::new(), &base, "fixture-key") + .await + .unwrap() + .unwrap(); + assert_eq!(command.queue_wait_ms(), Some(4750)); + assert_eq!(command.text, "private fixture"); + let requests = server.await.unwrap(); + assert!(requests[0].starts_with("POST /mobile/commands/claim ")); + assert!( + requests[0] + .to_ascii_lowercase() + .contains("authorization: bearer fixture-key\r\n") + ); + } + #[tokio::test] async fn fetches_all_pages_with_account_auth_and_sparse_sequences() { let (base, server) = transcript_server(vec![ diff --git a/crates/jcode-base/src/mobile_runner_poll_tests.rs b/crates/jcode-base/src/mobile_runner_poll_tests.rs new file mode 100644 index 00000000000..8ccd369c6e8 --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_poll_tests.rs @@ -0,0 +1,197 @@ +//! Controlled scheduler tests. These use no live mobile API or network service. + +use super::poll_commands; +use std::sync::{ + Arc, Mutex, + atomic::{AtomicUsize, Ordering}, +}; +use std::time::Duration; +use tokio::task::JoinHandle; +use tokio::time::{Instant, advance}; + +// Keep the test future runnable so Tokio cannot auto-advance its paused clock. +async fn settle() { + for _ in 0..16 { + tokio::task::yield_now().await; + } +} + +async fn tick(seconds: u64) { + advance(Duration::from_secs(seconds)).await; + settle().await; +} + +async fn cancel(task: JoinHandle<()>) { + task.abort(); + assert!(task.await.unwrap_err().is_cancelled()); +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_starts_immediately() { + let started = Instant::now(); + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Ok(false) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!( + Instant::now(), + started, + "startup must not advance the clock" + ); + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_drains_completed_work_without_delay() { + let started = Instant::now(); + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + let call = observed.fetch_add(1, Ordering::SeqCst); + async move { Ok(call < 4) } + })); + + settle().await; + assert_eq!( + calls.load(Ordering::SeqCst), + 5, + "four completed commands then one idle claim" + ); + assert_eq!( + Instant::now(), + started, + "queued commands must have no polling gap" + ); + tick(4).await; + assert_eq!(calls.load(Ordering::SeqCst), 5); + tick(1).await; + assert_eq!(calls.load(Ordering::SeqCst), 6); + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_idle_claims_once_per_five_seconds() { + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Ok(false) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + for expected in 2..=5 { + tick(4).await; + assert_eq!(calls.load(Ordering::SeqCst), expected - 1); + tick(1).await; + assert_eq!(calls.load(Ordering::SeqCst), expected); + } + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_errors_retry_after_twenty_not_twenty_five_seconds() { + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Err(anyhow::anyhow!("controlled claim failure")) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + tick(19).await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + tick(1).await; + assert_eq!( + calls.load(Ordering::SeqCst), + 2, + "retry must occur at t=20, not t=25" + ); + tick(5).await; + assert_eq!(calls.load(Ordering::SeqCst), 2); + tick(14).await; + assert_eq!(calls.load(Ordering::SeqCst), 2); + tick(1).await; + assert_eq!(calls.load(Ordering::SeqCst), 3); + cancel(task).await; +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_cancellation_stops_future_claims() { + let calls = Arc::new(AtomicUsize::new(0)); + let observed = calls.clone(); + let task = tokio::spawn(poll_commands(move || { + observed.fetch_add(1, Ordering::SeqCst); + async { Ok(false) } + })); + + settle().await; + assert_eq!(calls.load(Ordering::SeqCst), 1); + cancel(task).await; + for _ in 0..8 { + tick(5).await; + } + assert_eq!( + calls.load(Ordering::SeqCst), + 1, + "cancelled poller must never claim again" + ); +} + +#[tokio::test(start_paused = true)] +async fn poll_commands_controlled_clock_baseline_comparison() { + let started = Instant::now(); + let baseline_times = Arc::new(Mutex::new(Vec::new())); + let recorded = baseline_times.clone(); + // Model the old sleep-before-every-claim scheduling, not a live API. + let baseline = tokio::spawn(async move { + for _ in 0..3 { + tokio::time::sleep(Duration::from_secs(5)).await; + recorded.lock().unwrap().push(started.elapsed()); + } + }); + let new_times = Arc::new(Mutex::new(Vec::new())); + let recorded = new_times.clone(); + let new = tokio::spawn(poll_commands(move || { + let mut times = recorded.lock().unwrap(); + let completed = times.len() < 3; + if completed { + times.push(started.elapsed()); + } + async move { Ok(completed) } + })); + + settle().await; + assert!(baseline_times.lock().unwrap().is_empty()); + assert_eq!(*new_times.lock().unwrap(), vec![Duration::ZERO; 3]); + for _ in 0..3 { + tick(5).await; + } + baseline.await.unwrap(); + let old = baseline_times.lock().unwrap().clone(); + let new_observed = new_times.lock().unwrap().clone(); + assert_eq!( + old, + vec![ + Duration::from_secs(5), + Duration::from_secs(10), + Duration::from_secs(15) + ] + ); + assert_eq!(new_observed, vec![Duration::ZERO; 3]); + assert!( + old.windows(2) + .all(|pair| pair[1] - pair[0] == Duration::from_secs(5)) + ); + assert!(new_observed.windows(2).all(|pair| pair[1] == pair[0])); + println!( + "Controlled paused-clock scheduling comparison, NOT a live API benchmark: baseline command times {old:?}, new {new_observed:?}. Removed 5s startup delay and 5s gap per queued command." + ); + cancel(new).await; +} diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index d86892fe71f..b91a481ef61 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -147,6 +147,23 @@ in 3ms. This demonstrates removal of a pipe deadlock, not faster model turns. and poll wait, including errors and cancellation. The five-second poll cadence is unchanged. +The runner now claims immediately on startup and immediately after completing a +command. It sleeps five seconds only after an empty claim, retaining the idle +poll rate. Errors back off for 20 seconds rather than 20 seconds plus the old +unconditional five-second wait. This removes scheduling gaps at startup and +between queued commands, not the up-to-five-second wait for a command arriving +during an idle poll interval. The current API provides no long-poll or command +notification channel, so that remaining latency needs coordinated API work. + +`operation=mobile_runner stage=queue_wait` records `claimed_at - created_at` +using the two API-provided timestamps, avoiding phone/daemon clock skew. It +includes machine wake, previous queued work and polling delays, but excludes +claim-response transport and turn execution. Legacy responses without those +timestamps still execute normally. Invalid or reversed timestamps omit the +metric rather than changing command handling. Logs contain only the duration, +not command IDs, text or timestamps. Paused-clock regression comparisons prove +the eliminated five-second scheduling gaps, not production end-to-end latency. + The separate `jcode-mobile` repository's phone transcript transport now pools HTTP connections too (commit `a98b496`). Eight actual list calls in a loopback fixture with 30ms connection setup and 8ms body delay improved from median From e0d3dbda5fa889886c57287594cd0651ebc6638a Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:34:38 -0800 Subject: [PATCH 066/236] mobile: report aggregate agents status to /v1/agents/status Track per-session running/waiting/error state (tool name detail, title, since) from turn, tool and stdin hooks, including swarm workers. Debounce 2s, heartbeat every 5 min while non-empty, send a final empty snapshot, and reuse mobile push opt-out, credentials and no-devices back-off. --- crates/jcode-app-core/src/agents_status.rs | 483 ++++++++++++++++++ crates/jcode-app-core/src/lib.rs | 1 + crates/jcode-app-core/src/mobile_push.rs | 16 +- .../src/server/client_processing.rs | 10 + crates/jcode-app-core/src/server/state.rs | 7 + crates/jcode-app-core/src/server/swarm.rs | 1 + crates/jcode-app-core/src/tool/bash.rs | 1 + 7 files changed, 516 insertions(+), 3 deletions(-) create mode 100644 crates/jcode-app-core/src/agents_status.rs diff --git a/crates/jcode-app-core/src/agents_status.rs b/crates/jcode-app-core/src/agents_status.rs new file mode 100644 index 00000000000..548de378acf --- /dev/null +++ b/crates/jcode-app-core/src/agents_status.rs @@ -0,0 +1,483 @@ +//! Aggregate "many agents" status reporter for the mobile Live Activity. +//! +//! Contract: `~/jcode-mobile/docs/PUSH.md` ("Many agents (v2)"). The server +//! POSTs a full snapshot of this host's non-idle sessions to +//! `{base}/agents/status` on change (debounced), as a heartbeat every +//! [`HEARTBEAT`] while non-empty, and one final empty snapshot. Swarm workers +//! are included. Uses the same opt-out and credentials as mobile pushes, and +//! never blocks a turn. + +use serde::Serialize; +use std::collections::HashMap; +use std::sync::{Mutex, OnceLock}; +use std::time::{Duration, Instant}; + +pub const DEBOUNCE: Duration = Duration::from_secs(2); +pub const HEARTBEAT: Duration = Duration::from_secs(5 * 60); +const MAX_SESSIONS: usize = 200; +const TITLE_MAX: usize = 80; +const DETAIL_MAX: usize = 60; +const ERROR_BACKOFF_MIN: Duration = Duration::from_secs(30); +const ERROR_BACKOFF_MAX: Duration = Duration::from_secs(15 * 60); +const SEND_TIMEOUT: Duration = Duration::from_secs(5); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "lowercase")] +pub enum AgentState { + Running, + Waiting, + Error, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Entry { + title: String, + state: AgentState, + detail: String, + since: u64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct SessionStatus { + pub session_id: String, + pub title: String, + pub state: AgentState, + pub detail: String, + pub since: u64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Snapshot { + pub host: String, + pub sessions: Vec, +} + +/// Pure state machine: session states plus debounce/heartbeat scheduling. +#[derive(Debug, Default)] +pub struct Tracker { + sessions: HashMap, + /// First unsent change (debounce anchor). + dirty_since: Option, + last_sent: Option, + /// Whether the last delivered snapshot was non-empty. + last_sent_nonempty: bool, + /// Do not send before this (error back-off). + backoff_until: Option, + backoff: Option, +} + +fn now_unix() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or(0) +} + +fn clip(text: &str, max: usize) -> String { + let one_line = text.split_whitespace().collect::>().join(" "); + if one_line.chars().count() <= max { + return one_line; + } + let mut out: String = one_line.chars().take(max.saturating_sub(1)).collect(); + out.push('…'); + out +} + +impl Tracker { + fn mark(&mut self, now: Instant) { + self.dirty_since.get_or_insert(now); + } + + fn set( + &mut self, + id: &str, + state: AgentState, + title: Option<&str>, + detail: &str, + now: Instant, + ) { + let since = now_unix(); + let mut changed = !self.sessions.contains_key(id); + let entry = self + .sessions + .entry(id.to_string()) + .or_insert_with(|| Entry { + title: String::new(), + state, + detail: String::new(), + since, + }); + if let Some(title) = title { + let title = clip(title, TITLE_MAX); + if entry.title != title { + entry.title = title; + changed = true; + } + } + if entry.state != state { + entry.state = state; + entry.since = since; + changed = true; + } + let detail = clip(detail, DETAIL_MAX); + if entry.detail != detail { + entry.detail = detail; + changed = true; + } + if changed { + self.mark(now); + } + } + + pub fn turn_started(&mut self, id: &str, title: &str, now: Instant) { + self.sessions.remove(id); + self.set(id, AgentState::Running, Some(title), "", now); + } + + pub fn tool_started(&mut self, id: &str, tool: &str, now: Instant) { + if self + .sessions + .get(id) + .is_some_and(|e| e.state == AgentState::Running) + { + self.set(id, AgentState::Running, None, tool, now); + } + } + + pub fn tool_done(&mut self, id: &str, now: Instant) { + if self.sessions.contains_key(id) { + self.set(id, AgentState::Running, None, "", now); + } + } + + pub fn waiting(&mut self, id: &str, detail: &str, now: Instant) { + self.set(id, AgentState::Waiting, None, detail, now); + } + + pub fn turn_finished(&mut self, id: &str, error: Option<&str>, now: Instant) { + match error { + Some(detail) => self.set(id, AgentState::Error, None, detail, now), + None => { + if self.sessions.remove(id).is_some() { + self.mark(now); + } + } + } + } + + pub fn session_closed(&mut self, id: &str, now: Instant) { + if self.sessions.remove(id).is_some() { + self.mark(now); + } + } + + pub fn snapshot(&self, host: &str) -> Snapshot { + let mut sessions: Vec = self + .sessions + .iter() + .map(|(id, e)| SessionStatus { + session_id: id.clone(), + title: e.title.clone(), + state: e.state, + detail: e.detail.clone(), + since: e.since, + }) + .collect(); + let rank = |s: AgentState| match s { + AgentState::Waiting => 0, + AgentState::Error => 1, + AgentState::Running => 2, + }; + sessions.sort_by(|a, b| { + rank(a.state) + .cmp(&rank(b.state)) + .then(b.since.cmp(&a.since)) + .then(a.session_id.cmp(&b.session_id)) + }); + sessions.truncate(MAX_SESSIONS); + Snapshot { + host: host.to_string(), + sessions, + } + } + + /// When the next send is due, or `None` if nothing to send. + pub fn next_due(&self) -> Option { + let due = match (self.dirty_since, self.last_sent) { + (Some(dirty), _) => Some(dirty + DEBOUNCE), + (None, Some(last)) if self.last_sent_nonempty || !self.sessions.is_empty() => { + Some(last + HEARTBEAT) + } + (None, None) if !self.sessions.is_empty() => Some(Instant::now()), + _ => None, + }?; + Some(match self.backoff_until { + Some(b) if b > due => b, + _ => due, + }) + } + + /// Take a snapshot if a send is due at `now`. + pub fn take_due(&mut self, host: &str, now: Instant) -> Option { + if self.next_due()? > now { + return None; + } + Some(self.snapshot(host)) + } + + pub fn record_result(&mut self, snapshot: &Snapshot, ok: bool, now: Instant) { + if ok { + self.dirty_since = None; + self.last_sent = Some(now); + self.last_sent_nonempty = !snapshot.sessions.is_empty(); + self.backoff = None; + self.backoff_until = None; + } else { + let next = self + .backoff + .map(|b| (b * 2).min(ERROR_BACKOFF_MAX)) + .unwrap_or(ERROR_BACKOFF_MIN); + self.backoff = Some(next); + self.backoff_until = Some(now + next); + } + } + + /// Back off without counting an error (e.g. no registered devices). + pub fn suppress_until(&mut self, until: Instant) { + self.backoff_until = Some(until); + } +} + +static TRACKER: Mutex> = Mutex::new(None); +static WAKE: OnceLock = OnceLock::new(); +static WORKER_STARTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + +fn with_tracker(f: impl FnOnce(&mut Tracker, Instant)) { + if !crate::mobile_push::enabled() { + return; + } + let mut guard = TRACKER.lock().unwrap_or_else(|p| p.into_inner()); + f(guard.get_or_insert_with(Tracker::default), Instant::now()); + drop(guard); + ensure_worker(); + WAKE.get_or_init(tokio::sync::Notify::new).notify_one(); +} + +pub fn turn_started(session_id: &str, title: &str) { + with_tracker(|t, now| t.turn_started(session_id, title, now)); +} +pub fn tool_started(session_id: &str, tool: &str) { + with_tracker(|t, now| t.tool_started(session_id, tool, now)); +} +pub fn tool_done(session_id: &str) { + with_tracker(|t, now| t.tool_done(session_id, now)); +} +pub fn waiting(session_id: &str, detail: &str) { + with_tracker(|t, now| t.waiting(session_id, detail, now)); +} +pub fn turn_finished(session_id: &str, error: Option<&str>) { + with_tracker(|t, now| t.turn_finished(session_id, error, now)); +} +pub fn session_closed(session_id: &str) { + with_tracker(|t, now| t.session_closed(session_id, now)); +} + +/// Stable host id: `[A-Za-z0-9_.:-]{1,64}`, from /etc/machine-id or hostname. +pub fn host_id() -> &'static str { + static HOST: OnceLock = OnceLock::new(); + HOST.get_or_init(|| { + let raw = ["/etc/machine-id", "/var/lib/dbus/machine-id"] + .iter() + .find_map(|p| std::fs::read_to_string(p).ok()) + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()) + .or_else(|| std::env::var("HOSTNAME").ok()) + .or_else(|| std::fs::read_to_string("/etc/hostname").ok()) + .or_else(|| std::env::var("COMPUTERNAME").ok()) + .unwrap_or_else(|| "jcode-host".to_string()); + sanitize_host(&raw) + }) +} + +pub fn sanitize_host(raw: &str) -> String { + let s: String = raw + .trim() + .chars() + .map(|c| { + if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-') { + c + } else { + '-' + } + }) + .take(64) + .collect(); + if s.is_empty() { "jcode-host".into() } else { s } +} + +fn ensure_worker() { + let Ok(handle) = tokio::runtime::Handle::try_current() else { + return; + }; + if WORKER_STARTED.swap(true, std::sync::atomic::Ordering::SeqCst) { + return; + } + handle.spawn(worker()); +} + +async fn worker() { + let wake = WAKE.get_or_init(tokio::sync::Notify::new); + loop { + let due = TRACKER + .lock() + .unwrap_or_else(|p| p.into_inner()) + .as_ref() + .and_then(Tracker::next_due); + match due { + Some(due) => { + let sleep = tokio::time::sleep_until(tokio::time::Instant::from_std(due)); + tokio::select! { _ = sleep => {}, _ = wake.notified() => continue } + } + None => { + wake.notified().await; + continue; + } + } + let snapshot = TRACKER + .lock() + .unwrap_or_else(|p| p.into_inner()) + .as_mut() + .and_then(|t| t.take_due(host_id(), Instant::now())); + let Some(snapshot) = snapshot else { continue }; + let outcome = post(&snapshot).await; + let mut guard = TRACKER.lock().unwrap_or_else(|p| p.into_inner()); + if let Some(t) = guard.as_mut() { + let now = Instant::now(); + match outcome { + PostOutcome::Ok => t.record_result(&snapshot, true, now), + PostOutcome::NoDevices => { + t.record_result(&snapshot, true, now); + t.suppress_until(now + crate::mobile_push::NO_DEVICES_SUPPRESS); + } + PostOutcome::Failed => t.record_result(&snapshot, false, now), + } + } + } +} + +enum PostOutcome { + Ok, + NoDevices, + Failed, +} + +#[derive(serde::Deserialize)] +struct Reply { + #[serde(default)] + devices: Option, +} + +async fn post(snapshot: &Snapshot) -> PostOutcome { + if !crate::mobile_push::enabled() { + return PostOutcome::Ok; + } + if crate::mobile_push::devices_suppressed() { + return PostOutcome::NoDevices; + } + let Some((key, base)) = crate::mobile_push::credentials() else { + // Not signed in: treat as delivered so we do not spin. + return PostOutcome::Ok; + }; + let result = crate::provider::shared_http_client() + .post(format!("{base}/agents/status")) + .bearer_auth(key) + .json(snapshot) + .timeout(SEND_TIMEOUT) + .send() + .await; + match result { + Ok(resp) if resp.status().is_success() => match resp.json::().await { + Ok(Reply { devices: Some(0) }) => PostOutcome::NoDevices, + _ => PostOutcome::Ok, + }, + Ok(resp) => { + crate::logging::warn(&format!("agents status rejected: HTTP {}", resp.status())); + PostOutcome::Failed + } + Err(error) => { + crate::logging::warn(&format!("agents status failed: {error}")); + PostOutcome::Failed + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn snapshot_orders_and_truncates() { + let mut t = Tracker::default(); + let now = Instant::now(); + t.turn_started("a", "Fix login", now); + t.tool_started("a", "bash", now); + t.turn_started("b", &"x".repeat(200), now); + t.waiting("b", "needs input", now); + t.turn_started("c", "C", now); + t.turn_finished("c", Some("boom"), now); + t.turn_started("d", "D", now); + t.turn_finished("d", None, now); + let snap = t.snapshot("host1"); + let ids: Vec<_> = snap + .sessions + .iter() + .map(|s| s.session_id.as_str()) + .collect(); + assert_eq!(ids, ["b", "c", "a"]); + assert_eq!(snap.sessions[2].detail, "bash"); + assert_eq!(snap.sessions[0].title.chars().count(), TITLE_MAX); + let json = serde_json::to_value(&snap).unwrap(); + assert_eq!(json["sessions"][1]["state"], "error"); + assert_eq!(json["host"], "host1"); + } + + #[test] + fn debounce_heartbeat_and_final_empty() { + let mut t = Tracker::default(); + let t0 = Instant::now(); + assert!(t.next_due().is_none()); + t.turn_started("a", "A", t0); + t.tool_started("a", "bash", t0 + Duration::from_millis(500)); + assert!(t.take_due("h", t0 + Duration::from_secs(1)).is_none()); + let at = t0 + DEBOUNCE; + let snap = t.take_due("h", at).expect("due after debounce"); + assert_eq!(snap.sessions.len(), 1); + t.record_result(&snap, true, at); + assert_eq!(t.next_due(), Some(at + HEARTBEAT)); + t.turn_finished("a", None, at + Duration::from_secs(10)); + let end = at + Duration::from_secs(10) + DEBOUNCE; + let snap = t.take_due("h", end).expect("final empty"); + assert!(snap.sessions.is_empty()); + t.record_result(&snap, true, end); + assert!(t.next_due().is_none(), "no heartbeat after empty"); + } + + #[test] + fn errors_back_off() { + let mut t = Tracker::default(); + let t0 = Instant::now(); + t.turn_started("a", "A", t0); + let snap = t.take_due("h", t0 + DEBOUNCE).unwrap(); + t.record_result(&snap, false, t0 + DEBOUNCE); + assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + ERROR_BACKOFF_MIN)); + t.record_result(&snap, false, t0 + DEBOUNCE); + assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + ERROR_BACKOFF_MIN * 2)); + } + + #[test] + fn host_is_sanitized() { + assert_eq!(sanitize_host(" my host!\n"), "my-host-"); + assert_eq!(sanitize_host(""), "jcode-host"); + assert_eq!(sanitize_host(&"a".repeat(100)).len(), 64); + } +} diff --git a/crates/jcode-app-core/src/lib.rs b/crates/jcode-app-core/src/lib.rs index a5712243e69..6e922c4b9c9 100644 --- a/crates/jcode-app-core/src/lib.rs +++ b/crates/jcode-app-core/src/lib.rs @@ -32,6 +32,7 @@ pub mod build; pub mod catchup; pub mod channel; pub use jcode_base::external_auth; +pub mod agents_status; pub mod mission; pub mod mobile_push; pub mod network_retry; diff --git a/crates/jcode-app-core/src/mobile_push.rs b/crates/jcode-app-core/src/mobile_push.rs index badcd852ebc..e3a4d6a1582 100644 --- a/crates/jcode-app-core/src/mobile_push.rs +++ b/crates/jcode-app-core/src/mobile_push.rs @@ -437,6 +437,14 @@ impl Suppression { static SUPPRESSION: Mutex = Mutex::new(Suppression { until: None }); +/// True while the API recently reported no registered devices. +pub(crate) fn devices_suppressed() -> bool { + SUPPRESSION + .lock() + .map(|s| s.is_suppressed(Instant::now())) + .unwrap_or(false) +} + fn env_opted_out(value: Option<&str>) -> bool { matches!( value.map(|v| v.trim().to_ascii_lowercase()).as_deref(), @@ -444,7 +452,7 @@ fn env_opted_out(value: Option<&str>) -> bool { ) } -fn enabled() -> bool { +pub(crate) fn enabled() -> bool { let opt_out = match std::env::var(OPT_OUT_ENV) { Ok(value) => Some(value), Err(std::env::VarError::NotPresent) => None, @@ -459,7 +467,7 @@ fn enabled() -> bool { crate::config::config().server.mobile_push != Some(false) } -fn credentials() -> Option<(String, String)> { +pub(crate) fn credentials() -> Option<(String, String)> { let key = crate::provider_catalog::load_api_key_from_env_or_config( JCODE_API_KEY_ENV, JCODE_ENV_FILE, @@ -986,7 +994,9 @@ mod tests { (PushKind::TurnError, "boom", "\u{274C}"), (PushKind::Wake, "bash completed", "\u{23F0}"), ] { - let t = build_payload(kind, "s", Some("T"), body, None).session_title.unwrap(); + let t = build_payload(kind, "s", Some("T"), body, None) + .session_title + .unwrap(); assert_eq!(t, format!("{marker} T")); } let bullets = "- Updated parser\n- Added tests\n- Shipped v2 to production servers today"; diff --git a/crates/jcode-app-core/src/server/client_processing.rs b/crates/jcode-app-core/src/server/client_processing.rs index 00706e78888..a212625c3cc 100644 --- a/crates/jcode-app-core/src/server/client_processing.rs +++ b/crates/jcode-app-core/src/server/client_processing.rs @@ -296,9 +296,11 @@ pub(super) async fn start_processing_message( client_session_id, id, error )); } + let status_title = agent_guard.session_display_title_or_name(); drop(agent_guard); *state.client_is_processing = true; + crate::agents_status::turn_started(client_session_id, &status_title); *state.message_id = Some(id); *state.session_id = Some(client_session_id.to_string()); @@ -399,6 +401,14 @@ pub(super) async fn start_processing_message( agent.session_display_title_or_name(), ) }; + crate::agents_status::turn_finished( + &push_session_id, + result + .as_ref() + .err() + .map(|error| crate::util::format_error_chain(error)) + .as_deref(), + ); // Mobile push: fire-and-forget, skipped for user cancellation and for // swarm worker sessions (their coordinator reports instead). if !owned_worker && !crate::mobile_push::take_cancelled(&push_session_id) { diff --git a/crates/jcode-app-core/src/server/state.rs b/crates/jcode-app-core/src/server/state.rs index 3371b1063ee..014c29c4ae1 100644 --- a/crates/jcode-app-core/src/server/state.rs +++ b/crates/jcode-app-core/src/server/state.rs @@ -532,6 +532,13 @@ impl TurnEventChannel { let Some(event) = event else { return; }; + match &event { + ServerEvent::ToolExec { name, .. } => { + crate::agents_status::tool_started(&session_id, name) + } + ServerEvent::ToolDone { .. } => crate::agents_status::tool_done(&session_id), + _ => {} + } let event = ServerEvent::TurnEvent { identity: identity.clone(), event: Box::new(event), diff --git a/crates/jcode-app-core/src/server/swarm.rs b/crates/jcode-app-core/src/server/swarm.rs index 1239b7019a7..823e4135d45 100644 --- a/crates/jcode-app-core/src/server/swarm.rs +++ b/crates/jcode-app-core/src/server/swarm.rs @@ -1172,6 +1172,7 @@ pub(super) async fn remove_session_from_swarm( swarm_plans: &Arc>>, ) { let started = Instant::now(); + crate::agents_status::session_closed(session_id); log_swarm_lifecycle( "member_remove_start", vec![ diff --git a/crates/jcode-app-core/src/tool/bash.rs b/crates/jcode-app-core/src/tool/bash.rs index 98538c2c8d2..b4da105aa3c 100644 --- a/crates/jcode-app-core/src/tool/bash.rs +++ b/crates/jcode-app-core/src/tool/bash.rs @@ -999,6 +999,7 @@ impl BashTool { if stdin_tx.send(request).is_err() { break; } + crate::agents_status::waiting(&push_session_id, &push_command); crate::mobile_push::notify( crate::mobile_push::PushKind::NeedsInput, &push_session_id, From caf3111b3119a1c6cc10aeeb2b0d17a73cd35dae Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:36:38 -0800 Subject: [PATCH 067/236] mobile: clear agents status on session close and expire errors after 30 min --- crates/jcode-app-core/src/agents_status.rs | 64 ++++++++++++++++++- .../src/server/client_disconnect_cleanup.rs | 3 + 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/crates/jcode-app-core/src/agents_status.rs b/crates/jcode-app-core/src/agents_status.rs index 548de378acf..4ce75da5579 100644 --- a/crates/jcode-app-core/src/agents_status.rs +++ b/crates/jcode-app-core/src/agents_status.rs @@ -20,6 +20,8 @@ const DETAIL_MAX: usize = 60; const ERROR_BACKOFF_MIN: Duration = Duration::from_secs(30); const ERROR_BACKOFF_MAX: Duration = Duration::from_secs(15 * 60); const SEND_TIMEOUT: Duration = Duration::from_secs(5); +/// Unanswered error states drop out of the snapshot after this. +pub const ERROR_TTL: Duration = Duration::from_secs(30 * 60); #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "lowercase")] @@ -35,6 +37,8 @@ struct Entry { state: AgentState, detail: String, since: u64, + /// Monotonic time the current state began (for error expiry). + entered: Instant, } #[derive(Debug, Clone, PartialEq, Eq, Serialize)] @@ -106,6 +110,7 @@ impl Tracker { state, detail: String::new(), since, + entered: now, }); if let Some(title) = title { let title = clip(title, TITLE_MAX); @@ -117,6 +122,7 @@ impl Tracker { if entry.state != state { entry.state = state; entry.since = since; + entry.entered = now; changed = true; } let detail = clip(detail, DETAIL_MAX); @@ -171,6 +177,25 @@ impl Tracker { } } + /// Drop error states older than [`ERROR_TTL`]. + pub fn expire(&mut self, now: Instant) { + let before = self.sessions.len(); + self.sessions.retain(|_, e| { + e.state != AgentState::Error || now.saturating_duration_since(e.entered) < ERROR_TTL + }); + if self.sessions.len() != before { + self.mark(now); + } + } + + fn next_expiry(&self) -> Option { + self.sessions + .values() + .filter(|e| e.state == AgentState::Error) + .map(|e| e.entered + ERROR_TTL) + .min() + } + pub fn snapshot(&self, host: &str) -> Snapshot { let mut sessions: Vec = self .sessions @@ -210,15 +235,20 @@ impl Tracker { } (None, None) if !self.sessions.is_empty() => Some(Instant::now()), _ => None, - }?; - Some(match self.backoff_until { + }; + let due = due.map(|due| match self.backoff_until { Some(b) if b > due => b, _ => due, - }) + }); + match (due, self.next_expiry()) { + (Some(a), Some(b)) => Some(a.min(b)), + (a, b) => a.or(b), + } } /// Take a snapshot if a send is due at `now`. pub fn take_due(&mut self, host: &str, now: Instant) -> Option { + self.expire(now); if self.next_due()? > now { return None; } @@ -474,6 +504,34 @@ mod tests { assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + ERROR_BACKOFF_MIN * 2)); } + #[test] + fn error_state_expires_after_ttl() { + let mut t = Tracker::default(); + let t0 = Instant::now(); + t.turn_started("a", "A", t0); + t.turn_finished("a", Some("boom"), t0); + t.turn_started("b", "B", t0); + let snap = t.take_due("h", t0 + DEBOUNCE).unwrap(); + t.record_result(&snap, true, t0 + DEBOUNCE); + assert_eq!(t.next_due(), Some(t0 + DEBOUNCE + HEARTBEAT)); + t.last_sent = Some(t0 + ERROR_TTL); + assert_eq!( + t.next_due(), + Some(t0 + ERROR_TTL), + "expiry wakes the worker" + ); + t.expire(t0 + ERROR_TTL - Duration::from_secs(1)); + assert_eq!(t.snapshot("h").sessions.len(), 2); + t.expire(t0 + ERROR_TTL); + let snap = t.take_due("h", t0 + ERROR_TTL + DEBOUNCE).unwrap(); + let ids: Vec<_> = snap + .sessions + .iter() + .map(|s| s.session_id.as_str()) + .collect(); + assert_eq!(ids, ["b"]); + } + #[test] fn host_is_sanitized() { assert_eq!(sanitize_host(" my host!\n"), "my-host-"); diff --git a/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs b/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs index 93b4170fe33..a87f146c106 100644 --- a/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs +++ b/crates/jcode-app-core/src/server/client_disconnect_cleanup.rs @@ -153,6 +153,9 @@ pub(super) async fn cleanup_client_connection( } else { false }; + if disposition == DisconnectDisposition::Closed { + crate::agents_status::session_closed(client_session_id); + } let originally_registered = swarm_members.read().await.contains_key(client_session_id); record_transport_closed(client_session_id, client_connection_id, client_connections).await; From 1aba823d26f32d5af901118a5e7aabd42cab3e15 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:37:52 -0800 Subject: [PATCH 068/236] perf: skip per-frame background-task notification parse for non-notification messages --- crates/jcode-base/src/message.rs | 14 ++++++++++---- crates/jcode-base/src/message/notifications.rs | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/crates/jcode-base/src/message.rs b/crates/jcode-base/src/message.rs index f35f3d010ca..3d419ed3be6 100644 --- a/crates/jcode-base/src/message.rs +++ b/crates/jcode-base/src/message.rs @@ -32,10 +32,16 @@ pub use notifications::{ /// Legacy persisted wakes can retain the provider's user role even though /// their display role is a background notification, never a human prompt. pub fn is_background_task_notification_content(content: &str) -> bool { - content - .trim_start() - .starts_with("🐝 **Swarm await finished**") - || parse_background_task_notification_markdown(content).is_some() + let trimmed = content.trim_start(); + if trimmed.starts_with("🐝 **Swarm await finished**") { + return true; + } + // Both parsers anchor on a `**Background task` header. Bail out before + // they normalize (allocate a full copy of) every message on every frame. + if !trimmed.starts_with("**Background task") { + return false; + } + parse_background_task_notification_markdown(content).is_some() || parse_background_task_progress_notification_markdown(content).is_some() } diff --git a/crates/jcode-base/src/message/notifications.rs b/crates/jcode-base/src/message/notifications.rs index c5c9f11e24b..f42f4267485 100644 --- a/crates/jcode-base/src/message/notifications.rs +++ b/crates/jcode-base/src/message/notifications.rs @@ -1,5 +1,15 @@ use super::*; +/// Normalize CRLF/CR to LF, borrowing when the input has no carriage returns +/// (the common case) so per-frame role checks do not copy every message. +fn normalize_line_endings(content: &str) -> std::borrow::Cow<'_, str> { + if content.contains('\r') { + std::borrow::Cow::Owned(content.replace("\r\n", "\n").replace('\r', "\n")) + } else { + std::borrow::Cow::Borrowed(content) + } +} + fn sanitize_fenced_block(text: &str) -> String { text.replace("```", "``\u{200b}`") } @@ -401,7 +411,7 @@ pub fn parse_background_task_progress_notification_markdown( static HEADER_RE: OnceLock> = OnceLock::new(); static INLINE_RE: OnceLock> = OnceLock::new(); - let normalized = content.replace("\r\n", "\n").replace('\r', "\n"); + let normalized = normalize_line_endings(content); let trimmed = normalized.trim(); let header_re = HEADER_RE @@ -494,7 +504,7 @@ pub fn parse_background_task_notification_markdown( .get_or_init(|| compile_static_regex(r#"^_Full output:_ `(?P[^`]+)`$"#)) .as_ref()?; - let normalized = content.replace("\r\n", "\n").replace('\r', "\n"); + let normalized = normalize_line_endings(content); let mut sections = normalized.split("\n\n"); let header = sections.next()?.trim(); let captures = header_re.captures(header)?; From d2951073375b85be5329bdd956abc866beef41c8 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:39:36 -0800 Subject: [PATCH 069/236] tui: drop body cache entries superseded by a newer messages_version --- crates/jcode-tui/src/tui/ui.rs | 10 ++++ .../src/tui/ui_tests/basic/body_cache.rs | 48 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/crates/jcode-tui/src/tui/ui.rs b/crates/jcode-tui/src/tui/ui.rs index ab1f961303b..e17080d73df 100644 --- a/crates/jcode-tui/src/tui/ui.rs +++ b/crates/jcode-tui/src/tui/ui.rs @@ -1225,6 +1225,16 @@ impl BodyCacheState { if let Some(pos) = self.entries.iter().position(|entry| entry.key == key) { self.entries.remove(pos); } + // An entry that differs only by an older `messages_version` can never + // be an exact hit again, and the new entry is a better incremental + // base for the same layout. Keeping it only holds memory. + self.entries.retain(|entry| { + !(entry.key.messages_version < key.messages_version + && BodyCacheKey { + messages_version: key.messages_version, + ..entry.key.clone() + } == key) + }); self.entries.push_front(BodyCacheEntry { key, prepared, diff --git a/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs b/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs index 4b53364633b..3da973f479d 100644 --- a/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs +++ b/crates/jcode-tui/src/tui/ui_tests/basic/body_cache.rs @@ -1097,3 +1097,51 @@ fn test_full_prep_cache_drops_superseded_streaming_frames() { assert!(cache.get_exact(&key_for(10, 80)).is_none(), "stale stream frame dropped"); assert!(cache.get_exact(&key_for(20, 80)).is_some(), "newest stream frame kept"); } + +#[test] +fn test_body_cache_drops_superseded_message_versions() { + let mut cache = BodyCacheState::default(); + let key_for = |width: u16, version: u64| BodyCacheKey { + width, + diff_mode: crate::config::DiffDisplayMode::Off, + messages_version: version, + diagram_mode: crate::config::DiagramDisplayMode::Pinned, + centered: false, + compact_thinking: false, + mermaid_aspect_bucket: None, + pin_images: true, + inline_images_visible: true, + images_signature: (0, 0), + expanded_images_version: 0, + swarm_members_signature: 0, + latex_rendering: crate::config::LatexRenderingMode::Image, + show_link_urls: false, + }; + let prepared = || { + Arc::new(PreparedMessages { + wrapped_lines: vec![Line::from("x")], + wrapped_plain_lines: Arc::new(vec!["x".to_string()]), + wrapped_copy_offsets: Arc::new(vec![0]), + raw_plain_lines: Arc::new(Vec::new()), + wrapped_line_map: Arc::new(Vec::new()), + wrapped_user_indices: Vec::new(), + wrapped_user_prompt_starts: Vec::new(), + wrapped_user_prompt_ends: Vec::new(), + user_prompt_texts: Vec::new(), + image_regions: Vec::new(), + edit_tool_ranges: Vec::new(), + copy_targets: Vec::new(), + message_boundaries: Vec::new(), + mermaid_pending_epoch: None, + }) + }; + for version in 1..=5 { + cache.insert(key_for(80, version), prepared(), version as usize, 0); + } + // Other layouts are kept for fast width toggles. + cache.insert(key_for(120, 3), prepared(), 3, 0); + + assert_eq!(cache.entries.len(), 2); + assert!(cache.get_exact(&key_for(80, 5)).is_some()); + assert!(cache.get_exact(&key_for(120, 3)).is_some()); +} From b0ae6557da5c952a1191dfd0bcd871834baf5133 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:42:30 -0800 Subject: [PATCH 070/236] feat(voice): add authenticated cloud Nari relay sessions --- crates/jcode-voice/src/nari.rs | 219 ++++++++++++++++++++++++++++++++- 1 file changed, 218 insertions(+), 1 deletion(-) diff --git a/crates/jcode-voice/src/nari.rs b/crates/jcode-voice/src/nari.rs index 8dbbaf1599c..69fe38915a7 100644 --- a/crates/jcode-voice/src/nari.rs +++ b/crates/jcode-voice/src/nari.rs @@ -315,6 +315,22 @@ pub struct NariSession { } impl NariSession { + /// Connect to a Jcode cloud relay using a Jcode bearer, never a Nari key. + /// The endpoint must be `wss://.../voice/realtime`, without credentials, + /// query, or fragment. Plain WebSockets are permitted only in unit tests + /// and only to literal loopback IP addresses. + pub async fn connect_relay( + url: &str, + bearer: &str, + cancel: Arc, + ) -> Result { + if cancel.load(Ordering::SeqCst) { + return Err(VoiceError::Cancelled); + } + validate_relay_url(url)?; + Self::connect_socket(url, bearer, 4096, &recognition_prompt(), cancel).await + } + /// Resolves only after the server acknowledges session.configure. No microphone access. pub async fn connect(key: &str, cancel: Arc) -> Result { Self::connect_to(URL, key, cancel).await @@ -331,11 +347,24 @@ impl NariSession { key: &str, prompt: &str, cancel: Arc, + ) -> Result { + Self::connect_socket(url, key, 1024, prompt, cancel).await + } + + async fn connect_socket( + url: &str, + key: &str, + max_bearer_bytes: usize, + prompt: &str, + cancel: Arc, ) -> Result { if cancel.load(Ordering::SeqCst) { return Err(VoiceError::Cancelled); } - if key.is_empty() || key.len() > 1024 || !key.bytes().all(|b| (33..=126).contains(&b)) { + if key.is_empty() + || key.len() > max_bearer_bytes + || !key.bytes().all(|b| (33..=126).contains(&b)) + { return Err(VoiceError::NariNotConfigured); } let setup = async { @@ -471,6 +500,37 @@ impl NariSession { } } } +fn validate_relay_url(url: &str) -> Result<(), VoiceError> { + use tokio_tungstenite::tungstenite::http::Uri; + if url.bytes().any(|b| b <= 32 || b >= 127) || url.contains(['@', '?', '#', '\\', '%']) { + return Err(VoiceError::Network); + } + let uri: Uri = url.parse().map_err(|_| VoiceError::Network)?; + let host = uri.host().ok_or(VoiceError::Network)?; + // A Jcode bearer must never be sent to the direct provider endpoint. + let host_lower = host.to_ascii_lowercase(); + if host_lower.trim_end_matches('.') == "narilabs.com" + || host_lower.trim_end_matches('.').ends_with(".narilabs.com") + || !uri.path().ends_with("/voice/realtime") + || uri.authority().is_none() + { + return Err(VoiceError::Network); + } + if uri.scheme_str() == Some("wss") { + return Ok(()); + } + #[cfg(test)] + if uri.scheme_str() == Some("ws") + && host + .trim_start_matches('[') + .trim_end_matches(']') + .parse::() + .is_ok_and(|ip| ip.is_loopback()) + { + return Ok(()); + } + Err(VoiceError::Network) +} fn handshake_error(error: tokio_tungstenite::tungstenite::Error) -> VoiceError { if let tokio_tungstenite::tungstenite::Error::Http(response) = error { match response.status().as_u16() { @@ -607,6 +667,163 @@ impl TranscriptState { #[cfg(test)] mod tests { use super::*; + #[tokio::test] + async fn relay_rejects_unsafe_urls_and_invalid_bearers_before_network() { + for url in [ + "https://relay.example/voice/realtime", + "ws://relay.example/voice/realtime", + "wss://user:pass@relay.example/voice/realtime", + "wss://relay.example/voice/realtime?token=secret", + "wss://relay.example/voice/realtime#fragment", + "wss://relay.example/wrong", + "wss://api.narilabs.com/voice/realtime", + "wss://API.NARILABS.COM./voice/realtime", + "wss://relay.example/%2e/voice/realtime", + "wss:///voice/realtime", + ] { + assert!( + matches!( + NariSession::connect_relay(url, "test", Arc::new(AtomicBool::new(false))).await, + Err(VoiceError::Network) + ), + "unsafe URL accepted: {url}" + ); + } + assert!(validate_relay_url("wss://relay.example/api/voice/realtime").is_ok()); + for bearer in [ + String::new(), + "x".repeat(4097), + "bad\r\nheader".into(), + "bad bearer".into(), + ] { + assert!(matches!( + NariSession::connect_relay( + "wss://relay.invalid/voice/realtime", + &bearer, + Arc::new(AtomicBool::new(false)) + ) + .await, + Err(VoiceError::NariNotConfigured) + )); + } + assert!(matches!( + NariSession::connect(&"x".repeat(1025), Arc::new(AtomicBool::new(false))).await, + Err(VoiceError::NariNotConfigured) + )); + } + + async fn relay_server(bearer: String, stream: bool) -> (String, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}/voice/realtime", listener.local_addr().unwrap()); + let task = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut ws = tokio_tungstenite::accept_hdr_async( + tcp, + move |request: &tokio_tungstenite::tungstenite::handshake::server::Request, + response| { + assert_eq!(request.uri().path(), "/voice/realtime"); + assert_eq!( + request.headers()["authorization"], + format!("Bearer {bearer}") + ); + Ok(response) + }, + ) + .await + .unwrap(); + let config = receive(&mut ws).await.unwrap(); + assert_eq!( + config, + json!({"type":"session.configure", "session":{"model":"qwen3-asr-fast", "turn_detection":null, "language":"en", "prompt":recognition_prompt()}}) + ); + send(&mut ws, json!({"type":"session.configured"})) + .await + .unwrap(); + if !stream { + while let Some(Ok(_)) = ws.next().await {} + return; + } + let audio = receive(&mut ws).await.unwrap(); + assert_eq!(audio["type"], "input_audio_buffer.append"); + assert_eq!( + STANDARD.decode(audio["audio"].as_str().unwrap()).unwrap(), + [1, 0, 255, 255] + ); + let commit = receive(&mut ws).await.unwrap(); + assert_eq!(commit["event_id"], END); + for value in [ + json!({"type":"transcript.partial", "item_id":"a", "transcript":"Hel"}), + json!({"type":"input_audio_buffer.committed", "item_id":"a", "client_event_id":END}), + json!({"type":"transcript.completed", "item_id":"a", "transcript":"Hello"}), + ] { + send(&mut ws, value).await.unwrap(); + } + }); + (url, task) + } + + #[tokio::test] + async fn relay_auth_configure_partial_and_final_reuse_protocol() { + let bearer = "x".repeat(4096); + let (url, server) = relay_server(bearer.clone(), true).await; + let session = NariSession::connect_relay(&url, &bearer, Arc::new(AtomicBool::new(false))) + .await + .unwrap(); + let (tx, rx) = nari_pcm_channel(); + tx.send(vec![1, -1]).await.unwrap(); + drop(tx); + let mut events = Vec::new(); + assert_eq!( + session.run(rx, |event| events.push(event)).await.unwrap(), + "Hello" + ); + assert!( + events + .iter() + .any(|event| matches!(event, NariEvent::Transcript(text) if text == "Hel")) + ); + assert!(matches!(events.last(), Some(NariEvent::Finished(Ok(text))) if text == "Hello")); + server.await.unwrap(); + } + + #[tokio::test] + async fn relay_cancellation_interrupts_setup_and_streaming() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let cancel = Arc::new(AtomicBool::new(false)); + let c = cancel.clone(); + let trigger = tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(30)).await; + c.store(true, Ordering::SeqCst); + }); + assert!(matches!( + NariSession::connect_relay( + &format!("ws://{}/voice/realtime", listener.local_addr().unwrap()), + "test", + cancel + ) + .await, + Err(VoiceError::Cancelled) + )); + trigger.await.unwrap(); + let (url, server) = relay_server("test".into(), false).await; + let cancel = Arc::new(AtomicBool::new(false)); + let session = NariSession::connect_relay(&url, "test", cancel.clone()) + .await + .unwrap(); + let (_tx, rx) = nari_pcm_channel(); + cancel.store(true, Ordering::SeqCst); + let mut events = Vec::new(); + assert_eq!( + session.run(rx, |event| events.push(event)).await, + Err(VoiceError::Cancelled) + ); + assert!(matches!( + events.last(), + Some(NariEvent::Finished(Err(VoiceError::Cancelled))) + )); + server.await.unwrap(); + } + #[test] fn silent_prompt_leaks_are_dropped_but_real_speech_is_kept() { for leak in [ From 0871234b59fd3166eb180b35cdacf14b9460cbe1 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:56:26 -0800 Subject: [PATCH 071/236] harness-api: carry tool call identity on history tool rows Clients rebuilding a transcript from History lost every tool card because tool rows had only their output. HistoryMessage gains an optional 'tool' (id, name, input) on role=tool rows. Additive and skipped when absent. --- crates/jcode-harness-api-server/src/translate.rs | 13 +++++++++++++ .../jcode-harness-api-server/src/translate_tests.rs | 3 +++ crates/jcode-harness-api/src/events.rs | 13 +++++++++++++ crates/jcode-sdk/src/lib.rs | 2 +- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/jcode-harness-api-server/src/translate.rs b/crates/jcode-harness-api-server/src/translate.rs index 1d70a9d2990..92e21b1eef6 100644 --- a/crates/jcode-harness-api-server/src/translate.rs +++ b/crates/jcode-harness-api-server/src/translate.rs @@ -1395,6 +1395,17 @@ impl BridgeState { } } + /// Tool identity of a daemon `tool` history row, if it carries one. + fn history_tool(message: &Value) -> Option { + let data = &message["tool_data"]; + let id = data["id"].as_str().filter(|id| !id.is_empty())?; + (message["role"] == "tool").then(|| jcode_harness_api::HistoryTool { + id: id.to_string(), + name: data["name"].as_str().unwrap_or("").to_string(), + input: data["input"].clone(), + }) + } + fn attachment_recovery(event: &Value) -> Option { if event["messages"].as_array().is_none_or(Vec::is_empty) || event["activity"]["is_processing"].as_bool() == Some(true) @@ -2307,6 +2318,7 @@ impl BridgeState { .unwrap_or(None), role: m["role"].as_str().unwrap_or("").to_string(), content: m["content"].as_str().unwrap_or("").to_string(), + tool: Self::history_tool(m), }) .collect() }) @@ -3466,6 +3478,7 @@ impl BridgeState { response_stats: None, role: role.to_string(), content, + tool: None, }) }) .take(limit) diff --git a/crates/jcode-harness-api-server/src/translate_tests.rs b/crates/jcode-harness-api-server/src/translate_tests.rs index 36ac0fcce19..d1b97d40414 100644 --- a/crates/jcode-harness-api-server/src/translate_tests.rs +++ b/crates/jcode-harness-api-server/src/translate_tests.rs @@ -3250,6 +3250,9 @@ fn history_image_boundaries_survive_loss_of_tool_data() { }; assert_eq!(messages.len(), 3); assert_eq!(messages[1].role, "tool"); + let tool = messages[1].tool.as_ref().expect("tool rows carry their call"); + assert_eq!((tool.id.as_str(), tool.name.as_str()), ("read-1", "read")); + assert!(messages[0].tool.is_none()); assert_eq!(images.len(), 1); assert_eq!(images[0].history_message_index, Some(2)); assert_eq!( diff --git a/crates/jcode-harness-api/src/events.rs b/crates/jcode-harness-api/src/events.rs index d76b38646a0..51e6bf2ebce 100644 --- a/crates/jcode-harness-api/src/events.rs +++ b/crates/jcode-harness-api/src/events.rs @@ -588,6 +588,19 @@ pub struct HistoryMessage { /// "user" | "assistant" | "tool". pub role: String, pub content: String, + /// On "tool" rows: the call this result answers, so clients can rebuild + /// tool cards after a reload. `content` is the tool output. Absent from + /// older servers. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub tool: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct HistoryTool { + pub id: String, + pub name: String, + #[serde(default, skip_serializing_if = "serde_json::Value::is_null")] + pub input: serde_json::Value, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] diff --git a/crates/jcode-sdk/src/lib.rs b/crates/jcode-sdk/src/lib.rs index 0235708c5fc..b92978c1395 100644 --- a/crates/jcode-sdk/src/lib.rs +++ b/crates/jcode-sdk/src/lib.rs @@ -198,7 +198,7 @@ pub use turn_retry::{FailureKind, RetryDecision, RetryPolicy, TurnRetry, classif /// The protocol types, re-exported so a client needs one dependency, not two. pub use jcode_harness_api as api; pub use jcode_harness_api::{ - ApiEvent, ApiRequest, HistoryMessage, ModelRouteInfo, PermissionDecision, RenderedImage, + ApiEvent, ApiRequest, HistoryMessage, HistoryTool, ModelRouteInfo, PermissionDecision, RenderedImage, RenderedImageAnchor, RenderedImageSource, ResponseStats, SessionInfo, SessionToolDefinition, SidePanelPage, SidePanelPageFormat, SidePanelPageSource, SidePanelSnapshot, TextMatch, ToolConfiguration, TurnStopReason, api_socket_path, From 42343bd43a35a25f406b9b4fd7441890d8f16abd Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:06:55 -0800 Subject: [PATCH 072/236] Add optional mobile command wakeups with polling fallback --- crates/jcode-base/src/mobile_runner.rs | 38 +- crates/jcode-base/src/mobile_runner_events.rs | 176 +++++++++ .../src/mobile_runner_events_tests.rs | 334 ++++++++++++++++++ docs/CLOUD_LATENCY.md | 42 +++ 4 files changed, 587 insertions(+), 3 deletions(-) create mode 100644 crates/jcode-base/src/mobile_runner_events.rs create mode 100644 crates/jcode-base/src/mobile_runner_events_tests.rs diff --git a/crates/jcode-base/src/mobile_runner.rs b/crates/jcode-base/src/mobile_runner.rs index 21e1568c87c..1cfd2882626 100644 --- a/crates/jcode-base/src/mobile_runner.rs +++ b/crates/jcode-base/src/mobile_runner.rs @@ -15,6 +15,8 @@ use serde::Deserialize; use std::path::PathBuf; use std::process::Stdio; use std::time::Duration; +#[path = "mobile_runner_events.rs"] +mod events; #[cfg(test)] #[path = "mobile_runner_poll_tests.rs"] mod poll_tests; @@ -442,10 +444,40 @@ pub fn ensure_sync_enabled() { } } -async fn poll_commands(mut run: F) +trait PollWait { + fn wait(&mut self, delay: Duration) -> impl std::future::Future + Send; +} + +impl PollWait for events::Wakeups { + async fn wait(&mut self, delay: Duration) { + events::Wakeups::wait(self, delay).await; + } +} + +#[cfg(test)] +struct SleepWait; + +#[cfg(test)] +impl PollWait for SleepWait { + async fn wait(&mut self, delay: Duration) { + tokio::time::sleep(delay).await; + } +} + +#[cfg(test)] +async fn poll_commands(run: F) +where + F: FnMut() -> Fut, + Fut: std::future::Future>, +{ + poll_commands_with_wait(run, SleepWait).await; +} + +async fn poll_commands_with_wait(mut run: F, mut wait: W) where F: FnMut() -> Fut, Fut: std::future::Future>, + W: PollWait, { loop { let delay = match run().await { @@ -459,7 +491,7 @@ where } }; let mut timing = StageTimer::new("pollwait"); - tokio::time::sleep(delay).await; + wait.wait(delay).await; timing.outcome = "ok"; } } @@ -472,7 +504,7 @@ pub fn spawn_cloud_runner() { ensure_sync_enabled(); tokio::spawn(async move { let client = reqwest::Client::new(); - poll_commands(|| run_one(&client)).await; + poll_commands_with_wait(|| run_one(&client), events::spawn()).await; }); } diff --git a/crates/jcode-base/src/mobile_runner_events.rs b/crates/jcode-base/src/mobile_runner_events.rs new file mode 100644 index 00000000000..90194b4060a --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_events.rs @@ -0,0 +1,176 @@ +//! Optional command wakeup hints. HTTP claims remain the only queue authority. +//! A disconnected, disabled or old API never disables the five-second fallback. + +use futures::{SinkExt, StreamExt}; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::watch; +use tokio::task::JoinHandle; +use tokio_tungstenite::tungstenite::{ + Message, client::IntoClientRequest, protocol::WebSocketConfig, +}; + +#[cfg(test)] +#[path = "mobile_runner_events_tests.rs"] +mod tests; + +type Source = Arc Option<(String, String)> + Send + Sync>; + +pub(super) struct Wakeups { + receiver: watch::Receiver, + task: JoinHandle<()>, +} + +impl Drop for Wakeups { + fn drop(&mut self) { + self.task.abort(); + } +} + +impl Wakeups { + pub(super) async fn wait(&mut self, delay: Duration) { + // A notification must not bypass the runner's error backoff. + if delay >= Duration::from_secs(20) { + tokio::time::sleep(delay).await; + return; + } + let deadline = tokio::time::Instant::now() + delay; + tokio::select! { + changed = self.receiver.changed() => { + if changed.is_err() { + tokio::time::sleep_until(deadline).await; + } + } + _ = tokio::time::sleep_until(deadline) => {} + } + } +} + +pub(super) fn spawn() -> Wakeups { + spawn_with( + Arc::new(|| { + let key = crate::subscription_catalog::configured_api_key()?; + Some((crate::subscription_api::configured_api_base(), key)) + }), + Duration::from_secs(30), + ) +} + +fn event_request( + base: &str, + key: &str, +) -> Result, ()> { + let mut url = reqwest::Url::parse(base).map_err(|_| ())?; + if !url.username().is_empty() + || url.password().is_some() + || url.query().is_some() + || url.fragment().is_some() + { + return Err(()); + } + let scheme = match url.scheme() { + "https" => "wss", + "http" => "ws", + _ => return Err(()), + }; + url.set_scheme(scheme).map_err(|_| ())?; + url.set_path(&format!( + "{}/mobile/commands/events", + url.path().trim_end_matches('/') + )); + let mut request = url.as_str().into_client_request().map_err(|_| ())?; + let value = format!("Bearer {key}").parse().map_err(|_| ())?; + request.headers_mut().insert("Authorization", value); + Ok(request) +} + +fn available(message: &str) -> bool { + #[derive(serde::Deserialize)] + #[serde(deny_unknown_fields)] + struct Hint { + #[serde(rename = "type")] + kind: String, + } + matches!(serde_json::from_str::(message), Ok(hint) if hint.kind == "commands_available") +} + +fn spawn_with(source: Source, retry: Duration) -> Wakeups { + let (sender, receiver) = watch::channel(0u64); + let task = tokio::spawn(async move { + loop { + let scope = source(); + let Some((base, key)) = scope.as_ref() else { + tokio::time::sleep(Duration::from_secs(5)).await; + continue; + }; + let mut cooldown = retry; + if let Ok(request) = event_request(base, key) { + let config = WebSocketConfig { + max_message_size: Some(4096), + max_frame_size: Some(4096), + max_write_buffer_size: 16384, + write_buffer_size: 0, + ..Default::default() + }; + let mut timing = super::StageTimer::new("event_connect"); + let connection = tokio::time::timeout( + Duration::from_secs(2), + tokio_tungstenite::connect_async_with_config(request, Some(config), true), + ) + .await; + match connection { + Ok(Ok((mut socket, _))) => { + timing.outcome = "ok"; + drop(timing); + let mut lifetime = super::StageTimer::new("event_connection"); + // Recheck the queue after subscribing, covering enqueue between + // the previous empty claim and WebSocket registration. + sender.send_modify(|version| *version = version.wrapping_add(1)); + let mut check = tokio::time::interval(Duration::from_secs(5)); + check.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + let mut heartbeat = tokio::time::Instant::now(); + loop { + tokio::select! { + message = socket.next() => match message { + Some(Ok(Message::Text(text))) if available(&text) => { + // A watch channel coalesces bursts, with constant memory. + sender.send_modify(|version| *version = version.wrapping_add(1)); + } + Some(Ok(Message::Ping(_) | Message::Pong(_))) => {} + _ => { lifetime.outcome = "error"; break; }, + }, + _ = check.tick() => { + if source() != scope { lifetime.outcome = "scope_changed"; break; } + if heartbeat.elapsed() >= Duration::from_secs(60) { + if !matches!(tokio::time::timeout(Duration::from_secs(1), socket.send(Message::Ping(Vec::new()))).await, Ok(Ok(()))) { + lifetime.outcome = "error"; + break; + } + heartbeat = tokio::time::Instant::now(); + } + } + } + } + } + Ok(Err(tokio_tungstenite::tungstenite::Error::Http(response))) + if matches!(response.status().as_u16(), 404 | 426 | 503) => + { + timing.outcome = "unavailable"; + cooldown = Duration::from_secs(300); + } + _ => timing.outcome = "error", + } + } + // Reconnects are bounded independently of normal HTTP queue claims. + // Credential or endpoint changes invalidate the cooldown within 5s. + let deadline = tokio::time::Instant::now() + cooldown; + while source() == scope && tokio::time::Instant::now() < deadline { + tokio::time::sleep_until( + (tokio::time::Instant::now() + Duration::from_secs(5)).min(deadline), + ) + .await; + } + } + }); + Wakeups { receiver, task } +} diff --git a/crates/jcode-base/src/mobile_runner_events_tests.rs b/crates/jcode-base/src/mobile_runner_events_tests.rs new file mode 100644 index 00000000000..6d775bf3c6f --- /dev/null +++ b/crates/jcode-base/src/mobile_runner_events_tests.rs @@ -0,0 +1,334 @@ +use super::*; +use std::sync::atomic::{AtomicUsize, Ordering}; +use tokio::net::{TcpListener, TcpStream}; +use tokio_tungstenite::{WebSocketStream, accept_hdr_async}; + +const HINT: &str = r#"{"type":"commands_available"}"#; +const FALLBACK: Duration = Duration::from_secs(5); + +async fn fixture() -> (Wakeups, WebSocketStream) { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let mut wakeups = spawn_with( + Arc::new(move || Some((base.clone(), "fixture-only-key".into()))), + Duration::from_secs(30), + ); + let (stream, _) = tokio::time::timeout(Duration::from_secs(2), listener.accept()) + .await + .unwrap() + .unwrap(); + let socket = accept_hdr_async( + stream, + |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| { + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-only-key" + ); + assert_eq!(request.uri().path(), "/mobile/commands/events"); + assert!(request.uri().query().is_none()); + Ok(response) + }, + ) + .await + .unwrap(); + // The subscription itself intentionally generates one queue recheck. + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + (wakeups, socket) +} + +#[tokio::test] +async fn changed_source_closes_old_socket_and_authenticates_only_new_endpoint() { + use std::sync::Mutex; + let old = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let new = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let source = Arc::new(Mutex::new(Some(( + format!("http://{}", old.local_addr().unwrap()), + "fixture-old-account".to_string(), + )))); + let current = source.clone(); + let mut wakeups = spawn_with( + Arc::new(move || current.lock().unwrap().clone()), + Duration::from_secs(30), + ); + let (stream, _) = tokio::time::timeout(Duration::from_secs(2), old.accept()) + .await + .unwrap() + .unwrap(); + let mut old_socket = accept_hdr_async( + stream, + |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| { + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-old-account" + ); + Ok(response) + }, + ) + .await + .unwrap(); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + *source.lock().unwrap() = Some(( + format!("http://{}", new.local_addr().unwrap()), + "fixture-new-account".into(), + )); + // Scope checks run every five seconds. The bound includes scheduling margin, + // not an assertion about connection latency or throughput. + let (stream, _) = tokio::time::timeout(Duration::from_secs(7), new.accept()) + .await + .expect("changed source must leave the old subscription and its cooldown") + .unwrap(); + let mut new_socket = accept_hdr_async( + stream, + |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| { + assert_eq!( + request.headers()["authorization"], + "Bearer fixture-new-account" + ); + assert!(request.uri().query().is_none()); + Ok(response) + }, + ) + .await + .unwrap(); + let end = tokio::time::timeout(Duration::from_secs(1), old_socket.next()) + .await + .expect("old account socket must be closed"); + assert!(!matches!(end, Some(Ok(Message::Text(_))))); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + new_socket.send(Message::Text(HINT.into())).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(80), old.accept()) + .await + .is_err() + ); +} + +#[test] +fn request_uses_bearer_header_without_credentials_in_url() { + let request = event_request("https://example.invalid/api/", "fixture-only-key").unwrap(); + assert_eq!( + request.uri().to_string(), + "wss://example.invalid/api/mobile/commands/events" + ); + assert_eq!( + request.headers()["Authorization"], + "Bearer fixture-only-key" + ); + assert!(!request.uri().to_string().contains("fixture-only-key")); + for base in [ + "https://user:password@example.invalid", + "https://example.invalid?key=secret", + "https://example.invalid#secret", + "ftp://example.invalid", + "not a URL", + ] { + assert!(event_request(base, "fixture-only-key").is_err(), "{base}"); + } + assert!(event_request("https://example.invalid", "bad\r\nInjected: value").is_err()); +} + +#[test] +fn hints_accept_only_the_exact_non_authoritative_shape() { + assert!(available(HINT)); + for text in [ + "", + "null", + "[]", + "{}", + r#"{"type":"other"}"#, + r#"{"type":"commands_available","command":"execute"}"#, + r#"{"type":"commands_available","type":"commands_available"}"#, + ] { + assert!(!available(text), "{text}"); + } +} + +#[tokio::test] +async fn notification_wakes_before_five_second_fallback() { + let (mut wakeups, mut socket) = fixture().await; + socket.send(Message::Text(HINT.into())).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .expect("notification should wake within one second, not the five-second fallback"); +} + +#[tokio::test] +async fn notification_drives_a_real_poll_loop_claim_without_waiting_five_seconds() { + let (wakeups, mut socket) = fixture().await; + let queued = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let calls = Arc::new(AtomicUsize::new(0)); + let claimed = Arc::new(tokio::sync::Notify::new()); + let queue = queued.clone(); + let count = calls.clone(); + let signal = claimed.clone(); + let task = tokio::spawn(super::super::poll_commands_with_wait( + move || { + count.fetch_add(1, Ordering::SeqCst); + let ran = queue.swap(false, Ordering::SeqCst); + if ran { + signal.notify_one(); + } + async move { Ok(ran) } + }, + wakeups, + )); + tokio::time::timeout(Duration::from_secs(1), async { + while calls.load(Ordering::SeqCst) == 0 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + let started = tokio::time::Instant::now(); + queued.store(true, Ordering::SeqCst); + socket.send(Message::Text(HINT.into())).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), claimed.notified()) + .await + .unwrap(); + println!( + "Controlled localhost WebSocket hint to polling-loop claim: {:?}, not production latency", + started.elapsed() + ); + task.abort(); + assert!(task.await.unwrap_err().is_cancelled()); +} + +#[tokio::test] +async fn notification_is_buffered_and_bursts_coalesce() { + let (mut wakeups, mut socket) = fixture().await; + let initial = *wakeups.receiver.borrow(); + for _ in 0..128 { + socket.send(Message::Text(HINT.into())).await.unwrap(); + } + tokio::time::timeout(Duration::from_secs(2), async { + while *wakeups.receiver.borrow() != initial + 128 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + // All frames arrived before wait. A watch channel stores only the latest u64. + tokio::time::timeout(Duration::from_secs(1), wakeups.wait(FALLBACK)) + .await + .unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(80), wakeups.wait(FALLBACK)) + .await + .is_err() + ); +} + +#[tokio::test] +async fn dropped_socket_keeps_timer_fallback() { + let (mut wakeups, socket) = fixture().await; + drop(socket); + assert!( + tokio::time::timeout( + Duration::from_millis(60), + wakeups.wait(Duration::from_millis(180)) + ) + .await + .is_err() + ); + tokio::time::timeout( + Duration::from_secs(1), + wakeups.wait(Duration::from_millis(100)), + ) + .await + .unwrap(); +} + +#[tokio::test] +async fn dropping_wakeups_cancels_connection_task_and_socket() { + let (wakeups, mut socket) = fixture().await; + let task = wakeups.task.abort_handle(); + drop(wakeups); + let result = tokio::time::timeout(Duration::from_secs(1), socket.next()) + .await + .expect("dropping wakeups must close its websocket"); + assert!(!matches!(result, Some(Ok(Message::Text(_))))); + tokio::time::timeout(Duration::from_secs(1), async { + while !task.is_finished() { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); +} + +#[tokio::test] +async fn malformed_and_oversize_frames_fail_closed() { + for frame in [ + Message::Text("not JSON".into()), + Message::Text("x".repeat(4097)), + Message::Binary(vec![0; 16]), + ] { + let (mut wakeups, mut socket) = fixture().await; + socket.send(frame).await.unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(100), wakeups.wait(FALLBACK)) + .await + .is_err() + ); + let end = tokio::time::timeout(Duration::from_secs(1), socket.next()) + .await + .expect("invalid frames must terminate the connection"); + assert!(!matches!(end, Some(Ok(Message::Text(_))))); + } +} + +#[tokio::test(start_paused = true)] +async fn error_backoff_cannot_be_bypassed_by_buffered_hint() { + let (sender, receiver) = watch::channel(0u64); + let task = tokio::spawn(std::future::pending()); + let mut wakeups = Wakeups { receiver, task }; + sender.send_modify(|v| *v += 1); + let start = tokio::time::Instant::now(); + wakeups.wait(Duration::from_secs(20)).await; + assert!(start.elapsed() >= Duration::from_secs(20)); + // Backoff did not consume the pending queue hint. + assert!(wakeups.receiver.has_changed().unwrap()); +} + +#[tokio::test] +async fn unavailable_endpoint_does_not_hot_loop_and_fallback_remains_live() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let attempts = Arc::new(AtomicUsize::new(0)); + let count = attempts.clone(); + let server = tokio::spawn(async move { + loop { + let (mut stream, _) = listener.accept().await.unwrap(); + count.fetch_add(1, Ordering::SeqCst); + let mut request = [0; 4096]; + stream.read(&mut request).await.unwrap(); + stream.write_all(b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").await.unwrap(); + } + }); + let mut wakeups = spawn_with( + Arc::new(move || Some((base.clone(), "fixture-only-key".into()))), + Duration::from_millis(1), + ); + tokio::time::timeout(Duration::from_secs(1), async { + while attempts.load(Ordering::SeqCst) == 0 { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + let start = tokio::time::Instant::now(); + wakeups.wait(Duration::from_millis(150)).await; + assert!(start.elapsed() >= Duration::from_millis(150)); + assert_eq!(attempts.load(Ordering::SeqCst), 1); + server.abort(); +} diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index b91a481ef61..148ed255e74 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -164,6 +164,48 @@ metric rather than changing command handling. Logs contain only the duration, not command IDs, text or timestamps. Paused-clock regression comparisons prove the eliminated five-second scheduling gaps, not production end-to-end latency. +### Optional command wakeups + +The daemon can subscribe to `GET /v1/mobile/commands/events` using its normal +account bearer header over a WebSocket. The only accepted application frame is +`{"type":"commands_available"}`. It is a hint to perform the existing atomic +HTTP claim, never an executable command. Initial subscription triggers a queue +recheck to cover commands queued between an empty claim and socket registration. +Bursts coalesce in a bounded watch channel. Messages/frames are capped at 4KiB. + +The five-second claim fallback remains active even with a socket, protecting +against lost hints, service restart and unsupported APIs. Error backoff still +cannot be bypassed by a hint. Connection attempts have a two-second deadline, +network retry cooldown, and a five-minute cooldown for disabled/old endpoints. +Endpoint and credential changes discard the old socket. Cancellation aborts the +socket task. Fixed `event_connect` and `event_connection` records contain no +credentials, endpoint, account, command text or raw network error. + +The corresponding backend candidate uses an account-scoped Durable Object with +the WebSocket Hibernation API and at most four sockets per account. D1 remains +the queue authority. A committed enqueue publishes a bounded best-effort hint +before the existing host wake step. Failed publication cannot lose or invalidate +the command. The backend logs static `mobile_command_events` notify durations +and outcomes without payloads. No queue data is stored in the socket object. +Actual production hibernation/eviction and billing have not been measured. + +**Rollout is gated.** The backend's `MOBILE_COMMAND_EVENTS_ENABLED` remains false. +Backend commits `f3f1dd8` and `6e87e1d` are on the pre-existing +`feat/browser-code-subscribe` branch in `solosystems-backend`, not a production +deployment or permission to merge unrelated branch changes. Integrate only +those reviewed commits, deploy the binding/migration with the flag disabled, +validate account isolation, then explicitly enable it and monitor queue wait. +Update the managed daemon only when idle and after resolving its existing +pending runtime. Do not replace a separately staged package or force a busy +daemon restart for a benchmark. + +On November 8, 2026, read-only rollout checks found one active cloud turn and +an already-pending runtime, so neither was modified. No Apple USB device was +present and usbmuxd was inactive, preventing real-device install/paint timing. +The configured account's read-only command history request returned HTTP 403, +so no historical queue percentile is claimed. These are rollout/measurement +limitations, not evidence that the new event path is active in production. + The separate `jcode-mobile` repository's phone transcript transport now pools HTTP connections too (commit `a98b496`). Eight actual list calls in a loopback fixture with 30ms connection setup and 8ms body delay improved from median From 0eb1105dbb8f5cfea0965d8b8c3e9fe9b350ef07 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:10:10 -0800 Subject: [PATCH 073/236] tui: cut per-frame release CPU (memoize section byte estimate, FNV row hash, slice clear_area) --- crates/jcode-tui-render/src/chrome.rs | 13 +++++-- .../jcode-tui/src/tui/ui_memory_estimates.rs | 31 ++++++++++++++- crates/jcode-tui/src/tui/ui_smoothness.rs | 38 ++++++++++++------- 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/crates/jcode-tui-render/src/chrome.rs b/crates/jcode-tui-render/src/chrome.rs index cf24a169b6a..99d205cc32e 100644 --- a/crates/jcode-tui-render/src/chrome.rs +++ b/crates/jcode-tui-render/src/chrome.rs @@ -2,9 +2,16 @@ use ratatui::prelude::*; use ratatui::widgets::{Block, Borders, Paragraph}; pub fn clear_area(frame: &mut Frame, area: Rect) { - for x in area.left()..area.right() { - for y in area.top()..area.bottom() { - frame.buffer_mut()[(x, y)].reset(); + let buffer = frame.buffer_mut(); + let area = area.intersection(buffer.area); + if area.is_empty() { + return; + } + let width = usize::from(area.width); + for y in area.top()..area.bottom() { + let start = buffer.index_of(area.left(), y); + for cell in &mut buffer.content[start..start + width] { + cell.reset(); } } } diff --git a/crates/jcode-tui/src/tui/ui_memory_estimates.rs b/crates/jcode-tui/src/tui/ui_memory_estimates.rs index 1b709c647b0..33b0bb27920 100644 --- a/crates/jcode-tui/src/tui/ui_memory_estimates.rs +++ b/crates/jcode-tui/src/tui/ui_memory_estimates.rs @@ -96,6 +96,35 @@ pub(super) fn estimate_prepared_messages_bytes(prepared: &PreparedMessages) -> u + estimate_copy_targets_bytes(&prepared.copy_targets) } +/// Sections share immutable `Arc` bodies across frames. +/// Walking a large body (tens of thousands of lines) on every streaming frame +/// was ~10% of release-build client CPU, so remember the estimate per Arc. +/// `Weak` keeps the pointer from being reused for a different allocation. +fn estimate_shared_prepared_messages_bytes(prepared: &Arc) -> usize { + use std::cell::RefCell; + use std::sync::Weak; + const MEMO_CAP: usize = 8; + thread_local! { + static MEMO: RefCell, usize)>> = const { RefCell::new(Vec::new()) }; + } + MEMO.with(|memo| { + let mut memo = memo.borrow_mut(); + if let Some((_, bytes)) = memo + .iter() + .find(|(weak, _)| std::ptr::eq(weak.as_ptr(), Arc::as_ptr(prepared))) + { + return *bytes; + } + let bytes = estimate_prepared_messages_bytes(prepared); + memo.retain(|(weak, _)| weak.strong_count() > 0); + if memo.len() >= MEMO_CAP { + memo.remove(0); + } + memo.push((Arc::downgrade(prepared), bytes)); + bytes + }) +} + pub(super) fn estimate_prepared_chat_frame_bytes(prepared: &PreparedChatFrame) -> usize { // Include each section's prepared content: the wrapped lines dominate a // frame's real memory footprint. Without this, multi-megabyte transcripts @@ -104,7 +133,7 @@ pub(super) fn estimate_prepared_chat_frame_bytes(prepared: &PreparedChatFrame) - prepared .sections .iter() - .map(|section| estimate_prepared_messages_bytes(§ion.prepared)) + .map(|section| estimate_shared_prepared_messages_bytes(§ion.prepared)) .sum::() + prepared.sections.capacity() * std::mem::size_of::() + estimate_usize_vec_bytes(&prepared.wrapped_user_indices) diff --git a/crates/jcode-tui/src/tui/ui_smoothness.rs b/crates/jcode-tui/src/tui/ui_smoothness.rs index dee578fcbd6..7dc0f50fef5 100644 --- a/crates/jcode-tui/src/tui/ui_smoothness.rs +++ b/crates/jcode-tui/src/tui/ui_smoothness.rs @@ -25,23 +25,33 @@ fn recorder() -> &'static Mutex { /// Hash one buffer row's cell symbols within `area`. Returns /// [`BLANK_ROW_HASH`] for visually blank rows. fn hash_row(buffer: &Buffer, area: Rect, y: u16) -> u64 { - use std::hash::{Hash, Hasher}; - let mut hasher = std::collections::hash_map::DefaultHasher::new(); + // FNV-1a over symbol bytes: this runs over every visible cell each frame, + // and SipHash via `DefaultHasher` was a measurable share of release CPU. + const PRIME: u64 = 0x0000_0100_0000_01b3; + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; let mut blank = true; - for x in area.left()..area.right() { - if let Some(cell) = buffer.cell((x, y)) { - let symbol = cell.symbol(); - if blank && !symbol.trim().is_empty() { - blank = false; - } - symbol.hash(&mut hasher); - } + let buf_area = *buffer.area(); + if y < buf_area.top() || y >= buf_area.bottom() { + return BLANK_ROW_HASH; + } + let left = area.left().max(buf_area.left()); + let right = area.right().min(buf_area.right()); + if left >= right { + return BLANK_ROW_HASH; } - if blank { - BLANK_ROW_HASH - } else { - hasher.finish() + let start = buffer.index_of(left, y); + for cell in &buffer.content[start..start + usize::from(right - left)] { + let symbol = cell.symbol(); + if blank && !symbol.trim().is_empty() { + blank = false; + } + for &byte in symbol.as_bytes() { + hash = (hash ^ u64::from(byte)).wrapping_mul(PRIME); + } + // Cell separator so "ab","c" differs from "a","bc". + hash = (hash ^ 0xff).wrapping_mul(PRIME); } + if blank { BLANK_ROW_HASH } else { hash } } /// Build an [`AnchorFrame`] from a rendered buffer region. Shared by the live From 629cca346f71f6c051cf8570dbea35cd0b20d5a2 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:10:54 -0800 Subject: [PATCH 074/236] tui: reuse last_frame buffer allocation each full draw --- crates/jcode-tui/src/tui/app/run_shell.rs | 13 ++++++- crates/jcode-tui/src/tui/backend.rs | 45 +++++++++++++++++------ crates/jcode-tui/src/tui/backend_tests.rs | 18 +++++++++ 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/run_shell.rs b/crates/jcode-tui/src/tui/app/run_shell.rs index a9b10e78156..2d186f93f21 100644 --- a/crates/jcode-tui/src/tui/app/run_shell.rs +++ b/crates/jcode-tui/src/tui/app/run_shell.rs @@ -523,7 +523,10 @@ impl StatusSpinnerRenderer { app.force_full_redraw = false; app.force_full_repaint = false; - let previous_frame = self.last_frame.as_ref(); + // Take the previous frame so its allocation can be reused below + // (`clone_from`) instead of allocating a whole new buffer per frame. + let mut stored_frame = self.last_frame.take(); + let previous_frame = stored_frame.as_ref(); let draw_start = Instant::now(); let mut render_elapsed = Duration::ZERO; super::oauth_telemetry::begin_frame(); @@ -544,7 +547,13 @@ impl StatusSpinnerRenderer { .count() }); let total_cells = Some(completed.buffer.content.len()); - let completed_buffer = completed.buffer.clone(); + let completed_buffer = match stored_frame.as_mut() { + Some(buffer) => { + buffer.clone_from(completed.buffer); + stored_frame.take().unwrap_or_default() + } + None => completed.buffer.clone(), + }; // `completed` borrows the terminal; it is unused past this point, so the // borrow ends here (NLL) before we touch the backend again below. let frame_released = diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index 8c66c7a0a7b..565bacef549 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -245,6 +245,11 @@ pub struct BackendInfo { pub struct RemoteConnection { reader: BufReader, writer: Arc>, + /// FIFO outbound queue drained by a dedicated writer task. The UI event + /// loop must never await socket writes: if the daemon's receive buffer is + /// full while the daemon is itself blocked writing to us, awaiting the + /// write here would stop us reading and deadlock both ends. + outbound: std::sync::Mutex>>>, _dummy_peer: Option, session_id: Option, cloud_authority: Option>, @@ -416,6 +421,7 @@ impl RemoteConnection { let mut conn = Self { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), + outbound: std::sync::Mutex::new(None), _dummy_peer: None, session_id: None, cloud_authority: None, @@ -610,25 +616,17 @@ impl RemoteConnection { let mut timing = super::cloud_latency::begin_request(&request); let total_start = Instant::now(); - let writer_wait_start = Instant::now(); - let mut w = self.writer.lock().await; - let writer_wait_ms = writer_wait_start.elapsed().as_millis(); - let write_start = Instant::now(); - let result = w.write_all(json.as_bytes()).await; + let result = self.enqueue_outbound(json.into_bytes()); if let Some(fields) = &interrupt_log { match &result { Ok(()) => crate::logging::info(&format!( - "REMOTE_INTERRUPT_SEND_OK {} writer_wait_ms={} write_ms={} total_ms={}", + "REMOTE_INTERRUPT_SEND_OK {} queued total_ms={}", fields, - writer_wait_ms, - write_start.elapsed().as_millis(), total_start.elapsed().as_millis() )), Err(error) => crate::logging::warn(&format!( - "REMOTE_INTERRUPT_SEND_ERR {} writer_wait_ms={} write_ms={} total_ms={} error={}", + "REMOTE_INTERRUPT_SEND_ERR {} total_ms={} error={}", fields, - writer_wait_ms, - write_start.elapsed().as_millis(), total_start.elapsed().as_millis(), error )), @@ -642,6 +640,30 @@ impl RemoteConnection { Ok(()) } + /// Queue bytes for the connection writer task, spawning it on first use. + /// Never waits on the socket, so the caller keeps draining server events. + fn enqueue_outbound(&self, bytes: Vec) -> Result<()> { + let mut slot = self.outbound.lock().unwrap_or_else(|e| e.into_inner()); + if slot.as_ref().is_none_or(|tx| tx.is_closed()) { + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::>(); + let writer = Arc::clone(&self.writer); + tokio::spawn(async move { + while let Some(bytes) = rx.recv().await { + let mut w = writer.lock().await; + if let Err(error) = w.write_all(&bytes).await { + crate::logging::warn(&format!("Remote writer task stopped: {error}")); + break; + } + } + }); + *slot = Some(tx); + } + slot.as_ref() + .expect("outbound sender initialized") + .send(bytes) + .map_err(|_| anyhow::anyhow!("remote connection writer closed")) + } + async fn send_request(&self, request: Request) -> Result<()> { self.send_request_with_interrupt_trigger(request, None) .await @@ -1508,6 +1530,7 @@ impl RemoteConnection { Self { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), + outbound: std::sync::Mutex::new(None), _dummy_peer: Some(b), session_id: None, cloud_authority: None, diff --git a/crates/jcode-tui/src/tui/backend_tests.rs b/crates/jcode-tui/src/tui/backend_tests.rs index 642d19a83e9..45e1fa231fc 100644 --- a/crates/jcode-tui/src/tui/backend_tests.rs +++ b/crates/jcode-tui/src/tui/backend_tests.rs @@ -806,3 +806,21 @@ fn remote_protocol_frame_limit_rejects_oversize_and_overflow() { )); assert!(remote_protocol_frame_exceeds_limit(usize::MAX, 1)); } + +#[tokio::test] +async fn request_send_does_not_block_ui_when_server_stops_reading() { + // Regression: the TUI awaited socket writes inline, so when the daemon's + // receive buffer filled while it was blocked writing to us, the UI loop + // stopped reading and both ends deadlocked (frozen session). + let mut remote = RemoteConnection::dummy(); + let writer = remote.writer(); + let _guard = writer.lock().await; + + let result = tokio::time::timeout(Duration::from_millis(200), async { + for _ in 0..64 { + remote.request_todos().await.expect("queued"); + } + }) + .await; + assert!(result.is_ok(), "request sends must not wait on the socket writer"); +} From 5ca28ecf72b735abf8dc81845839d07ec32790b1 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:14:10 -0800 Subject: [PATCH 075/236] tui: never await socket writes on the UI loop, fixing client/server write deadlock The TUI awaited write_all for every request on its event loop. When the daemon's receive buffer filled while the daemon was blocked writing events to this client, the TUI stopped reading and both ends deadlocked, freezing the session (seen on owl: 213KB stuck in each direction). Requests now go through a per-connection writer task; senders wait at most 250ms for the write result (and not at all when a backlog exists) so broken pipes still surface. --- crates/jcode-tui/src/tui/backend.rs | 65 +++++++++++++++++++---- crates/jcode-tui/src/tui/backend_tests.rs | 2 +- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/crates/jcode-tui/src/tui/backend.rs b/crates/jcode-tui/src/tui/backend.rs index 565bacef549..60ec03c2390 100644 --- a/crates/jcode-tui/src/tui/backend.rs +++ b/crates/jcode-tui/src/tui/backend.rs @@ -249,7 +249,10 @@ pub struct RemoteConnection { /// loop must never await socket writes: if the daemon's receive buffer is /// full while the daemon is itself blocked writing to us, awaiting the /// write here would stop us reading and deadlock both ends. - outbound: std::sync::Mutex>>>, + outbound: std::sync::Mutex>>, + /// Queued writes not yet flushed. When a backlog exists, senders do not + /// wait for their own write result at all. + outbound_pending: Arc, _dummy_peer: Option, session_id: Option, cloud_authority: Option>, @@ -281,6 +284,12 @@ pub struct RemoteConnection { } const DETACHED_REQUEST_TIMEOUT: Duration = Duration::from_secs(2); +/// Longest a request send waits for its own write result before returning. +const OUTBOUND_RESULT_WAIT: Duration = Duration::from_millis(250); +type OutboundWrite = ( + Vec, + tokio::sync::oneshot::Sender>, +); const MAX_STRAY_REMOTE_PROTOCOL_LINES: usize = 32; /// Hard cap for one newline-delimited server event. History events can be large /// because they may contain images, but an authenticated or compromised peer @@ -422,6 +431,7 @@ impl RemoteConnection { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), outbound: std::sync::Mutex::new(None), + outbound_pending: Default::default(), _dummy_peer: None, session_id: None, cloud_authority: None, @@ -616,7 +626,22 @@ impl RemoteConnection { let mut timing = super::cloud_latency::begin_request(&request); let total_start = Instant::now(); - let result = self.enqueue_outbound(json.into_bytes()); + let backlogged = self + .outbound_pending + .load(std::sync::atomic::Ordering::Acquire) + > 0; + let result = match self.enqueue_outbound(json.into_bytes()) { + Err(error) => Err(error), + Ok(_) if backlogged => Ok(()), + // Surface prompt write failures (broken pipe) to the caller, but + // never wait longer than a short bound: a full socket must not + // stall the loop that drains server events. + Ok(done) => match tokio::time::timeout(OUTBOUND_RESULT_WAIT, done).await { + Ok(Ok(Err(error))) => Err(anyhow::anyhow!(error)), + Ok(Err(_)) => Err(anyhow::anyhow!("remote connection writer closed")), + Ok(Ok(Ok(()))) | Err(_) => Ok(()), + }, + }; if let Some(fields) = &interrupt_log { match &result { Ok(()) => crate::logging::info(&format!( @@ -642,26 +667,47 @@ impl RemoteConnection { /// Queue bytes for the connection writer task, spawning it on first use. /// Never waits on the socket, so the caller keeps draining server events. - fn enqueue_outbound(&self, bytes: Vec) -> Result<()> { + /// The returned receiver resolves with the write result. + fn enqueue_outbound( + &self, + bytes: Vec, + ) -> Result>> { let mut slot = self.outbound.lock().unwrap_or_else(|e| e.into_inner()); if slot.as_ref().is_none_or(|tx| tx.is_closed()) { - let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::>(); + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::(); let writer = Arc::clone(&self.writer); + // Writes dropped with a dead writer task never flushed. + self.outbound_pending + .store(0, std::sync::atomic::Ordering::Release); + let pending = Arc::clone(&self.outbound_pending); tokio::spawn(async move { - while let Some(bytes) = rx.recv().await { + while let Some((bytes, done)) = rx.recv().await { let mut w = writer.lock().await; - if let Err(error) = w.write_all(&bytes).await { - crate::logging::warn(&format!("Remote writer task stopped: {error}")); + let result = w.write_all(&bytes).await; + drop(w); + pending.fetch_sub(1, std::sync::atomic::Ordering::AcqRel); + let failed = result.is_err(); + let _ = done.send(result.map_err(|error| error.to_string())); + if failed { + crate::logging::warn("Remote writer task stopped after write error"); break; } } }); *slot = Some(tx); } + let (done_tx, done_rx) = tokio::sync::oneshot::channel(); + self.outbound_pending + .fetch_add(1, std::sync::atomic::Ordering::AcqRel); slot.as_ref() .expect("outbound sender initialized") - .send(bytes) - .map_err(|_| anyhow::anyhow!("remote connection writer closed")) + .send((bytes, done_tx)) + .map_err(|_| { + self.outbound_pending + .fetch_sub(1, std::sync::atomic::Ordering::AcqRel); + anyhow::anyhow!("remote connection writer closed") + })?; + Ok(done_rx) } async fn send_request(&self, request: Request) -> Result<()> { @@ -1531,6 +1577,7 @@ impl RemoteConnection { reader: BufReader::new(reader), writer: Arc::new(Mutex::new(writer)), outbound: std::sync::Mutex::new(None), + outbound_pending: Default::default(), _dummy_peer: Some(b), session_id: None, cloud_authority: None, diff --git a/crates/jcode-tui/src/tui/backend_tests.rs b/crates/jcode-tui/src/tui/backend_tests.rs index 45e1fa231fc..e8f3517ebae 100644 --- a/crates/jcode-tui/src/tui/backend_tests.rs +++ b/crates/jcode-tui/src/tui/backend_tests.rs @@ -816,7 +816,7 @@ async fn request_send_does_not_block_ui_when_server_stops_reading() { let writer = remote.writer(); let _guard = writer.lock().await; - let result = tokio::time::timeout(Duration::from_millis(200), async { + let result = tokio::time::timeout(Duration::from_secs(1), async { for _ in 0..64 { remote.request_todos().await.expect("queued"); } From 5d5053223bb798058fa1e22c012aa8e451fe96b7 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:21:26 -0800 Subject: [PATCH 076/236] tui: reuse last_frame cell storage via Vec::clone_from --- crates/jcode-tui/src/tui/app/run_shell.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/jcode-tui/src/tui/app/run_shell.rs b/crates/jcode-tui/src/tui/app/run_shell.rs index 2d186f93f21..ab453b42765 100644 --- a/crates/jcode-tui/src/tui/app/run_shell.rs +++ b/crates/jcode-tui/src/tui/app/run_shell.rs @@ -549,7 +549,10 @@ impl StatusSpinnerRenderer { let total_cells = Some(completed.buffer.content.len()); let completed_buffer = match stored_frame.as_mut() { Some(buffer) => { - buffer.clone_from(completed.buffer); + // Derived `Buffer::clone_from` reallocates; `Vec::clone_from` + // reuses the existing cell storage. + buffer.area = completed.buffer.area; + buffer.content.clone_from(&completed.buffer.content); stored_frame.take().unwrap_or_default() } None => completed.buffer.clone(), From 83ae68ad22f1ee401e281ded2b8697a1e08e1c77 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:27:16 -0800 Subject: [PATCH 077/236] Refuse cloud restart when activity cannot be verified --- src/cli/cloud_runtime.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/cli/cloud_runtime.rs b/src/cli/cloud_runtime.rs index 35d3a39c759..eba2e35a077 100644 --- a/src/cli/cloud_runtime.rs +++ b/src/cli/cloud_runtime.rs @@ -45,12 +45,15 @@ fn reload_command(wait_secs: u64, force: bool) -> String { r#"set -u export XDG_RUNTIME_DIR=/run/user/$(id -u) say() {{ echo "[cloud reload] $*" >&2; }} -streaming() {{ /opt/jcode-cloud/jcode menubar --json 2>/dev/null | sed -n 's/.*"streaming":\([0-9]*\).*/\1/p'; }} +streaming() {{ + activity=$(/opt/jcode-cloud/jcode --no-update --no-selfdev menubar --json 2>/dev/null) || return 1 + printf '%s' "$activity" | python3 -c 'import json,sys; n=json.load(sys.stdin).get("streaming"); sys.exit(1) if type(n) is not int or n < 0 else print(n)' 2>/dev/null +}} deadline=$(( $(date +%s) + {wait_secs} )) last="" if [ {force} = 0 ]; then while :; do - n=$(streaming); n=${{n:-0}} + n=$(streaming) || {{ say "cannot verify cloud activity; not restarting"; exit 3; }} [ "$n" = 0 ] && break if [ "$(date +%s)" -ge "$deadline" ]; then say "still $n session(s) mid-turn after {wait_secs}s; not restarting. Retry later or use --force."; exit 3; fi [ "$n" != "$last" ] && say "waiting for $n session(s) to finish their turn" @@ -322,6 +325,39 @@ mod tests { ); } + #[test] + fn runtime_reload_requires_a_verified_nonnegative_idle_count() { + // Execute only the idle gate. No test can reach service stop/start code. + let generated = reload_command(0, false); + let gate = generated.split("before=$(readlink").next().unwrap(); + for (json, status, idle) in [ + (r#"{"streaming":0}"#, 0, true), + (r#"{"streaming":2}"#, 0, false), + (r#"{"streaming":0}"#, 1, false), + ("", 0, false), + ("not json", 0, false), + ("{}", 0, false), + (r#"{"streaming":false}"#, 0, false), + (r#"{"streaming":-1}"#, 0, false), + (r#"{"streaming":"0"}"#, 0, false), + ] { + let mock = r#"sh -c 'printf %s "$MOCK_ACTIVITY"; exit $MOCK_STATUS' --"#; + let script = gate.replace("/opt/jcode-cloud/jcode", mock) + "printf verified_idle"; + let result = Command::new("sh") + .args(["-c", &script]) + .env("MOCK_ACTIVITY", json) + .env("MOCK_STATUS", status.to_string()) + .output() + .unwrap(); + assert_eq!(result.status.success(), idle, "{json:?}, status={status}"); + assert_eq!(result.stdout == b"verified_idle", idle); + if !idle { + assert_eq!(result.status.code(), Some(3)); + assert!(String::from_utf8_lossy(&result.stderr).contains("not restarting")); + } + } + } + #[test] fn runtime_upload_hashes_and_rewinds_the_same_open_file() { let mut file = tempfile::NamedTempFile::new().unwrap(); From a74b455691266ecdf6dd45d888b5cf07556a1503 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:28:18 -0800 Subject: [PATCH 078/236] fix(voice): use bundled TLS roots for Nari connections on iOS --- crates/jcode-voice/Cargo.toml | 4 ++- crates/jcode-voice/src/nari.rs | 59 +++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/crates/jcode-voice/Cargo.toml b/crates/jcode-voice/Cargo.toml index 380f6f50c7a..eb71eeffbcc 100644 --- a/crates/jcode-voice/Cargo.toml +++ b/crates/jcode-voice/Cargo.toml @@ -26,7 +26,9 @@ reqwest = { version = "0.12", optional = true, default-features = false, feature serde = { version = "1", optional = true, features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync", "time"] } -tokio-tungstenite = { version = "0.24", default-features = false, features = ["connect", "rustls-tls-native-roots"] } +# Use an explicit bundled-root connector, never the platform filesystem CA loader. +tokio-tungstenite = { version = "0.24", default-features = false, features = ["connect", "__rustls-tls"] } +webpki-roots = "1" rustls = { version = "0.23", default-features = false, features = ["aws_lc_rs"] } # Native microphone input is opt-in so CLI builds need no audio development libraries. diff --git a/crates/jcode-voice/src/nari.rs b/crates/jcode-voice/src/nari.rs index 69fe38915a7..da9a2f15732 100644 --- a/crates/jcode-voice/src/nari.rs +++ b/crates/jcode-voice/src/nari.rs @@ -309,6 +309,25 @@ pub(super) async fn cancelled(cancel: &AtomicBool) { } type Socket = WebSocketStream>; + +fn bundled_root_store() -> rustls::RootCertStore { + rustls::RootCertStore { + roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(), + } +} + +/// iOS is Unix but has no OpenSSL CA bundle. rustls-native-certs 0.8 uses +/// its Unix filesystem loader there, not the Apple trust store. An explicit +/// connector also avoids Cargo feature unification selecting native roots or +/// another TLS backend. Keep rustls's default chain, time and hostname checks. +fn tls_connector() -> tokio_tungstenite::Connector { + tokio_tungstenite::Connector::Rustls(Arc::new( + rustls::ClientConfig::builder() + .with_root_certificates(bundled_root_store()) + .with_no_client_auth(), + )) +} + pub struct NariSession { socket: Socket, cancel: Arc, @@ -389,10 +408,14 @@ impl NariSession { max_frame_size: Some(MAX_RESPONSE_BYTES), ..Default::default() }; - let (mut socket, _) = - tokio_tungstenite::connect_async_with_config(request, Some(config), false) - .await - .map_err(handshake_error)?; + let (mut socket, _) = tokio_tungstenite::connect_async_tls_with_config( + request, + Some(config), + false, + Some(tls_connector()), + ) + .await + .map_err(handshake_error)?; super::timing::mark("nari websocket connected"); send(&mut socket, json!({"type":"session.configure", "session":{"model":"qwen3-asr-fast", "turn_detection":null, "language":"en", "prompt":prompt}})).await?; loop { @@ -667,6 +690,34 @@ impl TranscriptState { #[cfg(test)] mod tests { use super::*; + + #[test] + fn bundled_tls_roots_are_complete_without_native_store() { + // No native loader or environment override participates in this store. + // Run with SSL_CERT_FILE/SSL_CERT_DIR pointing to missing paths as well. + let roots = bundled_root_store(); + assert!(!roots.is_empty()); + assert_eq!(roots.len(), webpki_roots::TLS_SERVER_ROOTS.len()); + for (actual, expected) in roots.roots.iter().zip(webpki_roots::TLS_SERVER_ROOTS) { + assert_eq!(actual, expected); + } + } + + #[test] + fn bundled_tls_connector_uses_rustls_even_with_unified_features() { + let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); + let tokio_tungstenite::Connector::Rustls(config) = tls_connector() else { + panic!("voice must use the explicit bundled-root rustls connector"); + }; + assert!(config.enable_sni); + assert!(!config.client_auth_cert_resolver.has_certs()); + let connection = rustls::ClientConnection::new( + config, + rustls::pki_types::ServerName::try_from("relay.example").unwrap(), + ); + assert!(connection.is_ok()); + } + #[tokio::test] async fn relay_rejects_unsafe_urls_and_invalid_bearers_before_network() { for url in [ From 9904e71cd16a2c050e34aa5c62efe006a643e216 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:30:23 -0800 Subject: [PATCH 079/236] Record enabled production mobile wakeups and safe runtime staging --- docs/CLOUD_LATENCY.md | 48 ++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/docs/CLOUD_LATENCY.md b/docs/CLOUD_LATENCY.md index 148ed255e74..c7e527ac2e6 100644 --- a/docs/CLOUD_LATENCY.md +++ b/docs/CLOUD_LATENCY.md @@ -189,22 +189,38 @@ the command. The backend logs static `mobile_command_events` notify durations and outcomes without payloads. No queue data is stored in the socket object. Actual production hibernation/eviction and billing have not been measured. -**Rollout is gated.** The backend's `MOBILE_COMMAND_EVENTS_ENABLED` remains false. -Backend commits `f3f1dd8` and `6e87e1d` are on the pre-existing -`feat/browser-code-subscribe` branch in `solosystems-backend`, not a production -deployment or permission to merge unrelated branch changes. Integrate only -those reviewed commits, deploy the binding/migration with the flag disabled, -validate account isolation, then explicitly enable it and monitor queue wait. -Update the managed daemon only when idle and after resolving its existing -pending runtime. Do not replace a separately staged package or force a busy -daemon restart for a benchmark. - -On November 8, 2026, read-only rollout checks found one active cloud turn and -an already-pending runtime, so neither was modified. No Apple USB device was -present and usbmuxd was inactive, preventing real-device install/paint timing. -The configured account's read-only command history request returned HTTP 403, -so no historical queue percentile is claimed. These are rollout/measurement -limitations, not evidence that the new event path is active in production. +**Backend enabled November 8, 2026.** A guarded additive patch of the actual +live Worker, not a rebuild or merge of the unrelated backend branch, deployed +the binding/migration with the flag disabled, validated it, then enabled version +`60e72d1d-f8ca-400d-80c6-26a3c81a0482` at 100 percent (deployment +`c38bece8-bd66-4be1-9a6f-93f8c8ed4787`). All 43 pre-existing bindings and unrelated +modules/settings were preserved. Disabled version +`ebb4fc7f-f578-4204-aea2-c5a186db4414` is the forward-compatible rollback target. +Backend commit `4220a15` records exact preservation hashes, tests, and rollback in +`solosystems-backend/docs/MOBILE_COMMAND_WAKEUP_ROLLOUT_2026_11_08.md`. + +Independent read-only production acceptance passed: health, sessions, and +commands returned 200, anonymous events returned 401, authorized non-upgrade +returned 426, and the authenticated WebSocket handshake returned 101 with a +verified accept header. No commands were created/claimed and no notifications +or model turns were requested. + +The combined portable cloud package built from source `5ca28ecf7` includes both +the wakeup client and the prior emoji change. Its immutable package SHA-256 is +`b468432330a561081b312cadeaf051aa404ffe7dab7af142b7c4292fcb2fe317`. +It is staged, with the previous package retained, and activation waits for idle +turns without `--force`. Duplicate older restart watchers were retired. The +reload client now fails closed if activity status is missing, malformed, or the +probe fails, rather than treating that failure as zero streaming turns. Four +hosted runtime tests, including execution of the shell idle gate, passed. + +The ordinary account's command inventory subsequently returned 200. Eight +existing mixed-version records had queue-wait p50 1,916ms and maximum 2,999,318ms. +This tiny historical sample mixes old clients and may include downtime or backlog. +It is not a post-rollout benchmark or evidence of a wakeup speedup. No Apple USB +device was connected and +usbmuxd was inactive. Real-device install/paint and end-to-end command timing +remain excluded from this rollout. The separate `jcode-mobile` repository's phone transcript transport now pools HTTP connections too (commit `a98b496`). Eight actual list calls in a loopback From 3cccd6998228ce124c016fa671124e76e97b97ad Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:31:33 -0800 Subject: [PATCH 080/236] chore(voice): lock bundled TLS root dependency --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index ab9bec50151..4931f4acfa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4917,6 +4917,7 @@ dependencies = [ "tempfile", "tokio", "tokio-tungstenite", + "webpki-roots", "zbus", ] From 6bf48f959f978403e4a0d1aa344bca040baa7db7 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:44:26 -0800 Subject: [PATCH 081/236] feat(resume): configurable resume time, default to last message Add display.resume_time (message/activity/opened/created, env JCODE_RESUME_TIME). The x ago label and recency sort now share one timestamp. Default 'message' uses the newest stored message of any role, so long runs show when they finished and merely opening a session does not bump it. Also register JCODE_BROWSER_HANDOFF in CONFIG_ENV_KEYS. --- crates/jcode-base/src/config.rs | 8 ++- crates/jcode-base/src/config/default_file.rs | 7 +++ crates/jcode-base/src/config/env_overrides.rs | 5 ++ crates/jcode-config-types/src/display.rs | 56 +++++++++++++++++++ crates/jcode-config-types/src/lib.rs | 6 +- crates/jcode-tui/src/tui/session_picker.rs | 4 +- .../src/tui/session_picker/filter.rs | 32 ++++------- .../src/tui/session_picker/loading.rs | 24 +++++++- .../src/tui/session_picker/loading_tests.rs | 31 ++++++++++ .../session_picker/preview_refresh_tests.rs | 47 ++++++++++++++++ .../src/tui/session_picker/render.rs | 6 +- .../src/tui/session_picker/time_labels.rs | 21 +++++++ 12 files changed, 215 insertions(+), 32 deletions(-) diff --git a/crates/jcode-base/src/config.rs b/crates/jcode-base/src/config.rs index 9b3a3effef9..56946e5ca22 100644 --- a/crates/jcode-base/src/config.rs +++ b/crates/jcode-base/src/config.rs @@ -10,9 +10,9 @@ pub use jcode_config_types::{ GatewayConfig, HookCommands, HooksConfig, KeybindingsConfig, LatexRenderingMode, LaunchHotkeyEntry, LaunchHotkeysConfig, MarkdownSpacingMode, MenubarConfig, NamedProviderAuth, NamedProviderConfig, NamedProviderModelConfig, NamedProviderType, NativeScrollbarConfig, - NotificationsConfig, PowerConfig, ProviderConfig, ReasoningDisplayMode, SafetyConfig, - SessionConfig, SessionPickerResumeAction, SponsorsConfig, SwarmSpawnMode, SwarmStripLayout, - TerminalConfig, UpdateChannel, WebSearchConfig, WebSearchEngine, + NotificationsConfig, PowerConfig, ProviderConfig, ReasoningDisplayMode, ResumeTimeMode, + SafetyConfig, SessionConfig, SessionPickerResumeAction, SponsorsConfig, SwarmSpawnMode, + SwarmStripLayout, TerminalConfig, UpdateChannel, WebSearchConfig, WebSearchEngine, }; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, BTreeSet, HashSet}; @@ -125,6 +125,8 @@ const CONFIG_ENV_KEYS: &[&str] = &[ "HOME", "JCODE_COMMIT_LOG_KEY", "JCODE_RESUME_PROJECT_SCOPE", + "JCODE_RESUME_TIME", + "JCODE_BROWSER_HANDOFF", "JCODE_SWARM_STALL_AFTER_SECS", "JCODE_SWARM_STALL_NUDGE", "JCODE_ACP_PROFILE", diff --git a/crates/jcode-base/src/config/default_file.rs b/crates/jcode-base/src/config/default_file.rs index 7c36e81cc59..69414688a21 100644 --- a/crates/jcode-base/src/config/default_file.rs +++ b/crates/jcode-base/src/config/default_file.rs @@ -266,6 +266,13 @@ prompt_entry_animation = true # disconnected SDK client, since the server cannot observe OS windows directly. # resume_recent_close_seconds = 30 +# Which time Resume shows as "x ago" and sorts by (default: "message"). +# "message": newest message of any role (prompts, replies, tool results) +# "activity": newest of the last message and the last open/resume +# "opened": last time the session was opened, resumed, or attached +# "created": when the session was created +# resume_time = "message" + # Disable specific animation variants by name. # Examples: ["donut"] or ["donut", "orbit_rings"] # Legacy aliases such as "three_rings" and "gyroscope" are still accepted. diff --git a/crates/jcode-base/src/config/env_overrides.rs b/crates/jcode-base/src/config/env_overrides.rs index 6c3f5317457..56c1fd98704 100644 --- a/crates/jcode-base/src/config/env_overrides.rs +++ b/crates/jcode-base/src/config/env_overrides.rs @@ -295,6 +295,11 @@ impl Config { self.display.resume_project_scope = parsed; } } + if let Ok(v) = std::env::var("JCODE_RESUME_TIME") { + if let Some(parsed) = super::ResumeTimeMode::parse(&v) { + self.display.resume_time = parsed; + } + } if let Ok(v) = std::env::var("JCODE_PERFORMANCE") { let trimmed = v.trim().to_lowercase(); if matches!(trimmed.as_str(), "auto" | "full" | "reduced" | "minimal") { diff --git a/crates/jcode-config-types/src/display.rs b/crates/jcode-config-types/src/display.rs index ce52e97ac86..efe91f93f48 100644 --- a/crates/jcode-config-types/src/display.rs +++ b/crates/jcode-config-types/src/display.rs @@ -131,9 +131,43 @@ pub struct DisplayConfig { /// within this many seconds in Resume (default: 30). Zero disables it. /// This uses socket-disconnect events, never transcript/message activity. pub resume_recent_close_seconds: u64, + /// Which timestamp Resume shows as "x ago" and sorts by + /// (message/activity/opened/created, default: message). + #[serde(deserialize_with = "crate::serde_lenient::lenient_enum")] + pub resume_time: ResumeTimeMode, /// Usage percentage wording: "left" (default) or "used". pub usage_display: String, } + +/// Timestamp used for Resume's "x ago" label and recency ordering. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ResumeTimeMode { + /// Newest conversation message of any role: prompts, replies, tool + /// results (default). Opening a session without working does not count. + #[default] + #[serde(alias = "messages", alias = "last_message")] + Message, + /// Newest of the last message and the last open/resume. + Activity, + /// Last time the session was opened, resumed, or attached. + #[serde(alias = "active", alias = "resumed")] + Opened, + /// When the session was created. + Created, +} + +impl ResumeTimeMode { + pub fn parse(value: &str) -> Option { + match value.trim().to_ascii_lowercase().as_str() { + "message" | "messages" | "last_message" => Some(Self::Message), + "activity" => Some(Self::Activity), + "opened" | "active" | "resumed" => Some(Self::Opened), + "created" => Some(Self::Created), + _ => None, + } + } +} impl Default for DisplayConfig { fn default() -> Self { Self { @@ -174,6 +208,7 @@ impl Default for DisplayConfig { external_sessions: true, resume_project_scope: false, resume_recent_close_seconds: 30, + resume_time: ResumeTimeMode::default(), usage_display: "left".to_string(), } } @@ -247,6 +282,27 @@ mod tests { } } + #[test] + fn resume_time_defaults_to_message_and_parses_modes() { + use super::ResumeTimeMode; + let missing: DisplayConfig = serde_json::from_str("{}").unwrap(); + assert_eq!(missing.resume_time, ResumeTimeMode::Message); + for (raw, mode) in [ + ("message", ResumeTimeMode::Message), + ("activity", ResumeTimeMode::Activity), + ("opened", ResumeTimeMode::Opened), + ("created", ResumeTimeMode::Created), + ] { + let config: DisplayConfig = + serde_json::from_value(serde_json::json!({ "resume_time": raw })).unwrap(); + assert_eq!(config.resume_time, mode); + assert_eq!(ResumeTimeMode::parse(raw), Some(mode)); + } + let bogus: DisplayConfig = + serde_json::from_value(serde_json::json!({ "resume_time": "bogus" })).unwrap(); + assert_eq!(bogus.resume_time, ResumeTimeMode::Message); + } + #[test] fn recent_close_window_defaults_to_thirty_and_is_configurable() { let missing: DisplayConfig = serde_json::from_str("{}").unwrap(); diff --git a/crates/jcode-config-types/src/lib.rs b/crates/jcode-config-types/src/lib.rs index 723a8f98bbf..5c4d8344c39 100644 --- a/crates/jcode-config-types/src/lib.rs +++ b/crates/jcode-config-types/src/lib.rs @@ -7,7 +7,11 @@ mod menubar; mod power; mod session; pub use session::{HibernationMode, SessionConfig}; -pub use {display::DisplayConfig, menubar::MenubarConfig, power::PowerConfig}; +pub use { + display::{DisplayConfig, ResumeTimeMode}, + menubar::MenubarConfig, + power::PowerConfig, +}; pub mod keybindings; mod serde_lenient; pub use keybindings::{ diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index 3f3420688dd..a7cc583541b 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -164,7 +164,7 @@ fn git_common_dir(dir: &Path) -> Option { /// Format duration since a time in a human-readable way #[path = "session_picker/time_labels.rs"] mod time_labels; -pub(crate) use time_labels::format_time_ago; +pub(crate) use time_labels::{format_time_ago, session_resume_time}; /// Which pane has keyboard focus #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -862,7 +862,7 @@ impl SessionPicker { self.visible_sessions .iter() .filter_map(|session_ref| self.session_by_ref(*session_ref)) - .max_by_key(|session| session.last_active_at.unwrap_or(session.last_message_time)) + .max_by_key(|session| session_resume_time(session)) .map(|session| session.resume_target.clone()) } diff --git a/crates/jcode-tui/src/tui/session_picker/filter.rs b/crates/jcode-tui/src/tui/session_picker/filter.rs index aa1da2ef9a8..0289bef9ee7 100644 --- a/crates/jcode-tui/src/tui/session_picker/filter.rs +++ b/crates/jcode-tui/src/tui/session_picker/filter.rs @@ -241,16 +241,12 @@ impl SessionPicker { }) .collect::>(); - filtered.sort_by(|a, b| { - let a = self - .session_by_ref(*a) - .map(|session| session.last_message_time) - .unwrap_or_default(); - let b = self - .session_by_ref(*b) - .map(|session| session.last_message_time) - .unwrap_or_default(); - b.cmp(&a) + filtered.sort_by_cached_key(|session_ref| { + std::cmp::Reverse( + self.session_by_ref(*session_ref) + .map(super::session_resume_time) + .unwrap_or_default(), + ) }); // Cache once per candidate, never run matching from the comparator. // Stable sorting keeps recency as the deterministic final tie-break. @@ -459,16 +455,12 @@ impl SessionPicker { } } - saved_sessions.sort_by(|a, b| { - let a = self - .session_by_ref(*a) - .map(|session| session.last_message_time) - .unwrap_or_default(); - let b = self - .session_by_ref(*b) - .map(|session| session.last_message_time) - .unwrap_or_default(); - b.cmp(&a) + saved_sessions.sort_by_cached_key(|session_ref| { + std::cmp::Reverse( + self.session_by_ref(*session_ref) + .map(super::session_resume_time) + .unwrap_or_default(), + ) }); if !saved_sessions.is_empty() { diff --git a/crates/jcode-tui/src/tui/session_picker/loading.rs b/crates/jcode-tui/src/tui/session_picker/loading.rs index 2b565bed1e0..f9a504b4dc1 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading.rs @@ -1272,10 +1272,15 @@ struct SessionMessageSummaryData { estimated_tokens: usize, first_user_prompt: Option, search_text: String, + /// Newest stored message timestamp, of any role (prompts, replies, tools). + last_message_at: Option>, } impl SessionMessageSummaryData { fn add_message(&mut self, message: &SessionMessageSummary) { + if let Some(ts) = message.timestamp { + self.last_message_at = Some(self.last_message_at.map_or(ts, |cur| cur.max(ts))); + } if !summary_message_is_visible_conversation(message) { return; } @@ -1305,6 +1310,10 @@ impl SessionMessageSummaryData { } fn merge(&mut self, other: Self) { + self.last_message_at = match (self.last_message_at, other.last_message_at) { + (Some(a), Some(b)) => Some(a.max(b)), + (a, b) => a.or(b), + }; self.visible_message_count = self .visible_message_count .saturating_add(other.visible_message_count); @@ -1392,6 +1401,7 @@ struct SessionMessageSummary { content_text: Option, display_role: Option, token_usage: Option, + timestamp: Option>, } impl<'de> Deserialize<'de> for SessionMessageSummary { @@ -1424,12 +1434,16 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { let mut content: Option<&'de RawValue> = None; let mut display_role: Option = None; let mut token_usage: Option = None; + let mut timestamp: Option> = None; while let Some(key) = map.next_key::>()? { match key.as_ref() { "role" => { role = Some(map.next_value()?); } + "timestamp" => { + timestamp = map.next_value()?; + } "content" => { content = Some(map.next_value()?); } @@ -1466,6 +1480,7 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { content_text, display_role, token_usage, + timestamp, }) } } @@ -1777,7 +1792,12 @@ fn parse_jcode_session_info( user_message_count, assistant_message_count, created_at: session.created_at, - last_message_time: session.updated_at, + // Newest message, falling back to the save time for old snapshots + // whose messages predate per-message timestamps (or empty sessions). + last_message_time: session + .messages + .last_message_at + .unwrap_or(session.updated_at), last_active_at: session.last_active_at, working_dir: session.working_dir, model: session.model, @@ -1969,7 +1989,7 @@ fn load_sessions_inner(bypass_cache: bool) -> Result> { /// ordinary conversations out of the most recent results. Saved and cloud /// sessions compete by recency too, rather than reserving extra visible slots. fn limit_sessions_by_recency(sessions: &mut Vec, limit: usize) { - sessions.sort_by(|a, b| b.last_message_time.cmp(&a.last_message_time)); + sessions.sort_by_cached_key(|s| std::cmp::Reverse(super::session_resume_time(s))); let mut visible = 0; let mut debug = 0; sessions.retain(|session| { diff --git a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs index 50d1536eb4c..920f7c562ec 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading_tests.rs @@ -1596,3 +1596,34 @@ fn benchmark_resume_loading_reports_timings() { #[path = "loading_scope_tests.rs"] mod loading_scope; + +#[test] +fn summary_tracks_newest_message_timestamp_across_roles() { + let data: SessionMessageSummaryData = serde_json::from_str( + r#"[ + {"role":"user","content":"hi","timestamp":"2026-01-01T00:00:00Z"}, + {"role":"assistant","content":"later tool reply","timestamp":"2026-01-01T03:00:00Z"}, + {"role":"user","content":"no ts"} + ]"#, + ) + .unwrap(); + assert_eq!( + data.last_message_at.map(|t| t.to_rfc3339()), + Some("2026-01-01T03:00:00+00:00".to_string()) + ); + + let mut base: SessionMessageSummaryData = serde_json::from_str( + r#"[{"role":"user","content":"a","timestamp":"2026-01-01T00:00:00Z"}]"#, + ) + .unwrap(); + base.merge( + serde_json::from_str( + r#"[{"role":"assistant","content":"b","timestamp":"2026-01-02T00:00:00Z"}]"#, + ) + .unwrap(), + ); + assert_eq!( + base.last_message_at.map(|t| t.to_rfc3339()), + Some("2026-01-02T00:00:00+00:00".to_string()) + ); +} diff --git a/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs b/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs index b5100548696..b1d48c98ed5 100644 --- a/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs @@ -281,3 +281,50 @@ fn genuinely_empty_completed_preview_remains_loaded_and_removed_sessions_release picker.reseed_grouped(vec![], vec![]); assert!(picker.preview_extras.is_empty()); } + +#[test] +fn resume_time_modes_pick_expected_timestamp() { + use super::super::time_labels::session_resume_time_for; + use crate::config::ResumeTimeMode; + use chrono::Duration; + + let now = chrono::Utc::now(); + let mut session = fixture(); + session.created_at = now - Duration::days(3); + session.last_message_time = now - Duration::hours(2); + session.last_active_at = Some(now - Duration::minutes(5)); + + let at = |s: &SessionInfo, mode| session_resume_time_for(s, mode); + // Opening without working must not refresh the default label. + assert_eq!( + at(&session, ResumeTimeMode::Message), + now - Duration::hours(2) + ); + assert_eq!( + at(&session, ResumeTimeMode::Activity), + now - Duration::minutes(5) + ); + assert_eq!( + at(&session, ResumeTimeMode::Opened), + now - Duration::minutes(5) + ); + assert_eq!( + at(&session, ResumeTimeMode::Created), + now - Duration::days(3) + ); + + // Long run after opening: message and activity follow the newest message. + session.last_message_time = now - Duration::minutes(1); + assert_eq!( + at(&session, ResumeTimeMode::Message), + now - Duration::minutes(1) + ); + assert_eq!( + at(&session, ResumeTimeMode::Activity), + now - Duration::minutes(1) + ); + assert_eq!( + at(&session, ResumeTimeMode::Opened), + now - Duration::minutes(5) + ); +} diff --git a/crates/jcode-tui/src/tui/session_picker/render.rs b/crates/jcode-tui/src/tui/session_picker/render.rs index 6e697b580db..603b2407e52 100644 --- a/crates/jcode-tui/src/tui/session_picker/render.rs +++ b/crates/jcode-tui/src/tui/session_picker/render.rs @@ -203,10 +203,8 @@ impl SessionPicker { let dimmer: Color = rgb(70, 70, 70); let batch_restore: Color = rgb(255, 140, 140); - // The list is ordered by last activity, so show that instead of the - // creation time (a day-old session active a minute ago sits on top). - let active_ago = - format_time_ago(session.last_active_at.unwrap_or(session.last_message_time)); + // Same timestamp the list is sorted by (`display.resume_time`). + let active_ago = format_time_ago(session_resume_time(session)); let in_batch_restore = self.crashed_session_ids.contains(&session.id); let is_marked = self.selected_session_ids.contains(&session.id); let same_dir = self.session_in_current_dir(session); diff --git a/crates/jcode-tui/src/tui/session_picker/time_labels.rs b/crates/jcode-tui/src/tui/session_picker/time_labels.rs index 358a29c70a8..6bc4f882d68 100644 --- a/crates/jcode-tui/src/tui/session_picker/time_labels.rs +++ b/crates/jcode-tui/src/tui/session_picker/time_labels.rs @@ -1,5 +1,26 @@ //! Compact session age labels. +use crate::config::ResumeTimeMode; +use jcode_tui_session_picker::SessionInfo; + +/// Timestamp Resume shows and sorts by, per `display.resume_time`. +pub(crate) fn session_resume_time(session: &SessionInfo) -> chrono::DateTime { + session_resume_time_for(session, crate::config::config().display.resume_time) +} + +pub(crate) fn session_resume_time_for( + session: &SessionInfo, + mode: ResumeTimeMode, +) -> chrono::DateTime { + let message = session.last_message_time; + match mode { + ResumeTimeMode::Message => message, + ResumeTimeMode::Activity => session.last_active_at.map_or(message, |a| a.max(message)), + ResumeTimeMode::Opened => session.last_active_at.unwrap_or(message), + ResumeTimeMode::Created => session.created_at, + } +} + pub(crate) fn format_time_ago(time: chrono::DateTime) -> String { let now = chrono::Utc::now(); let duration = now.signed_duration_since(time); From 663aa21dd443fcf9b0d4876a8d50f0dc69d124d5 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:01:01 -0800 Subject: [PATCH 082/236] Revert "Restore compact comprehensive TUI activity status" This reverts commit 017e37da5f7e8a3863d0dd128a353bd1541b1988. --- crates/jcode-tui/src/tui/compact_status.rs | 149 ---- crates/jcode-tui/src/tui/stable_status.rs | 766 +++++++++++------- crates/jcode-tui/src/tui/ui_input.rs | 5 +- .../src/tui/ui_input/status_tests.rs | 166 ++-- crates/jcode-tui/src/tui/ui_tests/mod.rs | 18 +- .../src/tui/ui_tests/stable_status.rs | 230 ++---- 6 files changed, 610 insertions(+), 724 deletions(-) delete mode 100644 crates/jcode-tui/src/tui/compact_status.rs diff --git a/crates/jcode-tui/src/tui/compact_status.rs b/crates/jcode-tui/src/tui/compact_status.rs deleted file mode 100644 index d00de2251f9..00000000000 --- a/crates/jcode-tui/src/tui/compact_status.rs +++ /dev/null @@ -1,149 +0,0 @@ -use super::*; - -pub(super) fn status_single_line(text: &str) -> String { - text.chars() - .map(|c| { - if c.is_control() - || c.is_whitespace() - || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') - { - ' ' - } else { - c - } - }) - .collect::() - .split_whitespace() - .collect::>() - .join(" ") -} - -/// Main composer activity. Only the phase occupies a fixed ten-cell field. -pub(in crate::tui) fn composer_processing_status_line( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, -) -> Line<'static> { - if matches!( - status.phase.as_str(), - "stopped" | "failed" | "crashed" | "completed" | "done" | "cancelled" - ) { - let mut line = live_processing_status_line(status, stale_secs, spinner); - for span in &mut line.spans { - span.content = status_single_line(&span.content).into(); - } - return line; - } - let connection = status.connection_phase.as_deref().unwrap_or("connecting"); - let phase = match status.phase.as_str() { - "running_tool" if status.tool_name.as_deref() == Some("batch") => "batch", - "running_tool" => "tool", - "running" => "thinking", - "waiting_network" => "offline", - "connecting" if connection.starts_with("retry") => "retry", - "sending" => "sending", - "connecting" => "connecting", - "thinking" => "thinking", - "streaming" => "streaming", - "building" => "building", - "retry" => "retry", - _ => "thinking", - }; - let seconds = status.elapsed_ms / 1000; - let elapsed = if seconds >= 3600 { - format!("{}h {}m", seconds / 3600, seconds / 60 % 60) - } else if seconds >= 60 { - format!("{}m {}s", seconds / 60, seconds % 60) - } else { - format!("{seconds}s") - }; - let warning = matches!(phase, "offline" | "retry" | "building"); - let color = if warning { - rgb(255, 193, 7) - } else { - dim_color() - }; - let mut spans = vec![ - Span::styled( - spinner, - Style::default().fg(if warning { color } else { ai_color() }), - ), - Span::styled( - format!(" {phase:<10} {elapsed}"), - Style::default().fg(color), - ), - ]; - let mut detail = |text: String| { - let text = status_single_line(&text); - if !text.is_empty() { - spans.push(Span::styled( - format!(" · {text}"), - Style::default().fg(color), - )); - } - }; - match status.phase.as_str() { - "connecting" => { - let label = match connection { - "authenticating" => "refreshing auth", - "sending_request" => "sending context", - "waiting_for_response" => "waiting for response", - other => other, - }; - if label != phase { - detail(label.into()); - } - } - "running_tool" => { - if let Some(name) = status - .tool_name - .as_ref() - .filter(|name| name.as_str() != "batch") - { - detail(name.clone()); - } - if let Some(intent) = &status.tool_intent { - detail(intent.clone()); - } - } - "streaming" if !status.stream_message_ended => match stale_secs { - Some(s) if s > 10.0 => detail(format!("(stalled {s:.0}s)")), - Some(s) if s > 2.0 => detail(format!("(no tokens {s:.0}s)")), - _ => {} - }, - "waiting_network" => detail("network disconnected, waiting to retry".into()), - _ => {} - } - let tokens = [ - status - .input_tokens - .map(|input| format!("↑{}", format_stream_tokens(input))), - status - .output_tokens - .map(|output| format!("↓{}", format_stream_tokens(output))), - ] - .into_iter() - .flatten() - .collect::>() - .join(" "); - if !tokens.is_empty() { - detail(tokens); - } - if let Some(tps) = status.output_tps_milli { - detail(format!("{:.1} tps", tps as f64 / 1000.0)); - } - for label in collect_transport_context_labels( - status - .status_detail - .as_deref() - .and_then(normalize_status_detail), - status - .connection_type - .as_deref() - .map(display_connection_type), - None, - ) { - detail(label); - } - Line::from(spans) -} diff --git a/crates/jcode-tui/src/tui/stable_status.rs b/crates/jcode-tui/src/tui/stable_status.rs index 0de64365db2..f49c940f9e5 100644 --- a/crates/jcode-tui/src/tui/stable_status.rs +++ b/crates/jcode-tui/src/tui/stable_status.rs @@ -1,353 +1,503 @@ use super::*; -use crate::tui::info_widget::occasional_status_tip; -use crate::tui::ui::activity_indicator; +use unicode_width::UnicodeWidthStr; + +const PHASE_WIDTH: usize = 12; +const ELAPSED_WIDTH: usize = 8; + +fn status_single_line(text: &str) -> String { + text.chars() + .map(|c| { + if c.is_control() || c.is_whitespace() + || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') + { ' ' } else { c } + }) + .collect::() + .split_whitespace() + .collect::>() + .join(" ") +} + +pub(super) fn status_cell(text: &str, width: usize) -> String { + let line = jcode_tui_render::truncate_line_with_ellipsis_to_width( + &Line::from(status_single_line(text)), + width, + ); + let mut result = line + .spans + .iter() + .map(|s| s.content.as_ref()) + .collect::(); + result.push_str(&" ".repeat(width.saturating_sub(UnicodeWidthStr::width(result.as_str())))); + result +} + +pub(super) fn stable_status_elapsed(elapsed_ms: u64) -> String { + let seconds = elapsed_ms / 1000; + let hours = seconds / 3600; + let value = if hours == 0 { + format!("{:02}:{:02}", seconds / 60, seconds % 60) + } else if hours < 100 { + format!("{hours}:{:02}:{:02}", seconds / 60 % 60, seconds % 60) + } else if hours <= 999 { + format!("{hours}h{:02}m", seconds / 60 % 60) + } else if hours <= 999_999 { + format!("{hours}h") + } else { + ">999999h".into() + }; + status_cell(&value, ELAPSED_WIDTH) +} + +/// Values never resize a telemetry slot, including when telemetry is absent. +pub(super) fn stable_status_telemetry(status: &crate::protocol::SwarmLiveStatus) -> [String; 3] { + let token = |value: Option| { + value + .map(format_stream_tokens) + .unwrap_or_else(|| "-".into()) + }; + [ + format!("↑{}", status_cell(&token(status.input_tokens), 6)), + format!("↓{}", status_cell(&token(status.output_tokens), 6)), + status_cell( + &status + .output_tps_milli + .map(|v| format!("{:.1}/s", v as f64 / 1000.0)) + .unwrap_or_else(|| "-/s".into()), + 8, + ), + ] +} + +#[derive(Default)] +struct StatusBadges { + queued: usize, + background: usize, +} + +impl StatusBadges { + fn text(&self, width: usize) -> String { + let count = |n: usize| if n > 99 { "99+".into() } else { n.to_string() }; + let queued = if self.queued > 0 { + if width >= 22 { + format!("[+{} queued]", count(self.queued)) + } else { + format!("q:{}", count(self.queued)) + } + } else { + String::new() + }; + let background = if self.background > 0 { + if width >= 22 { + format!("[bg {}]", count(self.background)) + } else { + format!("bg:{}", count(self.background)) + } + } else { + String::new() + }; + let (queue_width, background_width) = match width { + 22.. => (13, 8), + 12.. => (5, 6), + _ => (3, 4), + }; + format!( + "{} {}", + status_cell(&queued, queue_width), + status_cell(&background, background_width) + ) + } +} + +#[cfg(test)] +pub(super) fn stable_status_line( + glyph: &str, + phase: &str, + elapsed_ms: u64, + activity: &str, + telemetry: &[String], + width: usize, +) -> Line<'static> { + compose_status( + glyph, + phase, + elapsed_ms, + activity, + telemetry, + &StatusBadges::default(), + width, + ) +} + +fn compose_status( + glyph: &str, + phase: &str, + elapsed_ms: u64, + activity: &str, + telemetry: &[String], + badges: &StatusBadges, + width: usize, +) -> Line<'static> { + let clock = if phase == "idle" { + " ".repeat(ELAPSED_WIDTH) + } else { + stable_status_elapsed(elapsed_ms) + }; + let prefix = format!(" {} {} │ ", status_cell(phase, PHASE_WIDTH), clock); + let prefix_width = 1 + UnicodeWidthStr::width(prefix.as_str()); + // These reservations depend only on terminal width, never on label/count length. + let badge_width = match width { + 100.. => 22, + 64.. => 12, + 40.. => 8, + _ => 0, + }; + let mut count = telemetry.len(); + while count > 0 + && prefix_width + + 8 + + badge_width + + 3 + + telemetry[..count] + .iter() + .map(|s| UnicodeWidthStr::width(s.as_str()) + 1) + .sum::() + > width + { + count -= 1; + } + let right = telemetry[..count].join(" "); + let right_width = UnicodeWidthStr::width(right.as_str()) + if count > 0 { 3 } else { 0 }; + let activity_width = width.saturating_sub(prefix_width + badge_width + right_width); + let warning = matches!( + phase, + "building" | "rate limited" | "offline" | "blocked" | "failed" | "crashed" + ); + let warning_color = rgb(255, 193, 7); + let activity_warning = warning + || activity.contains('⚠') + || activity.starts_with("stalled") + || activity.starts_with("no tokens"); + let mut spans = vec![ + Span::styled( + status_cell(glyph, 1), + Style::default().fg(if warning { warning_color } else { ai_color() }), + ), + Span::styled( + prefix, + Style::default().fg(if warning { warning_color } else { dim_color() }), + ), + Span::styled( + status_cell(activity, activity_width), + Style::default().fg(if activity_warning { + warning_color + } else { + ai_color() + }), + ), + ]; + if badge_width > 0 { + spans.push(Span::styled( + badges.text(badge_width), + Style::default().fg(queued_color()), + )); + } + if count > 0 { + spans.push(Span::styled( + format!(" │ {right}"), + Style::default().fg(dim_color()), + )); + } + jcode_tui_render::truncate_line_with_ellipsis_to_width(&Line::from(spans), width) +} + +fn live_row( + status: &crate::protocol::SwarmLiveStatus, + stale_secs: Option, + spinner: &'static str, + badges: &StatusBadges, + width: usize, +) -> Line<'static> { + let phase = match status.phase.as_str() { + "running_tool" if status.tool_name.as_deref() == Some("batch") => "batch", + "running_tool" => "tool", + "running" => "working", + "waiting_network" => "offline", + other => other, + }; + let terminal = matches!( + phase, + "completed" + | "done" + | "failed" + | "crashed" + | "stopped" + | "cancelled" + | "blocked" + | "waiting" + | "idle" + | "ready" + | "queued" + ); + let mut activity = Vec::new(); + // Warnings have a stable home, ahead of potentially unbounded tool intent. + if status.phase == "streaming" + && !status.stream_message_ended + && let Some(stale) = stale_secs.filter(|s| *s > 2.0) + { + activity.push(format!( + "{} {:.0}s", + if stale > 10.0 { "stalled" } else { "no tokens" }, + stale + )); + } + if terminal { + if let Some(reason) = status + .stop_reason + .as_ref() + .or(status.status_detail.as_ref()) + { + activity.push(reason.clone()); + } + } else { + // draw_status places important warnings at the start of this field. + if let Some(intent) = &status.tool_intent + && intent.contains('⚠') + { + activity.push(intent.clone()); + } + if let Some(name) = &status.tool_name { + activity.push(name.clone()); + } + if let Some(intent) = &status.tool_intent + && !intent.contains('⚠') + { + activity.push(intent.clone()); + } + if let Some(connection) = &status.connection_phase { + activity.push(match connection.as_str() { + "authenticating" => "refreshing auth".into(), + "sending_request" => "sending context".into(), + "waiting_for_response" => "waiting for response".into(), + other => other.into(), + }); + } + activity.extend(collect_transport_context_labels( + status + .status_detail + .as_deref() + .and_then(normalize_status_detail), + status + .connection_type + .as_deref() + .map(display_connection_type), + None, + )); + } + activity.retain(|detail| !detail.trim().is_empty()); + let glyph = match phase { + "completed" | "done" => "✓", + "failed" | "crashed" => "✗", + "blocked" => "⊘", + "waiting" | "offline" => "↻", + "idle" | "ready" | "stopped" | "cancelled" => "◼", + _ => spinner, + }; + let telemetry = if terminal { + [" ".repeat(7), " ".repeat(7), " ".repeat(8)] + } else { + stable_status_telemetry(status) + }; + compose_status( + glyph, + phase, + status.elapsed_ms, + &activity.join(" · "), + &telemetry, + badges, + width, + ) +} + +#[cfg(test)] +pub(super) fn live_processing_status_line_at_width( + status: &crate::protocol::SwarmLiveStatus, + stale_secs: Option, + spinner: &'static str, + width: usize, +) -> Line<'static> { + live_row(status, stale_secs, spinner, &StatusBadges::default(), width) +} pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { crate::tui::app::oauth_telemetry::note_status_render(area); + let notice = Line::from(status_single_line( + crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT, + )); frame.render_widget( - Paragraph::new(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) - .style(Style::default().fg(accent_color())), + Paragraph::new(jcode_tui_render::truncate_line_with_ellipsis_to_width( + ¬ice, + area.width as usize, + )) + .style(Style::default().fg(accent_color())), area, ); return; } let elapsed = app.elapsed().map(|d| d.as_secs_f32()).unwrap_or(0.0); - let stale_secs = app.time_since_activity().map(|d| d.as_secs_f32()); - let (cache_read, cache_creation) = app.streaming_cache_tokens(); - let user_turn_count = app.display_user_message_count(); - let (streaming_input_tokens, _) = app.streaming_tokens(); - let provider_name = app.provider_name(); - let upstream_provider = app.upstream_provider(); - let cache_ttl = app.cache_ttl_status(); - let kv_cache_problem = detect_kv_cache_problem( - &provider_name, - upstream_provider.as_deref(), - user_turn_count, - streaming_input_tokens, - cache_read, - cache_creation, - cache_ttl.as_ref(), - ); - - let queued_suffix = if pending_count > 0 { - format!(" · +{} queued", pending_count) - } else { - String::new() - }; - - let line = if let Some(build_progress) = crate::build::read_build_progress() { - let spinner = activity_indicator(elapsed, 12.5); - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "building".into(); - let mut spans = composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - spans.push(Span::styled( - format!(" · {build_progress}"), - Style::default().fg(rgb(255, 193, 7)), - )); - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.elapsed_ms = app + .elapsed() + .map(|d| d.as_millis().min(u64::MAX as u128) as u64) + .unwrap_or(0); + let mut activity = Vec::new(); + let build = crate::build::read_build_progress(); + snapshot.phase = if let Some(build) = build { + activity.push(build.to_string()); + "building".into() } else if let Some(remaining) = app.rate_limit_remaining() { - let secs = remaining.as_secs(); - let spinner = activity_indicator(elapsed, 4.0); - let time_str = if secs >= 3600 { - let hours = secs / 3600; - let mins = (secs % 3600) / 60; - format!("{}h {}m", hours, mins) - } else if secs >= 60 { - let mins = secs / 60; - let s = secs % 60; - format!("{}m {}s", mins, s) - } else { - format!("{}s", secs) - }; - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "retry".into(); - let mut spans = composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - spans.push(Span::styled( - format!(" · Rate limited. Auto-retry in {time_str}..."), - Style::default().fg(rgb(255, 193, 7)), + activity.push(format!( + "auto-retry in {}", + stable_status_elapsed(remaining.as_millis().min(u64::MAX as u128) as u64).trim() )); - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) + "rate limited".into() } else if app.is_processing() { - let spinner = activity_indicator(elapsed, 12.5); - match app.status() { - ProcessingStatus::Idle => Line::from(""), - ProcessingStatus::Sending => { - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "sending".into(); - let mut spans = - composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) - } + ProcessingStatus::Idle => "idle".into(), + ProcessingStatus::Sending => "sending".into(), + ProcessingStatus::Thinking(_) => "thinking".into(), + ProcessingStatus::Streaming => "streaming".into(), ProcessingStatus::Connecting(ref phase) => { - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "connecting".into(); snapshot.connection_phase = Some(connection_phase_label(phase)); - // "Suspiciously long" is measured per connection attempt, not - // across the whole turn, so later round-trips don't immediately - // render yellow just because the turn has been running a while. let phase_elapsed = app .connection_phase_elapsed() .map_or(elapsed, |d| d.as_secs_f32()); - let label_color = match phase { - crate::message::ConnectionPhase::Retrying { .. } => rgb(255, 193, 7), - crate::message::ConnectionPhase::Authenticating if phase_elapsed > 10.0 => { - rgb(255, 193, 7) - } - crate::message::ConnectionPhase::Connecting if phase_elapsed > 10.0 => { - rgb(255, 193, 7) - } - crate::message::ConnectionPhase::SendingRequest if phase_elapsed > 10.0 => { - rgb(255, 193, 7) - } - _ => dim_color(), - }; - let mut spans = - composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - if let Some(label) = spans.get_mut(1) { - label.style = label.style.fg(label_color); - } - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) - } - ProcessingStatus::Thinking(_start) => { - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "thinking".into(); - let mut spans = - composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) - } - ProcessingStatus::Streaming => { - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "streaming".into(); - let mut spans = - composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - if let Some(upstream) = app.upstream_provider() { - spans.push(Span::styled( - format!(" · via {upstream}"), - Style::default().fg(dim_color()), - )); - } - if let Some(problem) = kv_cache_problem { - let miss_tokens = problem.affected_tokens.unwrap_or(0); - let miss_str = if miss_tokens >= 1000 { - format!("{}k", miss_tokens / 1000) - } else if miss_tokens > 0 { - format!("{}", miss_tokens) - } else { - "kv".to_string() - }; - spans.push(Span::styled( - format!(" · ⚠ {miss_str} cache miss"), - Style::default().fg(rgb(255, 193, 7)), - )); + if matches!(phase, crate::message::ConnectionPhase::Retrying { .. }) + || (phase_elapsed > 10.0 + && matches!( + phase, + crate::message::ConnectionPhase::Authenticating + | crate::message::ConnectionPhase::Connecting + | crate::message::ConnectionPhase::SendingRequest + )) + { + activity.push("⚠ connection delayed".into()); } - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) + "connecting".into() } ProcessingStatus::WaitingForNetwork { listener } => { - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "waiting_network".into(); - let mut spans = - composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - spans.push(Span::styled( - format!(" · {listener}"), - Style::default().fg(rgb(255, 193, 7)), + activity.push(format!( + "network disconnected, waiting to retry · {listener}" )); - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) + "waiting_network".into() } ProcessingStatus::RunningTool(ref name) => { - let anim_color = ai_color(); - let batch_prog = app.batch_progress(); - let is_batch = name == "batch"; - // For batch: compute initial total from the streaming tool call input - let batch_total_initial = if is_batch { - app.streaming_tool_calls() + snapshot.tool_name = Some(name.clone()); + if name == "batch" { + let calls = app.streaming_tool_calls(); + let initial = calls .last() .and_then(|tc| tc.input.get("tool_calls")) .and_then(|v| v.as_array()) - .map(|a| a.len()) - } else { - None - }; - let tool_detail = if is_batch { - None // batch always uses progress display - } else { - app.streaming_tool_calls() - .last() - .map(get_tool_activity_detail) - .filter(|s| !s.is_empty()) - }; - let experimental_notice = app.active_experimental_feature_notice(); - let subagent = app.subagent_status(); - - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.phase = "running_tool".into(); - snapshot.tool_name = Some(name.clone()); - snapshot.tool_intent = tool_detail; - let mut spans = - composer_processing_status_line(&snapshot, stale_secs, spinner).spans; - - // For batch tool: show "completed/total · last_tool" progress - if is_batch { + .map(|a| a.len()); + let mut spans = Vec::new(); append_batch_progress_spans( &mut spans, - anim_color, - batch_prog, - batch_total_initial, + ai_color(), + app.batch_progress(), + initial, ); - } - - if let Some(notice) = experimental_notice { - spans.push(Span::styled( - format!(" · ⚠ {}", notice), - Style::default().fg(rgb(255, 193, 7)).bold(), - )); - } - - if let Some(status) = subagent { - spans.push(Span::styled( - format!(" ({})", status), - Style::default().fg(dim_color()), - )); - } - if let Some(label) = app - .upstream_provider() - .map(|provider| format!("via {provider}")) + activity.push( + spans + .iter() + .map(|s| s.content.as_ref()) + .collect::() + .trim_start_matches(" · ") + .to_string(), + ); + } else if let Some(detail) = app + .streaming_tool_calls() + .last() + .map(get_tool_activity_detail) + .filter(|d| !d.is_empty()) { - spans.push(Span::styled( - format!(" · {}", label), - Style::default().fg(dim_color()), - )); - } - - if let Some(problem) = kv_cache_problem { - let miss_tokens = problem.affected_tokens.unwrap_or(0); - let miss_str = if miss_tokens >= 1000 { - format!("{}k", miss_tokens / 1000) - } else if miss_tokens > 0 { - format!("{}", miss_tokens) - } else { - "kv".to_string() - }; - spans.push(Span::styled( - format!(" · ⚠ {} cache miss", miss_str), - Style::default().fg(rgb(255, 193, 7)), - )); + activity.push(detail); } - - spans.push(Span::styled( - format!(" · {} bg", jcode_tui_core::keybind::alt_chord("B")), - Style::default().fg(rgb(100, 100, 100)), + "running_tool".into() + } + } + } else { + "idle".into() + }; + if app.is_processing() { + let mut warnings = Vec::new(); + if let Some(notice) = app.active_experimental_feature_notice() { + warnings.push(format!("⚠ {notice}")); + } + if matches!(snapshot.phase.as_str(), "streaming" | "running_tool") { + let (read, creation) = app.streaming_cache_tokens(); + if let Some(problem) = detect_kv_cache_problem( + &app.provider_name(), + app.upstream_provider().as_deref(), + app.display_user_message_count(), + app.streaming_tokens().0, + read, + creation, + app.cache_ttl_status().as_ref(), + ) { + warnings.push(format!( + "⚠ {} cache miss", + problem + .affected_tokens + .map(format_stream_tokens) + .unwrap_or_else(|| "kv".into()) )); - - push_queued_suffix(&mut spans, &queued_suffix); - Line::from(spans) } } - } else if let Some((total_in, total_out)) = app.total_session_tokens() { - let total = total_in + total_out; - if let Some(warning) = occasional_session_history_warning( - total, - app.session_compaction_count(), - app.context_limit(), - area.width as usize, - app.animation_elapsed() as u64, - ) { - let severe_token_threshold = app - .context_limit() - .and_then(|limit| u64::try_from(limit).ok()) - .map(|limit| limit.saturating_mul(3)) - .unwrap_or(1_000_000); - let warning_color = - if total >= severe_token_threshold || app.session_compaction_count() >= 3 { - rgb(255, 100, 100) - } else { - rgb(255, 193, 7) - }; - Line::from(vec![ - Span::styled("⚠ ", Style::default().fg(warning_color)), - Span::styled(warning, Style::default().fg(warning_color)), - ]) - } else if let Some(tip) = - occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) - { - Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) - } else { - Line::from("") + warnings.append(&mut activity); + activity = warnings; + if let Some(subagent) = app.subagent_status() { + activity.push(subagent); } - } else { - if let Some(tip) = - occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) + if let Some(upstream) = app.upstream_provider() { + activity.push(format!("via {upstream}")); + } + } else if snapshot.phase == "idle" { + // Idle is steady except for actionable session-history warnings. No rotating tips. + if let Some((input, output)) = app.total_session_tokens() + && let Some(warning) = occasional_session_history_warning( + input.saturating_add(output), + app.session_compaction_count(), + app.context_limit(), + area.width as usize, + app.animation_elapsed() as u64, + ) { - Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) - } else { - Line::from("") + activity.push(format!("⚠ {warning}")); } + snapshot.status_detail = activity.first().cloned(); + snapshot.connection_type = None; + } + if snapshot.phase != "streaming" { + snapshot.output_tps_milli = None; + } + snapshot.tool_intent = Some(activity.join(" · ")); + let badges = StatusBadges { + queued: pending_count, + background: app + .background_task_rows() + .iter() + .filter(|t| t.status == crate::tui::BackgroundTaskRowStatus::Running) + .count(), }; - - let line = status_with_background_tasks( - line, - app.background_task_rows(), + let line = live_row( + &snapshot, + app.time_since_activity().map(|d| d.as_secs_f32()), + super::super::activity_indicator(elapsed, 12.5), + &badges, area.width as usize, - app.is_processing(), ); - crate::memory::check_staleness(); - - frame.render_widget(Paragraph::new(line), area); -} - -/// Background launches finish their tool call immediately, but the work and -/// its intent must remain visible while the agent moves on to another tool. -fn status_with_background_tasks( - mut foreground: Line<'static>, - tasks: &[crate::tui::BackgroundTaskRow], - width: usize, - processing: bool, -) -> Line<'static> { - use crate::tui::BackgroundTaskRowStatus; - use jcode_tui_render::truncate_line_with_ellipsis_to_width as fit; - - for span in &mut foreground.spans { - // Preserve intentional prefix spacing while stripping terminal controls. - span.content = span.content.chars().map(|c| { - if c.is_control() || c.is_whitespace() - || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') - { ' ' } else { c } - }).collect::().into(); - } - let running: Vec<_> = tasks - .iter() - .filter(|task| task.status == BackgroundTaskRowStatus::Running) - .collect(); - if running.is_empty() { - return fit(&foreground, width); - } - let labels = running - .iter() - .map(|task| compact_status::status_single_line(&task.label)) - .collect::>() - .join(" · "); - let background = Line::from(Span::styled( - format!("bg {}: {}", running.len(), labels), - Style::default().fg(ai_color()), - )); - // Idle tips must not displace actual work. While processing, reserve space - // for both activities rather than letting a long foreground intent hide bg. - if !processing || foreground.width() == 0 || width < 24 { - return fit(&background, width); - } - let background_width = background.width().min(width / 2); - let mut line = fit(&foreground, width.saturating_sub(background_width + 3)); - line.spans - .push(Span::styled(" · ", Style::default().fg(dim_color()))); - line.spans.extend(fit(&background, background_width).spans); - line + // Center the composer container, never the changing status text inside it. + frame.render_widget(Paragraph::new(line).alignment(Alignment::Left), area); } diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index 98d05020175..16b88abdd64 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -671,13 +671,10 @@ fn append_batch_progress_spans( } } -#[path = "compact_status.rs"] -mod compact_status; #[path = "stable_status.rs"] mod stable_status; -pub(super) use compact_status::composer_processing_status_line; -/// Compact live activity for todo-lane observers. The composer uses compact_status. +/// Compact live activity for todo-lane observers. The composer uses stable_status. /// No provider or worker state is inferred here: absent telemetry stays absent. pub(super) fn live_processing_status_line( status: &crate::protocol::SwarmLiveStatus, diff --git a/crates/jcode-tui/src/tui/ui_input/status_tests.rs b/crates/jcode-tui/src/tui/ui_input/status_tests.rs index 4f2a27226a7..2b1eaf2d9f1 100644 --- a/crates/jcode-tui/src/tui/ui_input/status_tests.rs +++ b/crates/jcode-tui/src/tui/ui_input/status_tests.rs @@ -1,89 +1,107 @@ use super::*; use ratatui::style::Modifier; -use super::compact_status::composer_processing_status_line; - -#[test] -fn compact_status_absent_telemetry_does_not_render_placeholders_or_gaps() { - let status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - elapsed_ms: 12_300, - ..Default::default() - }; - let text = composer_processing_status_line(&status, None, "*").to_string(); - assert_eq!(text, "* streaming 12s"); - assert!(!text.contains('│') && !text.contains('↑') && !text.contains('↓')); - assert!(!text.ends_with(' ')); -} - -#[test] -fn compact_status_partial_telemetry_only_shows_known_counts() { - for (input_tokens, output_tokens, expected) in [ - (Some(1200), None, "↑1k"), - (None, Some(456), "↓456"), - (Some(0), Some(0), "↑0 ↓0"), +use super::stable_status::{ + live_processing_status_line_at_width, stable_status_elapsed, stable_status_line, + stable_status_telemetry, status_cell, +}; + +#[test] +fn stable_status_elapsed_keeps_cell_width_across_time_boundaries() { + for elapsed in [ + 0, + 999, + 59_999, + 60_000, + 3_599_999, + 3_600_000, + 360_000_000, + u64::MAX, ] { - let status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - input_tokens, - output_tokens, - ..Default::default() - }; - let text = composer_processing_status_line(&status, None, "*").to_string(); - assert!(text.contains(expected), "{text}"); - assert!(!text.contains('?') && !text.contains('│'), "{text}"); + let value = stable_status_elapsed(elapsed); + assert_eq!( + unicode_width::UnicodeWidthStr::width(value.as_str()), + 8, + "{value}" + ); } + assert_eq!(stable_status_elapsed(12_300).trim(), "00:12"); + assert_eq!(stable_status_elapsed(3_600_000).trim(), "1:00:00"); } #[test] -fn compact_status_prefix_clock_is_consistent_across_phases() { - let mut column = None; - for phase in [ - "sending", - "thinking", - "connecting", - "streaming", - "running_tool", - ] { - let status = crate::protocol::SwarmLiveStatus { - phase: phase.into(), - elapsed_ms: 12_300, - tool_name: Some("bash".into()), - ..Default::default() - }; - let text = composer_processing_status_line(&status, None, "*").to_string(); - let offset = text.find("12s").expect(&text); - assert_eq!(*column.get_or_insert(offset), offset, "{text}"); - assert!(!text.contains('│'), "{text}"); +fn stable_status_cells_fit_unicode_and_normalize_controls() { + for width in 0..40 { + let value = status_cell("界\n tests\tand \x1bmore", width); + assert_eq!(unicode_width::UnicodeWidthStr::width(value.as_str()), width); + assert!(!value.chars().any(char::is_control)); } } #[test] -fn compact_status_detail_normalizes_unicode_and_controls() { - let status = crate::protocol::SwarmLiveStatus { - phase: "running_tool".into(), +fn stable_status_telemetry_stays_anchored_as_values_and_intent_change() { + let mut status = crate::protocol::SwarmLiveStatus { + phase: "streaming".into(), elapsed_ms: 12_300, - tool_name: Some("bash".into()), - tool_intent: Some("Verify 界\n tests\tand \u{001b}more".into()), ..Default::default() }; - let text = composer_processing_status_line(&status, None, "*").to_string(); - assert!(text.contains("Verify 界 tests and more"), "{text}"); - assert!(!text.chars().any(char::is_control), "{text}"); + for width in [60, 80, 100, 120] { + let unknown = live_processing_status_line_at_width(&status, None, "*", width).to_string(); + status.input_tokens = Some(123_456); + status.output_tokens = Some(1234); + status.output_tps_milli = Some(12_500); + status.tool_intent = Some("界 long intent ".repeat(30)); + let known = live_processing_status_line_at_width(&status, None, "*", width).to_string(); + let column = |text: &str, needle: char| { + text.find(needle) + .map(|offset| unicode_width::UnicodeWidthStr::width(&text[..offset])) + }; + assert_eq!( + column(&unknown, '↑'), + column(&known, '↑'), + "{unknown}\n{known}" + ); + assert_eq!( + column(&unknown, '↓'), + column(&known, '↓'), + "{unknown}\n{known}" + ); + assert_eq!(unicode_width::UnicodeWidthStr::width(known.as_str()), width); + status.input_tokens = None; + status.output_tokens = None; + status.output_tps_milli = None; + status.tool_intent = None; + } } #[test] -fn compact_status_empty_intent_does_not_add_a_leading_separator() { +fn stable_status_empty_intent_does_not_add_a_leading_separator() { let status = crate::protocol::SwarmLiveStatus { phase: "streaming".into(), - elapsed_ms: 12_300, tool_intent: Some(String::new()), status_detail: Some("fresh websocket".into()), ..Default::default() }; - let text = composer_processing_status_line(&status, None, "*").to_string(); - assert!(text.ends_with("12s · opening websocket"), "{text}"); - assert!(!text.contains("· ·"), "{text}"); + let text = live_processing_status_line_at_width(&status, None, "*", 120).to_string(); + assert!(text.contains("│ opening websocket"), "{text}"); + assert!(!text.contains("│ ·"), "{text}"); +} + +#[test] +fn stable_status_fits_every_terminal_cell_width() { + let telemetry = stable_status_telemetry(&crate::protocol::SwarmLiveStatus::default()); + for width in 0..160 { + let line = stable_status_line( + "*", + "thinking", + 123_456, + &"界\nlong\tintent".repeat(40), + &telemetry, + width, + ); + assert!(line.width() <= width, "width={width}: {line}"); + assert!(!line.to_string().chars().any(char::is_control)); + } } #[test] @@ -220,7 +238,7 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { tool_intent: Some("Verify 界\noutput".into()), ..Default::default() }; - let line = composer_processing_status_line(&status, Some(0.0), "⠋"); + let line = live_processing_status_line_at_width(&status, Some(0.0), "⠋", 160); let text = line.to_string(); assert!( text.contains("bash") && text.contains("Verify 界 output"), @@ -230,7 +248,7 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { text.contains("↑12k") && text.contains("↓456") && text.contains("existing websocket") - && text.contains("12s"), + && text.contains("00:12"), "{text}" ); assert_eq!(line.spans[0].style.fg, Some(ai_color())); @@ -238,19 +256,19 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { status.phase = "streaming".into(); status.output_tps_milli = Some(12_500); assert!( - composer_processing_status_line(&status, None, "⠋") + live_processing_status_line_at_width(&status, None, "⠋", 160) .to_string() - .contains("12.5 tps") + .contains("12.5/s") ); status.input_tokens = None; status.output_tokens = None; status.connection_type = None; - let text = composer_processing_status_line(&status, None, "⠋").to_string(); + let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); assert!(!text.contains("↑0") && !text.contains("websocket")); - assert!(!text.contains('↑') && !text.contains('↓'), "{text}"); + assert!(text.contains("↑-") && text.contains("↓-"), "{text}"); status.phase = "stopped".into(); status.stop_reason = Some("Rate limit\nretry later".into()); - let text = composer_processing_status_line(&status, None, "⠋").to_string(); + let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); assert!(text.contains("Rate limit retry later") && !text.contains('⠋')); } @@ -706,13 +724,13 @@ fn streaming_liveness_stays_in_activity_and_only_warns_during_streaming() { elapsed_ms: 12_000, ..Default::default() }; - let quiet = composer_processing_status_line(&status, Some(3.4), "*").to_string(); + let quiet = live_processing_status_line_at_width(&status, Some(3.4), "*", 120).to_string(); assert!(quiet.contains("no tokens 3s"), "{quiet}"); - let stalled = composer_processing_status_line(&status, Some(12.1), "*").to_string(); + let stalled = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); assert!(stalled.contains("stalled 12s"), "{stalled}"); - assert_eq!(quiet.find("12s"), stalled.find("12s")); + assert_eq!(quiet.find("00:12"), stalled.find("00:12")); status.stream_message_ended = true; - let ended = composer_processing_status_line(&status, Some(12.1), "*").to_string(); + let ended = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); assert!( ended.starts_with('*') && !ended.contains("stalled"), "{ended}" @@ -720,7 +738,7 @@ fn streaming_liveness_stays_in_activity_and_only_warns_during_streaming() { status.stream_message_ended = false; for phase in ["thinking", "running_tool", "sending", "connecting"] { status.phase = phase.into(); - let text = composer_processing_status_line(&status, Some(12.1), "*").to_string(); + let text = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); assert!( !text.contains("stalled") && !text.contains("no tokens"), "{text}" diff --git a/crates/jcode-tui/src/tui/ui_tests/mod.rs b/crates/jcode-tui/src/tui/ui_tests/mod.rs index 05022d36c48..121396bd4a2 100644 --- a/crates/jcode-tui/src/tui/ui_tests/mod.rs +++ b/crates/jcode-tui/src/tui/ui_tests/mod.rs @@ -138,12 +138,6 @@ struct TestState { status_elapsed: Option, status_rate_limit: Option, status_background: Vec, - status_tool_calls: Vec, - status_tokens: (u64, u64), - status_tps: Option, - status_transport: Option, - status_upstream: Option, - status_subagent: Option, queue_mode: bool, active_skill: Option, centered_mode: bool, @@ -257,13 +251,13 @@ impl crate::tui::TuiState for TestState { .unwrap_or_else(|| "mock-model".to_string()) } fn upstream_provider(&self) -> Option { - self.status_upstream.clone() + None } fn connection_type(&self) -> Option { None } fn status_detail(&self) -> Option { - self.status_transport.clone() + None } fn mcp_servers(&self) -> Vec<(String, usize)> { Vec::new() @@ -272,16 +266,16 @@ impl crate::tui::TuiState for TestState { Vec::new() } fn streaming_tokens(&self) -> (u64, u64) { - self.status_tokens + (0, 0) } fn streaming_cache_tokens(&self) -> (Option, Option) { (None, None) } fn output_tps(&self) -> Option { - self.status_tps + None } fn streaming_tool_calls(&self) -> Vec { - self.status_tool_calls.clone() + Vec::new() } fn elapsed(&self) -> Option { self.status_elapsed @@ -296,7 +290,7 @@ impl crate::tui::TuiState for TestState { self.active_skill.clone() } fn subagent_status(&self) -> Option { - self.status_subagent.clone() + None } fn batch_progress(&self) -> Option { self.batch_progress.clone() diff --git a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs b/crates/jcode-tui/src/tui/ui_tests/stable_status.rs index c05fbaf1f06..0703f0a4cd3 100644 --- a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs +++ b/crates/jcode-tui/src/tui/ui_tests/stable_status.rs @@ -5,7 +5,9 @@ fn render_status(state: &TestState, width: u16, queued: usize) -> Vec { // A second sentinel row catches accidental wrapping or layout spillover. let mut terminal = Terminal::new(TestBackend::new(width.max(1), 2)).unwrap(); terminal - .draw(|frame| input_ui::draw_status(frame, state, Rect::new(0, 0, width, 1), queued)) + .draw(|frame| { + input_ui::draw_status(frame, state, Rect::new(0, 0, width, 1), queued); + }) .unwrap(); let buffer = terminal.backend().buffer(); (0..2) @@ -24,10 +26,6 @@ fn phases() -> Vec { ProcessingStatus::Connecting(crate::message::ConnectionPhase::Connecting), ProcessingStatus::Connecting(crate::message::ConnectionPhase::SendingRequest), ProcessingStatus::Connecting(crate::message::ConnectionPhase::WaitingForResponse), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::Retrying { - attempt: 2, - max: 3, - }), ProcessingStatus::Thinking(std::time::Instant::now()), ProcessingStatus::Streaming, ProcessingStatus::RunningTool("bash".into()), @@ -38,29 +36,10 @@ fn phases() -> Vec { ] } -fn tool(name: &str, intent: &str) -> ToolCall { - serde_json::from_value(serde_json::json!({ - "id": "fixture", "name": name, "intent": intent, "input": { "command": "true" } - })) - .unwrap() -} - -fn background( - label: &str, - status: crate::tui::BackgroundTaskRowStatus, -) -> crate::tui::BackgroundTaskRow { - crate::tui::BackgroundTaskRow { - task_id: label.into(), - label: label.into(), - percent: None, - status, - completed_at: None, - } -} - #[test] -fn compact_status_phase_transitions_keep_clock_column_without_dashboard() { +fn stable_status_phase_transitions_preserve_clock_and_activity_columns() { let mut clock_column = None; + let mut activity_column = None; for status in phases() { let state = TestState { status, @@ -68,17 +47,17 @@ fn compact_status_phase_transitions_keep_clock_column_without_dashboard() { ..Default::default() }; let rows = render_status(&state, 120, 0); - let text = rows[0].trim_end(); - let offset = text.find("12s").expect(text); - let clock = unicode_width::UnicodeWidthStr::width(&text[..offset]); + let text = &rows[0]; + let clock = text.find("00:12").expect(text); + let activity = text.find('│').expect(text); assert_eq!(*clock_column.get_or_insert(clock), clock, "{text}"); - assert!(!text.contains('│') && !text.contains("-/s"), "{text}"); + assert_eq!(*activity_column.get_or_insert(activity), activity, "{text}"); assert!(rows[1].trim().is_empty(), "{rows:?}"); } } #[test] -fn compact_status_centered_mode_does_not_move_changing_text() { +fn stable_status_centered_mode_does_not_move_changing_text() { for status in phases() { let mut state = TestState { status, @@ -92,16 +71,18 @@ fn compact_status_centered_mode_does_not_move_changing_text() { } #[test] -fn compact_status_narrow_unicode_widths_do_not_wrap_or_panic() { +fn stable_status_narrow_widths_do_not_wrap_or_panic() { for status in phases() { let state = TestState { status, status_elapsed: Some(Duration::from_secs(360_000)), - status_tool_calls: vec![tool("bash", &"界\nlong\tintent ".repeat(30))], - status_background: vec![background( - &"界\nlong\tbackground intent".repeat(20), - crate::tui::BackgroundTaskRowStatus::Running, - )], + status_background: vec![crate::tui::BackgroundTaskRow { + task_id: "bg".into(), + label: "界\nlong\tbackground intent".repeat(20), + percent: None, + status: crate::tui::BackgroundTaskRowStatus::Running, + completed_at: None, + }], ..Default::default() }; for width in 0..=120 { @@ -113,37 +94,7 @@ fn compact_status_narrow_unicode_widths_do_not_wrap_or_panic() { } #[test] -fn compact_status_connection_details_and_retry_counts_are_preserved() { - for (phase, expected) in [ - ( - crate::message::ConnectionPhase::Authenticating, - "refreshing auth", - ), - ( - crate::message::ConnectionPhase::SendingRequest, - "sending context", - ), - ( - crate::message::ConnectionPhase::WaitingForResponse, - "waiting for response", - ), - ( - crate::message::ConnectionPhase::Retrying { attempt: 2, max: 3 }, - "2/3", - ), - ] { - let state = TestState { - status: ProcessingStatus::Connecting(phase), - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let rows = render_status(&state, 160, 0); - assert!(rows[0].contains(expected), "{rows:?}"); - } -} - -#[test] -fn compact_status_rate_limit_keeps_clock_and_human_retry_time() { +fn stable_status_rate_limit_uses_the_same_clock_position() { let mut state = TestState { status: ProcessingStatus::Sending, status_elapsed: Some(Duration::from_secs(12)), @@ -151,127 +102,52 @@ fn compact_status_rate_limit_keeps_clock_and_human_retry_time() { }; let active = render_status(&state, 100, 0); state.status_rate_limit = Some(Duration::from_secs(80)); - let limited = render_status(&state, 100, 2); - assert_eq!(active[0].find("12s"), limited[0].find("12s")); - assert!( - limited[0].contains("1m 20s") && limited[0].contains("+2 queued"), - "{limited:?}" - ); + let limited = render_status(&state, 100, 0); + assert_eq!(active[0].find("00:12"), limited[0].find("00:12")); + assert!(limited[0].to_lowercase().contains("retry"), "{limited:?}"); } #[test] -fn compact_status_streaming_keeps_real_tokens_speed_and_transport() { - let state = TestState { - status: ProcessingStatus::Streaming, - status_elapsed: Some(Duration::from_secs(12)), - status_tokens: (12_345, 456), - status_tps: Some(35.2), - status_transport: Some("fresh websocket".into()), - status_upstream: Some("upstream".into()), - ..Default::default() - }; - let rows = render_status(&state, 160, 0); - for expected in [ - "streaming", - "12s", - "↑12k ↓456", - "35.2 tps", - "opening websocket", - "via upstream", - ] { - assert!(rows[0].contains(expected), "missing {expected}: {rows:?}"); +fn stable_status_idle_does_not_rotate_tips() { + let mut state = TestState::default(); + let initial = render_status(&state, 100, 0); + for elapsed in [1.0, 15.0, 60.0, 120.0, 1000.0] { + state.anim_elapsed = elapsed; + assert_eq!(initial, render_status(&state, 100, 0)); } - assert!( - rows[0].trim_end().len() < 140, - "unexpected padding: {rows:?}" - ); } #[test] -fn compact_status_tool_keeps_intent_subagent_shortcut_and_queue() { - let state = TestState { - status: ProcessingStatus::RunningTool("bash".into()), - status_elapsed: Some(Duration::from_secs(12)), - status_tool_calls: vec![tool("bash", "Run focused tests")], - status_tokens: (12_345, 456), - status_subagent: Some("worker fox".into()), - status_upstream: Some("upstream".into()), - ..Default::default() - }; - let rows = render_status(&state, 180, 2); - for expected in [ - "bash", - "Run focused tests", - "↑12k ↓456", - "worker fox", - "via upstream", - " bg", - "+2 queued", - ] { - assert!(rows[0].contains(expected), "missing {expected}: {rows:?}"); - } -} - -#[test] -fn compact_status_batch_keeps_completed_running_and_last_done() { - let state = TestState { - status: ProcessingStatus::RunningTool("batch".into()), - status_elapsed: Some(Duration::from_secs(12)), - batch_progress: Some(crate::bus::BatchProgress { - session_id: "fixture".into(), - tool_call_id: "batch".into(), - total: 5, - completed: 2, - last_completed: Some("read".into()), - running: vec![tool("bash", "Run tests")], - subcalls: Vec::new(), - }), - ..Default::default() - }; - let rows = render_status(&state, 180, 0); - for expected in ["batch", "2/5 done", "running:", "bash", "last done: read"] { - assert!(rows[0].contains(expected), "missing {expected}: {rows:?}"); - } -} - -#[test] -fn compact_status_background_keeps_actual_intents_and_filters_finished_tasks() { +fn stable_status_badges_do_not_move_activity_or_telemetry() { let mut state = TestState { status: ProcessingStatus::RunningTool("bash".into()), status_elapsed: Some(Duration::from_secs(12)), - status_background: vec![ - background("Run tests", crate::tui::BackgroundTaskRowStatus::Running), - background("Build app", crate::tui::BackgroundTaskRowStatus::Running), - background( - "Already done", - crate::tui::BackgroundTaskRowStatus::Completed, - ), - ], ..Default::default() }; - let active = render_status(&state, 160, 0); - for expected in ["bg 2:", "Run tests", "Build app"] { - assert!(active[0].contains(expected), "{active:?}"); - } - assert!(!active[0].contains("Already done")); - state.status = ProcessingStatus::Idle; - let idle = render_status(&state, 160, 0); - assert!( - idle[0].contains("Run tests") && idle[0].contains("Build app"), - "{idle:?}" - ); - assert!( - !idle[0].contains('│') && !idle[0].contains("◼ idle"), - "{idle:?}" - ); -} - -#[test] -fn compact_status_idle_has_no_dashboard_label_or_telemetry() { - let state = TestState::default(); - let rows = render_status(&state, 120, 0); + let before = render_status(&state, 120, 0); + state.status_background = (0..100) + .map(|index| crate::tui::BackgroundTaskRow { + task_id: index.to_string(), + label: "unbounded background intent".repeat(20), + percent: None, + status: crate::tui::BackgroundTaskRowStatus::Running, + completed_at: None, + }) + .collect(); + let after = render_status(&state, 120, 100); + assert_eq!(before[0].find("bash"), after[0].find("bash")); + assert_eq!(before[0].find('↑'), after[0].find('↑')); assert!( - !rows[0].contains('│') && !rows[0].contains("◼ idle") && !rows[0].contains("-/s"), - "{rows:?}" + after[0].contains("[+99+ queued]") && after[0].contains("[bg 99+]"), + "{after:?}" ); + state.status_background[0].status = crate::tui::BackgroundTaskRowStatus::Completed; + let completed = render_status(&state, 120, 1); + assert!(completed[0].contains("[bg 99]"), "{completed:?}"); + assert!(!completed[0].contains("unbounded")); + let without_queue = render_status(&state, 120, 0); + assert_eq!(completed[0].find("[bg"), without_queue[0].find("[bg")); + state.status = ProcessingStatus::Idle; + let idle = render_status(&state, 120, 0); + assert_eq!(without_queue[0].find("[bg"), idle[0].find("[bg")); } From 3adafbd005aea93029963ef97180221b299c13e5 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:01:26 -0800 Subject: [PATCH 083/236] Revert "Make TUI activity status columns stable across phases" This reverts commit 79ab1d1c09333e2fb09fb550ab3bf74aec0a9a87. --- crates/jcode-tui/src/tui/stable_status.rs | 503 ------------------ crates/jcode-tui/src/tui/ui_input.rs | 350 +++++++++++- .../src/tui/ui_input/status_tests.rs | 256 +++++---- crates/jcode-tui/src/tui/ui_tests/mod.rs | 11 +- .../src/tui/ui_tests/stable_status.rs | 153 ------ 5 files changed, 469 insertions(+), 804 deletions(-) delete mode 100644 crates/jcode-tui/src/tui/stable_status.rs delete mode 100644 crates/jcode-tui/src/tui/ui_tests/stable_status.rs diff --git a/crates/jcode-tui/src/tui/stable_status.rs b/crates/jcode-tui/src/tui/stable_status.rs deleted file mode 100644 index f49c940f9e5..00000000000 --- a/crates/jcode-tui/src/tui/stable_status.rs +++ /dev/null @@ -1,503 +0,0 @@ -use super::*; -use unicode_width::UnicodeWidthStr; - -const PHASE_WIDTH: usize = 12; -const ELAPSED_WIDTH: usize = 8; - -fn status_single_line(text: &str) -> String { - text.chars() - .map(|c| { - if c.is_control() || c.is_whitespace() - || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') - { ' ' } else { c } - }) - .collect::() - .split_whitespace() - .collect::>() - .join(" ") -} - -pub(super) fn status_cell(text: &str, width: usize) -> String { - let line = jcode_tui_render::truncate_line_with_ellipsis_to_width( - &Line::from(status_single_line(text)), - width, - ); - let mut result = line - .spans - .iter() - .map(|s| s.content.as_ref()) - .collect::(); - result.push_str(&" ".repeat(width.saturating_sub(UnicodeWidthStr::width(result.as_str())))); - result -} - -pub(super) fn stable_status_elapsed(elapsed_ms: u64) -> String { - let seconds = elapsed_ms / 1000; - let hours = seconds / 3600; - let value = if hours == 0 { - format!("{:02}:{:02}", seconds / 60, seconds % 60) - } else if hours < 100 { - format!("{hours}:{:02}:{:02}", seconds / 60 % 60, seconds % 60) - } else if hours <= 999 { - format!("{hours}h{:02}m", seconds / 60 % 60) - } else if hours <= 999_999 { - format!("{hours}h") - } else { - ">999999h".into() - }; - status_cell(&value, ELAPSED_WIDTH) -} - -/// Values never resize a telemetry slot, including when telemetry is absent. -pub(super) fn stable_status_telemetry(status: &crate::protocol::SwarmLiveStatus) -> [String; 3] { - let token = |value: Option| { - value - .map(format_stream_tokens) - .unwrap_or_else(|| "-".into()) - }; - [ - format!("↑{}", status_cell(&token(status.input_tokens), 6)), - format!("↓{}", status_cell(&token(status.output_tokens), 6)), - status_cell( - &status - .output_tps_milli - .map(|v| format!("{:.1}/s", v as f64 / 1000.0)) - .unwrap_or_else(|| "-/s".into()), - 8, - ), - ] -} - -#[derive(Default)] -struct StatusBadges { - queued: usize, - background: usize, -} - -impl StatusBadges { - fn text(&self, width: usize) -> String { - let count = |n: usize| if n > 99 { "99+".into() } else { n.to_string() }; - let queued = if self.queued > 0 { - if width >= 22 { - format!("[+{} queued]", count(self.queued)) - } else { - format!("q:{}", count(self.queued)) - } - } else { - String::new() - }; - let background = if self.background > 0 { - if width >= 22 { - format!("[bg {}]", count(self.background)) - } else { - format!("bg:{}", count(self.background)) - } - } else { - String::new() - }; - let (queue_width, background_width) = match width { - 22.. => (13, 8), - 12.. => (5, 6), - _ => (3, 4), - }; - format!( - "{} {}", - status_cell(&queued, queue_width), - status_cell(&background, background_width) - ) - } -} - -#[cfg(test)] -pub(super) fn stable_status_line( - glyph: &str, - phase: &str, - elapsed_ms: u64, - activity: &str, - telemetry: &[String], - width: usize, -) -> Line<'static> { - compose_status( - glyph, - phase, - elapsed_ms, - activity, - telemetry, - &StatusBadges::default(), - width, - ) -} - -fn compose_status( - glyph: &str, - phase: &str, - elapsed_ms: u64, - activity: &str, - telemetry: &[String], - badges: &StatusBadges, - width: usize, -) -> Line<'static> { - let clock = if phase == "idle" { - " ".repeat(ELAPSED_WIDTH) - } else { - stable_status_elapsed(elapsed_ms) - }; - let prefix = format!(" {} {} │ ", status_cell(phase, PHASE_WIDTH), clock); - let prefix_width = 1 + UnicodeWidthStr::width(prefix.as_str()); - // These reservations depend only on terminal width, never on label/count length. - let badge_width = match width { - 100.. => 22, - 64.. => 12, - 40.. => 8, - _ => 0, - }; - let mut count = telemetry.len(); - while count > 0 - && prefix_width - + 8 - + badge_width - + 3 - + telemetry[..count] - .iter() - .map(|s| UnicodeWidthStr::width(s.as_str()) + 1) - .sum::() - > width - { - count -= 1; - } - let right = telemetry[..count].join(" "); - let right_width = UnicodeWidthStr::width(right.as_str()) + if count > 0 { 3 } else { 0 }; - let activity_width = width.saturating_sub(prefix_width + badge_width + right_width); - let warning = matches!( - phase, - "building" | "rate limited" | "offline" | "blocked" | "failed" | "crashed" - ); - let warning_color = rgb(255, 193, 7); - let activity_warning = warning - || activity.contains('⚠') - || activity.starts_with("stalled") - || activity.starts_with("no tokens"); - let mut spans = vec![ - Span::styled( - status_cell(glyph, 1), - Style::default().fg(if warning { warning_color } else { ai_color() }), - ), - Span::styled( - prefix, - Style::default().fg(if warning { warning_color } else { dim_color() }), - ), - Span::styled( - status_cell(activity, activity_width), - Style::default().fg(if activity_warning { - warning_color - } else { - ai_color() - }), - ), - ]; - if badge_width > 0 { - spans.push(Span::styled( - badges.text(badge_width), - Style::default().fg(queued_color()), - )); - } - if count > 0 { - spans.push(Span::styled( - format!(" │ {right}"), - Style::default().fg(dim_color()), - )); - } - jcode_tui_render::truncate_line_with_ellipsis_to_width(&Line::from(spans), width) -} - -fn live_row( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, - badges: &StatusBadges, - width: usize, -) -> Line<'static> { - let phase = match status.phase.as_str() { - "running_tool" if status.tool_name.as_deref() == Some("batch") => "batch", - "running_tool" => "tool", - "running" => "working", - "waiting_network" => "offline", - other => other, - }; - let terminal = matches!( - phase, - "completed" - | "done" - | "failed" - | "crashed" - | "stopped" - | "cancelled" - | "blocked" - | "waiting" - | "idle" - | "ready" - | "queued" - ); - let mut activity = Vec::new(); - // Warnings have a stable home, ahead of potentially unbounded tool intent. - if status.phase == "streaming" - && !status.stream_message_ended - && let Some(stale) = stale_secs.filter(|s| *s > 2.0) - { - activity.push(format!( - "{} {:.0}s", - if stale > 10.0 { "stalled" } else { "no tokens" }, - stale - )); - } - if terminal { - if let Some(reason) = status - .stop_reason - .as_ref() - .or(status.status_detail.as_ref()) - { - activity.push(reason.clone()); - } - } else { - // draw_status places important warnings at the start of this field. - if let Some(intent) = &status.tool_intent - && intent.contains('⚠') - { - activity.push(intent.clone()); - } - if let Some(name) = &status.tool_name { - activity.push(name.clone()); - } - if let Some(intent) = &status.tool_intent - && !intent.contains('⚠') - { - activity.push(intent.clone()); - } - if let Some(connection) = &status.connection_phase { - activity.push(match connection.as_str() { - "authenticating" => "refreshing auth".into(), - "sending_request" => "sending context".into(), - "waiting_for_response" => "waiting for response".into(), - other => other.into(), - }); - } - activity.extend(collect_transport_context_labels( - status - .status_detail - .as_deref() - .and_then(normalize_status_detail), - status - .connection_type - .as_deref() - .map(display_connection_type), - None, - )); - } - activity.retain(|detail| !detail.trim().is_empty()); - let glyph = match phase { - "completed" | "done" => "✓", - "failed" | "crashed" => "✗", - "blocked" => "⊘", - "waiting" | "offline" => "↻", - "idle" | "ready" | "stopped" | "cancelled" => "◼", - _ => spinner, - }; - let telemetry = if terminal { - [" ".repeat(7), " ".repeat(7), " ".repeat(8)] - } else { - stable_status_telemetry(status) - }; - compose_status( - glyph, - phase, - status.elapsed_ms, - &activity.join(" · "), - &telemetry, - badges, - width, - ) -} - -#[cfg(test)] -pub(super) fn live_processing_status_line_at_width( - status: &crate::protocol::SwarmLiveStatus, - stale_secs: Option, - spinner: &'static str, - width: usize, -) -> Line<'static> { - live_row(status, stale_secs, spinner, &StatusBadges::default(), width) -} - -pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { - if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { - crate::tui::app::oauth_telemetry::note_status_render(area); - let notice = Line::from(status_single_line( - crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT, - )); - frame.render_widget( - Paragraph::new(jcode_tui_render::truncate_line_with_ellipsis_to_width( - ¬ice, - area.width as usize, - )) - .style(Style::default().fg(accent_color())), - area, - ); - return; - } - let elapsed = app.elapsed().map(|d| d.as_secs_f32()).unwrap_or(0.0); - let mut snapshot = live_status_snapshot(app, elapsed); - snapshot.elapsed_ms = app - .elapsed() - .map(|d| d.as_millis().min(u64::MAX as u128) as u64) - .unwrap_or(0); - let mut activity = Vec::new(); - let build = crate::build::read_build_progress(); - snapshot.phase = if let Some(build) = build { - activity.push(build.to_string()); - "building".into() - } else if let Some(remaining) = app.rate_limit_remaining() { - activity.push(format!( - "auto-retry in {}", - stable_status_elapsed(remaining.as_millis().min(u64::MAX as u128) as u64).trim() - )); - "rate limited".into() - } else if app.is_processing() { - match app.status() { - ProcessingStatus::Idle => "idle".into(), - ProcessingStatus::Sending => "sending".into(), - ProcessingStatus::Thinking(_) => "thinking".into(), - ProcessingStatus::Streaming => "streaming".into(), - ProcessingStatus::Connecting(ref phase) => { - snapshot.connection_phase = Some(connection_phase_label(phase)); - let phase_elapsed = app - .connection_phase_elapsed() - .map_or(elapsed, |d| d.as_secs_f32()); - if matches!(phase, crate::message::ConnectionPhase::Retrying { .. }) - || (phase_elapsed > 10.0 - && matches!( - phase, - crate::message::ConnectionPhase::Authenticating - | crate::message::ConnectionPhase::Connecting - | crate::message::ConnectionPhase::SendingRequest - )) - { - activity.push("⚠ connection delayed".into()); - } - "connecting".into() - } - ProcessingStatus::WaitingForNetwork { listener } => { - activity.push(format!( - "network disconnected, waiting to retry · {listener}" - )); - "waiting_network".into() - } - ProcessingStatus::RunningTool(ref name) => { - snapshot.tool_name = Some(name.clone()); - if name == "batch" { - let calls = app.streaming_tool_calls(); - let initial = calls - .last() - .and_then(|tc| tc.input.get("tool_calls")) - .and_then(|v| v.as_array()) - .map(|a| a.len()); - let mut spans = Vec::new(); - append_batch_progress_spans( - &mut spans, - ai_color(), - app.batch_progress(), - initial, - ); - activity.push( - spans - .iter() - .map(|s| s.content.as_ref()) - .collect::() - .trim_start_matches(" · ") - .to_string(), - ); - } else if let Some(detail) = app - .streaming_tool_calls() - .last() - .map(get_tool_activity_detail) - .filter(|d| !d.is_empty()) - { - activity.push(detail); - } - "running_tool".into() - } - } - } else { - "idle".into() - }; - if app.is_processing() { - let mut warnings = Vec::new(); - if let Some(notice) = app.active_experimental_feature_notice() { - warnings.push(format!("⚠ {notice}")); - } - if matches!(snapshot.phase.as_str(), "streaming" | "running_tool") { - let (read, creation) = app.streaming_cache_tokens(); - if let Some(problem) = detect_kv_cache_problem( - &app.provider_name(), - app.upstream_provider().as_deref(), - app.display_user_message_count(), - app.streaming_tokens().0, - read, - creation, - app.cache_ttl_status().as_ref(), - ) { - warnings.push(format!( - "⚠ {} cache miss", - problem - .affected_tokens - .map(format_stream_tokens) - .unwrap_or_else(|| "kv".into()) - )); - } - } - warnings.append(&mut activity); - activity = warnings; - if let Some(subagent) = app.subagent_status() { - activity.push(subagent); - } - if let Some(upstream) = app.upstream_provider() { - activity.push(format!("via {upstream}")); - } - } else if snapshot.phase == "idle" { - // Idle is steady except for actionable session-history warnings. No rotating tips. - if let Some((input, output)) = app.total_session_tokens() - && let Some(warning) = occasional_session_history_warning( - input.saturating_add(output), - app.session_compaction_count(), - app.context_limit(), - area.width as usize, - app.animation_elapsed() as u64, - ) - { - activity.push(format!("⚠ {warning}")); - } - snapshot.status_detail = activity.first().cloned(); - snapshot.connection_type = None; - } - if snapshot.phase != "streaming" { - snapshot.output_tps_milli = None; - } - snapshot.tool_intent = Some(activity.join(" · ")); - let badges = StatusBadges { - queued: pending_count, - background: app - .background_task_rows() - .iter() - .filter(|t| t.status == crate::tui::BackgroundTaskRowStatus::Running) - .count(), - }; - let line = live_row( - &snapshot, - app.time_since_activity().map(|d| d.as_secs_f32()), - super::super::activity_indicator(elapsed, 12.5), - &badges, - area.width as usize, - ); - crate::memory::check_staleness(); - // Center the composer container, never the changing status text inside it. - frame.render_widget(Paragraph::new(line).alignment(Alignment::Left), area); -} diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index 16b88abdd64..d8562eea059 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -14,6 +14,7 @@ use crate::message::ConnectionPhase; use crate::tui::app; use crate::tui::color_support::rgb; use crate::tui::detect_kv_cache_problem; +use crate::tui::info_widget::occasional_status_tip; use crate::tui::layout_utils; use crate::tui::session_facts; use ratatui::{prelude::*, style::Modifier, widgets::Paragraph}; @@ -671,10 +672,7 @@ fn append_batch_progress_spans( } } -#[path = "stable_status.rs"] -mod stable_status; - -/// Compact live activity for todo-lane observers. The composer uses stable_status. +/// Shared by the open session status line and its lightweight todo-lane observer. /// No provider or worker state is inferred here: absent telemetry stays absent. pub(super) fn live_processing_status_line( status: &crate::protocol::SwarmLiveStatus, @@ -822,7 +820,349 @@ fn live_status_snapshot(app: &dyn TuiState, elapsed: f32) -> crate::protocol::Sw } pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { - stable_status::draw_status(frame, app, area, pending_count); + if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { + crate::tui::app::oauth_telemetry::note_status_render(area); + frame.render_widget( + Paragraph::new(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) + .style(Style::default().fg(accent_color())), + area, + ); + return; + } + let elapsed = app.elapsed().map(|d| d.as_secs_f32()).unwrap_or(0.0); + let stale_secs = app.time_since_activity().map(|d| d.as_secs_f32()); + let (cache_read, cache_creation) = app.streaming_cache_tokens(); + let user_turn_count = app.display_user_message_count(); + let (streaming_input_tokens, _) = app.streaming_tokens(); + let provider_name = app.provider_name(); + let upstream_provider = app.upstream_provider(); + let cache_ttl = app.cache_ttl_status(); + let kv_cache_problem = detect_kv_cache_problem( + &provider_name, + upstream_provider.as_deref(), + user_turn_count, + streaming_input_tokens, + cache_read, + cache_creation, + cache_ttl.as_ref(), + ); + + let queued_suffix = if pending_count > 0 { + format!(" · +{} queued", pending_count) + } else { + String::new() + }; + + let line = if let Some(build_progress) = crate::build::read_build_progress() { + let spinner = super::activity_indicator(elapsed, 12.5); + Line::from(vec![ + Span::styled(spinner, Style::default().fg(rgb(255, 193, 7))), + Span::styled( + format!(" {}", build_progress), + Style::default().fg(rgb(255, 193, 7)), + ), + ]) + } else if let Some(remaining) = app.rate_limit_remaining() { + let secs = remaining.as_secs(); + let spinner = super::activity_indicator(elapsed, 4.0); + let time_str = if secs >= 3600 { + let hours = secs / 3600; + let mins = (secs % 3600) / 60; + format!("{}h {}m", hours, mins) + } else if secs >= 60 { + let mins = secs / 60; + let s = secs % 60; + format!("{}m {}s", mins, s) + } else { + format!("{}s", secs) + }; + Line::from(vec![ + Span::styled(spinner, Style::default().fg(rgb(255, 193, 7))), + Span::styled( + format!( + " Rate limited. Auto-retry in {}...{}", + time_str, queued_suffix + ), + Style::default().fg(rgb(255, 193, 7)), + ), + ]) + } else if app.is_processing() { + let spinner = super::activity_indicator(elapsed, 12.5); + + match app.status() { + ProcessingStatus::Idle => Line::from(""), + ProcessingStatus::Sending => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "sending".into(); + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Connecting(ref phase) => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "connecting".into(); + snapshot.connection_phase = Some(connection_phase_label(phase)); + // "Suspiciously long" is measured per connection attempt, not + // across the whole turn, so later round-trips don't immediately + // render yellow just because the turn has been running a while. + let phase_elapsed = app + .connection_phase_elapsed() + .map_or(elapsed, |d| d.as_secs_f32()); + let label_color = match phase { + crate::message::ConnectionPhase::Retrying { .. } => rgb(255, 193, 7), + crate::message::ConnectionPhase::Authenticating if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + crate::message::ConnectionPhase::Connecting if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + crate::message::ConnectionPhase::SendingRequest if phase_elapsed > 10.0 => { + rgb(255, 193, 7) + } + _ => dim_color(), + }; + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + if let Some(label) = spans.get_mut(1) { + label.style = label.style.fg(label_color); + } + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Thinking(_start) => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "thinking".into(); + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::Streaming => { + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "streaming".into(); + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + if let Some(upstream) = app.upstream_provider() { + spans.push(Span::styled( + format!(" · via {upstream}"), + Style::default().fg(dim_color()), + )); + } + if let Some(problem) = kv_cache_problem { + let miss_tokens = problem.affected_tokens.unwrap_or(0); + let miss_str = if miss_tokens >= 1000 { + format!("{}k", miss_tokens / 1000) + } else if miss_tokens > 0 { + format!("{}", miss_tokens) + } else { + "kv".to_string() + }; + spans.insert( + 1, + Span::styled( + format!(" ⚠ {miss_str} cache miss ·"), + Style::default().fg(rgb(255, 193, 7)), + ), + ); + } + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::WaitingForNetwork { listener } => { + let mut spans = vec![ + Span::styled("↻ ", Style::default().fg(rgb(255, 193, 7))), + Span::styled( + format!( + "network disconnected, waiting to retry · {} · {}", + listener, + format_elapsed(elapsed) + ), + Style::default().fg(rgb(255, 193, 7)), + ), + ]; + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + ProcessingStatus::RunningTool(ref name) => { + let anim_color = ai_color(); + let batch_prog = app.batch_progress(); + let is_batch = name == "batch"; + // For batch: compute initial total from the streaming tool call input + let batch_total_initial = if is_batch { + app.streaming_tool_calls() + .last() + .and_then(|tc| tc.input.get("tool_calls")) + .and_then(|v| v.as_array()) + .map(|a| a.len()) + } else { + None + }; + let tool_detail = if is_batch { + None // batch always uses progress display + } else { + app.streaming_tool_calls() + .last() + .map(get_tool_activity_detail) + .filter(|s| !s.is_empty()) + }; + let experimental_notice = app.active_experimental_feature_notice(); + let subagent = app.subagent_status(); + + let mut snapshot = live_status_snapshot(app, elapsed); + snapshot.phase = "running_tool".into(); + snapshot.tool_name = Some(name.clone()); + snapshot.tool_intent = tool_detail; + let mut spans = live_processing_status_line(&snapshot, stale_secs, spinner).spans; + + // For batch tool: show "completed/total · last_tool" progress + if is_batch { + append_batch_progress_spans( + &mut spans, + anim_color, + batch_prog, + batch_total_initial, + ); + } + + if let Some(notice) = experimental_notice { + spans.push(Span::styled( + format!(" · ⚠ {}", notice), + Style::default().fg(rgb(255, 193, 7)).bold(), + )); + } + + if let Some(status) = subagent { + spans.push(Span::styled( + format!(" ({})", status), + Style::default().fg(dim_color()), + )); + } + if let Some(label) = app + .upstream_provider() + .map(|provider| format!("via {provider}")) + { + spans.push(Span::styled( + format!(" · {}", label), + Style::default().fg(dim_color()), + )); + } + + if let Some(problem) = kv_cache_problem { + let miss_tokens = problem.affected_tokens.unwrap_or(0); + let miss_str = if miss_tokens >= 1000 { + format!("{}k", miss_tokens / 1000) + } else if miss_tokens > 0 { + format!("{}", miss_tokens) + } else { + "kv".to_string() + }; + spans.push(Span::styled( + format!(" · ⚠ {} cache miss", miss_str), + Style::default().fg(rgb(255, 193, 7)), + )); + } + + spans.push(Span::styled( + format!(" · {} bg", jcode_tui_core::keybind::alt_chord("B")), + Style::default().fg(rgb(100, 100, 100)), + )); + + push_queued_suffix(&mut spans, &queued_suffix); + Line::from(spans) + } + } + } else if let Some((total_in, total_out)) = app.total_session_tokens() { + let total = total_in + total_out; + if let Some(warning) = occasional_session_history_warning( + total, + app.session_compaction_count(), + app.context_limit(), + area.width as usize, + app.animation_elapsed() as u64, + ) { + let severe_token_threshold = app + .context_limit() + .and_then(|limit| u64::try_from(limit).ok()) + .map(|limit| limit.saturating_mul(3)) + .unwrap_or(1_000_000); + let warning_color = + if total >= severe_token_threshold || app.session_compaction_count() >= 3 { + rgb(255, 100, 100) + } else { + rgb(255, 193, 7) + }; + Line::from(vec![ + Span::styled("⚠ ", Style::default().fg(warning_color)), + Span::styled(warning, Style::default().fg(warning_color)), + ]) + } else if let Some(tip) = + occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) + { + Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) + } else { + Line::from("") + } + } else { + if let Some(tip) = + occasional_status_tip(area.width as usize, app.animation_elapsed() as u64) + { + Line::from(vec![Span::styled(tip, Style::default().fg(dim_color()))]) + } else { + Line::from("") + } + }; + + let line = status_with_background_tasks( + line, + app.background_task_rows(), + area.width as usize, + app.is_processing(), + ); + + crate::memory::check_staleness(); + + if app.centered_mode() { + frame.render_widget(Paragraph::new(line.alignment(Alignment::Center)), area); + return; + } + frame.render_widget(Paragraph::new(line), area); +} + +/// Background launches finish their tool call immediately, but the work and +/// its intent must remain visible while the agent moves on to another tool. +fn status_with_background_tasks( + foreground: Line<'static>, + tasks: &[crate::tui::BackgroundTaskRow], + width: usize, + processing: bool, +) -> Line<'static> { + use crate::tui::BackgroundTaskRowStatus; + use jcode_tui_render::truncate_line_with_ellipsis_to_width as fit; + + let running: Vec<_> = tasks + .iter() + .filter(|task| task.status == BackgroundTaskRowStatus::Running) + .collect(); + if running.is_empty() { + return fit(&foreground, width); + } + let labels = running + .iter() + .map(|task| task.label.split_whitespace().collect::>().join(" ")) + .collect::>() + .join(" · "); + let background = Line::from(Span::styled( + format!("bg {}: {}", running.len(), labels), + Style::default().fg(ai_color()), + )); + // Idle tips must not displace actual work. While processing, reserve space + // for both activities rather than letting a long foreground intent hide bg. + if !processing || foreground.width() == 0 || width < 24 { + return fit(&background, width); + } + let background_width = background.width().min(width / 2); + let mut line = fit(&foreground, width.saturating_sub(background_width + 3)); + line.spans + .push(Span::styled(" · ", Style::default().fg(dim_color()))); + line.spans.extend(fit(&background, background_width).spans); + line } fn running_tool_header_spans( diff --git a/crates/jcode-tui/src/tui/ui_input/status_tests.rs b/crates/jcode-tui/src/tui/ui_input/status_tests.rs index 2b1eaf2d9f1..3a10b076ddf 100644 --- a/crates/jcode-tui/src/tui/ui_input/status_tests.rs +++ b/crates/jcode-tui/src/tui/ui_input/status_tests.rs @@ -1,106 +1,59 @@ use super::*; use ratatui::style::Modifier; -use super::stable_status::{ - live_processing_status_line_at_width, stable_status_elapsed, stable_status_line, - stable_status_telemetry, status_cell, -}; - -#[test] -fn stable_status_elapsed_keeps_cell_width_across_time_boundaries() { - for elapsed in [ - 0, - 999, - 59_999, - 60_000, - 3_599_999, - 3_600_000, - 360_000_000, - u64::MAX, - ] { - let value = stable_status_elapsed(elapsed); - assert_eq!( - unicode_width::UnicodeWidthStr::width(value.as_str()), - 8, - "{value}" - ); +fn background_row( + label: &str, + status: crate::tui::BackgroundTaskRowStatus, +) -> crate::tui::BackgroundTaskRow { + crate::tui::BackgroundTaskRow { + task_id: label.into(), + label: label.into(), + percent: None, + status, + completed_at: None, } - assert_eq!(stable_status_elapsed(12_300).trim(), "00:12"); - assert_eq!(stable_status_elapsed(3_600_000).trim(), "1:00:00"); } #[test] -fn stable_status_cells_fit_unicode_and_normalize_controls() { - for width in 0..40 { - let value = status_cell("界\n tests\tand \x1bmore", width); - assert_eq!(unicode_width::UnicodeWidthStr::width(value.as_str()), width); - assert!(!value.chars().any(char::is_control)); - } -} - -#[test] -fn stable_status_telemetry_stays_anchored_as_values_and_intent_change() { - let mut status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - elapsed_ms: 12_300, - ..Default::default() - }; - for width in [60, 80, 100, 120] { - let unknown = live_processing_status_line_at_width(&status, None, "*", width).to_string(); - status.input_tokens = Some(123_456); - status.output_tokens = Some(1234); - status.output_tps_milli = Some(12_500); - status.tool_intent = Some("界 long intent ".repeat(30)); - let known = live_processing_status_line_at_width(&status, None, "*", width).to_string(); - let column = |text: &str, needle: char| { - text.find(needle) - .map(|offset| unicode_width::UnicodeWidthStr::width(&text[..offset])) - }; - assert_eq!( - column(&unknown, '↑'), - column(&known, '↑'), - "{unknown}\n{known}" - ); - assert_eq!( - column(&unknown, '↓'), - column(&known, '↓'), - "{unknown}\n{known}" - ); - assert_eq!(unicode_width::UnicodeWidthStr::width(known.as_str()), width); - status.input_tokens = None; - status.output_tokens = None; - status.output_tps_milli = None; - status.tool_intent = None; - } -} - -#[test] -fn stable_status_empty_intent_does_not_add_a_leading_separator() { - let status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - tool_intent: Some(String::new()), - status_detail: Some("fresh websocket".into()), - ..Default::default() - }; - let text = live_processing_status_line_at_width(&status, None, "*", 120).to_string(); - assert!(text.contains("│ opening websocket"), "{text}"); - assert!(!text.contains("│ ·"), "{text}"); +fn background_status_keeps_all_running_intents_after_launch_returns() { + use crate::tui::BackgroundTaskRowStatus::*; + let tasks = [ + background_row("Run tests", Running), + background_row("Build app", Running), + background_row("Already done", Completed), + background_row("Old failure", Failed), + ]; + let text = status_with_background_tasks(Line::from("thinking…"), &tasks, 120, true).to_string(); + assert_eq!(text, "thinking… · bg 2: Run tests · Build app"); + let idle = status_with_background_tasks(Line::from("idle tip"), &tasks, 120, false).to_string(); + assert_eq!(idle, "bg 2: Run tests · Build app"); + assert_eq!( + status_with_background_tasks(Line::from("thinking…"), &tasks[2..], 120, true).to_string(), + "thinking…" + ); } #[test] -fn stable_status_fits_every_terminal_cell_width() { - let telemetry = stable_status_telemetry(&crate::protocol::SwarmLiveStatus::default()); - for width in 0..160 { - let line = stable_status_line( - "*", - "thinking", - 123_456, - &"界\nlong\tintent".repeat(40), - &telemetry, - width, - ); - assert!(line.width() <= width, "width={width}: {line}"); - assert!(!line.to_string().chars().any(char::is_control)); +fn background_status_fits_unicode_cells_and_keeps_background_visible() { + let tasks = [background_row( + "Run 界\n tests\tand more", + crate::tui::BackgroundTaskRowStatus::Running, + )]; + for width in 0..120 { + for processing in [false, true] { + let line = status_with_background_tasks( + Line::from("foreground 界 intent ".repeat(10)), + &tasks, + width, + processing, + ); + assert!(line.width() <= width, "width={width}: {line}"); + let text = line.to_string(); + assert!(!text.contains('\n') && !text.contains('\t'), "{text}"); + if width >= 24 { + assert!(text.contains("bg 1:"), "width={width}: {text}"); + } + } } } @@ -238,17 +191,11 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { tool_intent: Some("Verify 界\noutput".into()), ..Default::default() }; - let line = live_processing_status_line_at_width(&status, Some(0.0), "⠋", 160); + let line = live_processing_status_line(&status, Some(0.0), "⠋"); let text = line.to_string(); + assert!(text.contains("running bash · Verify 界 output"), "{text}"); assert!( - text.contains("bash") && text.contains("Verify 界 output"), - "{text}" - ); - assert!( - text.contains("↑12k") - && text.contains("↓456") - && text.contains("existing websocket") - && text.contains("00:12"), + text.contains("↑12k ↓456") && text.contains("existing websocket") && text.contains("12s"), "{text}" ); assert_eq!(line.spans[0].style.fg, Some(ai_color())); @@ -256,19 +203,18 @@ fn live_processing_line_uses_real_tokens_transport_and_tool_intent() { status.phase = "streaming".into(); status.output_tps_milli = Some(12_500); assert!( - live_processing_status_line_at_width(&status, None, "⠋", 160) + live_processing_status_line(&status, None, "⠋") .to_string() - .contains("12.5/s") + .contains("12.5 tps") ); status.input_tokens = None; status.output_tokens = None; status.connection_type = None; - let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); - assert!(!text.contains("↑0") && !text.contains("websocket")); - assert!(text.contains("↑-") && text.contains("↓-"), "{text}"); + let text = live_processing_status_line(&status, None, "⠋").to_string(); + assert!(!text.contains('↑') && !text.contains("websocket")); status.phase = "stopped".into(); status.stop_reason = Some("Rate limit\nretry later".into()); - let text = live_processing_status_line_at_width(&status, None, "⠋", 160).to_string(); + let text = live_processing_status_line(&status, None, "⠋").to_string(); assert!(text.contains("Rate limit retry later") && !text.contains('⠋')); } @@ -482,6 +428,22 @@ fn swarm_effort_model_status_uses_shared_label() { assert_eq!(overscroll_short_reasoning(" "), None); } +#[test] +fn running_tool_header_emphasizes_detail_over_tool_name() { + let accent = Color::Rgb(12, 34, 56); + let spans = running_tool_header_spans("*", "bash", Some("cargo test"), accent); + + assert_eq!(spans.len(), 3); + assert_eq!(spans[0].content.as_ref(), "*"); + assert_eq!(spans[0].style.fg, Some(accent)); + assert_eq!(spans[1].content.as_ref(), " running bash"); + assert_eq!(spans[1].style.fg, Some(dim_color())); + assert!(!spans[1].style.add_modifier.contains(Modifier::BOLD)); + assert_eq!(spans[2].content.as_ref(), " · cargo test"); + assert_eq!(spans[2].style.fg, Some(accent)); + assert!(spans[2].style.add_modifier.contains(Modifier::BOLD)); +} + #[test] fn visual_line_move_follows_soft_wrapped_rows() { // 20 chars, width 10 => two visual rows, no newline in the input. @@ -718,32 +680,58 @@ fn connection_phase_waiting_label_is_generic_response_wait() { } #[test] -fn streaming_liveness_stays_in_activity_and_only_warns_during_streaming() { - let mut status = crate::protocol::SwarmLiveStatus { - phase: "streaming".into(), - elapsed_ms: 12_000, - ..Default::default() - }; - let quiet = live_processing_status_line_at_width(&status, Some(3.4), "*", 120).to_string(); - assert!(quiet.contains("no tokens 3s"), "{quiet}"); - let stalled = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); - assert!(stalled.contains("stalled 12s"), "{stalled}"); - assert_eq!(quiet.find("00:12"), stalled.find("00:12")); - status.stream_message_ended = true; - let ended = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); - assert!( - ended.starts_with('*') && !ended.contains("stalled"), - "{ended}" - ); - status.stream_message_ended = false; - for phase in ["thinking", "running_tool", "sending", "connecting"] { - status.phase = phase.into(); - let text = live_processing_status_line_at_width(&status, Some(12.1), "*", 120).to_string(); - assert!( - !text.contains("stalled") && !text.contains("no tokens"), - "{text}" - ); - } +fn streaming_liveness_label_shows_quiet_stream_warning_before_message_end() { + assert_eq!( + streaming_liveness_label("4.2s".to_string(), Some(3.4), false), + "(no tokens 3s) · 4.2s" + ); + assert_eq!( + streaming_liveness_label("12.0s".to_string(), Some(12.1), false), + "(stalled 12s) · 12.0s" + ); +} + +#[test] +fn streaming_liveness_label_suppresses_quiet_stream_warning_after_message_end() { + assert_eq!( + streaming_liveness_label("4.2s".to_string(), Some(3.4), true), + "4.2s" + ); + assert_eq!( + streaming_liveness_label("12.0s".to_string(), Some(12.1), true), + "12.0s" + ); +} + +#[test] +fn streaming_status_spans_keep_spinner_while_finalizing() { + let spans = streaming_status_spans("⠋", "4.2s".to_string(), false, false, " · +1 queued"); + + assert_eq!(spans.len(), 3); + assert_eq!(spans[0].content.as_ref(), "⠋"); + assert_eq!(spans[1].content.as_ref(), " 4.2s"); + assert_eq!(spans[2].content.as_ref(), " · +1 queued"); +} + +#[test] +fn streaming_status_spans_keep_spinner_after_message_end_while_finalizing() { + let spans = streaming_status_spans("⠋", "finalizing".to_string(), true, false, ""); + + assert_eq!(spans.len(), 2); + assert_eq!(spans[0].content.as_ref(), "⠋"); + assert_eq!(spans[1].content.as_ref(), " finalizing"); +} + +#[test] +fn push_queued_suffix_appends_only_when_present() { + let mut spans: Vec> = Vec::new(); + push_queued_suffix(&mut spans, ""); + assert!(spans.is_empty(), "empty suffix should add no span"); + + push_queued_suffix(&mut spans, " · +2 queued"); + assert_eq!(spans.len(), 1); + assert_eq!(spans[0].content.as_ref(), " · +2 queued"); + assert_eq!(spans[0].style.fg, Some(queued_color())); } #[test] diff --git a/crates/jcode-tui/src/tui/ui_tests/mod.rs b/crates/jcode-tui/src/tui/ui_tests/mod.rs index 121396bd4a2..055d835e0a0 100644 --- a/crates/jcode-tui/src/tui/ui_tests/mod.rs +++ b/crates/jcode-tui/src/tui/ui_tests/mod.rs @@ -3,7 +3,6 @@ use super::*; mod cloud_directories; mod preview_redraw_schedule; mod resume_parity; -mod stable_status; use crate::tui::session_picker; use crate::tui::ui::tools_ui; @@ -135,9 +134,6 @@ struct TestState { pending_soft_interrupts: Vec, interleave_message: Option, status: ProcessingStatus, - status_elapsed: Option, - status_rate_limit: Option, - status_background: Vec, queue_mode: bool, active_skill: Option, centered_mode: bool, @@ -278,7 +274,7 @@ impl crate::tui::TuiState for TestState { Vec::new() } fn elapsed(&self) -> Option { - self.status_elapsed + None } fn status(&self) -> ProcessingStatus { self.status.clone() @@ -371,10 +367,7 @@ impl crate::tui::TuiState for TestState { self.anim_elapsed } fn rate_limit_remaining(&self) -> Option { - self.status_rate_limit - } - fn background_task_rows(&self) -> &[crate::tui::BackgroundTaskRow] { - &self.status_background + None } fn queue_mode(&self) -> bool { self.queue_mode diff --git a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs b/crates/jcode-tui/src/tui/ui_tests/stable_status.rs deleted file mode 100644 index 0703f0a4cd3..00000000000 --- a/crates/jcode-tui/src/tui/ui_tests/stable_status.rs +++ /dev/null @@ -1,153 +0,0 @@ -use super::*; -use ratatui::{Terminal, backend::TestBackend}; - -fn render_status(state: &TestState, width: u16, queued: usize) -> Vec { - // A second sentinel row catches accidental wrapping or layout spillover. - let mut terminal = Terminal::new(TestBackend::new(width.max(1), 2)).unwrap(); - terminal - .draw(|frame| { - input_ui::draw_status(frame, state, Rect::new(0, 0, width, 1), queued); - }) - .unwrap(); - let buffer = terminal.backend().buffer(); - (0..2) - .map(|y| { - (0..width.max(1)) - .map(|x| buffer[(x, y)].symbol()) - .collect::() - }) - .collect() -} - -fn phases() -> Vec { - vec![ - ProcessingStatus::Sending, - ProcessingStatus::Connecting(crate::message::ConnectionPhase::Authenticating), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::Connecting), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::SendingRequest), - ProcessingStatus::Connecting(crate::message::ConnectionPhase::WaitingForResponse), - ProcessingStatus::Thinking(std::time::Instant::now()), - ProcessingStatus::Streaming, - ProcessingStatus::RunningTool("bash".into()), - ProcessingStatus::RunningTool("batch".into()), - ProcessingStatus::WaitingForNetwork { - listener: "界\nlistener".into(), - }, - ] -} - -#[test] -fn stable_status_phase_transitions_preserve_clock_and_activity_columns() { - let mut clock_column = None; - let mut activity_column = None; - for status in phases() { - let state = TestState { - status, - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let rows = render_status(&state, 120, 0); - let text = &rows[0]; - let clock = text.find("00:12").expect(text); - let activity = text.find('│').expect(text); - assert_eq!(*clock_column.get_or_insert(clock), clock, "{text}"); - assert_eq!(*activity_column.get_or_insert(activity), activity, "{text}"); - assert!(rows[1].trim().is_empty(), "{rows:?}"); - } -} - -#[test] -fn stable_status_centered_mode_does_not_move_changing_text() { - for status in phases() { - let mut state = TestState { - status, - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let left = render_status(&state, 100, 0); - state.centered_mode = true; - assert_eq!(left, render_status(&state, 100, 0)); - } -} - -#[test] -fn stable_status_narrow_widths_do_not_wrap_or_panic() { - for status in phases() { - let state = TestState { - status, - status_elapsed: Some(Duration::from_secs(360_000)), - status_background: vec![crate::tui::BackgroundTaskRow { - task_id: "bg".into(), - label: "界\nlong\tbackground intent".repeat(20), - percent: None, - status: crate::tui::BackgroundTaskRowStatus::Running, - completed_at: None, - }], - ..Default::default() - }; - for width in 0..=120 { - let rows = render_status(&state, width, 1234); - assert!(rows[1].trim().is_empty(), "width={width}: {rows:?}"); - assert!(!rows[0].contains('\n') && !rows[0].contains('\t')); - } - } -} - -#[test] -fn stable_status_rate_limit_uses_the_same_clock_position() { - let mut state = TestState { - status: ProcessingStatus::Sending, - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let active = render_status(&state, 100, 0); - state.status_rate_limit = Some(Duration::from_secs(80)); - let limited = render_status(&state, 100, 0); - assert_eq!(active[0].find("00:12"), limited[0].find("00:12")); - assert!(limited[0].to_lowercase().contains("retry"), "{limited:?}"); -} - -#[test] -fn stable_status_idle_does_not_rotate_tips() { - let mut state = TestState::default(); - let initial = render_status(&state, 100, 0); - for elapsed in [1.0, 15.0, 60.0, 120.0, 1000.0] { - state.anim_elapsed = elapsed; - assert_eq!(initial, render_status(&state, 100, 0)); - } -} - -#[test] -fn stable_status_badges_do_not_move_activity_or_telemetry() { - let mut state = TestState { - status: ProcessingStatus::RunningTool("bash".into()), - status_elapsed: Some(Duration::from_secs(12)), - ..Default::default() - }; - let before = render_status(&state, 120, 0); - state.status_background = (0..100) - .map(|index| crate::tui::BackgroundTaskRow { - task_id: index.to_string(), - label: "unbounded background intent".repeat(20), - percent: None, - status: crate::tui::BackgroundTaskRowStatus::Running, - completed_at: None, - }) - .collect(); - let after = render_status(&state, 120, 100); - assert_eq!(before[0].find("bash"), after[0].find("bash")); - assert_eq!(before[0].find('↑'), after[0].find('↑')); - assert!( - after[0].contains("[+99+ queued]") && after[0].contains("[bg 99+]"), - "{after:?}" - ); - state.status_background[0].status = crate::tui::BackgroundTaskRowStatus::Completed; - let completed = render_status(&state, 120, 1); - assert!(completed[0].contains("[bg 99]"), "{completed:?}"); - assert!(!completed[0].contains("unbounded")); - let without_queue = render_status(&state, 120, 0); - assert_eq!(completed[0].find("[bg"), without_queue[0].find("[bg")); - state.status = ProcessingStatus::Idle; - let idle = render_status(&state, 120, 0); - assert_eq!(without_queue[0].find("[bg"), idle[0].find("[bg")); -} From 8053f1ef0ce8de9f92d51bef00b51748c500ecb6 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:02:37 -0800 Subject: [PATCH 084/236] feat(resume): show spinner 'running' state instead of age for live turns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A session mid-turn renders '⠋ running' in place of 'x ago', reusing the existing 8fps picker spinner redraw. Idle and ready sessions still show age. --- .../session_picker/preview_refresh_tests.rs | 28 +++++++++++++++++++ .../src/tui/session_picker/render.rs | 16 +++++++++-- .../tui/session_picker_tests/live_presence.rs | 14 +++++----- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs b/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs index b1d48c98ed5..7bace2eedb1 100644 --- a/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/preview_refresh_tests.rs @@ -328,3 +328,31 @@ fn resume_time_modes_pick_expected_timestamp() { now - Duration::minutes(5) ); } + +#[test] +fn running_session_shows_spinner_state_instead_of_age() { + let session = fixture(); + let mut picker = SessionPicker::new(vec![session.clone()]); + let text = |picker: &SessionPicker| { + picker + .render_session_item_lines_at_frame(&session, false, 0) + .iter() + .flat_map(|line| line.spans.iter().map(|span| span.content.to_string())) + .collect::() + }; + assert!(text(&picker).contains("ago")); + + picker.live_presence.insert( + session.id.clone(), + crate::session::SessionPresence { + session_id: session.id.clone(), + pid: 1, + streaming: true, + streaming_since: None, + internal: false, + }, + ); + let running = text(&picker); + assert!(running.contains("running"), "row: {running}"); + assert!(!running.contains("ago"), "row: {running}"); +} diff --git a/crates/jcode-tui/src/tui/session_picker/render.rs b/crates/jcode-tui/src/tui/session_picker/render.rs index 603b2407e52..5f0987edd32 100644 --- a/crates/jcode-tui/src/tui/session_picker/render.rs +++ b/crates/jcode-tui/src/tui/session_picker/render.rs @@ -197,7 +197,7 @@ impl SessionPicker { &self, session: &SessionInfo, is_selected: bool, - _spinner_frame: usize, + spinner_frame: usize, ) -> Vec> { let dim: Color = rgb(100, 100, 100); let dimmer: Color = rgb(70, 70, 70); @@ -320,8 +320,18 @@ impl SessionPicker { .add_modifier(Modifier::BOLD), )); } - let activity = format!(" {active_ago}"); - detail_spans.push(Span::styled(activity, Style::default().fg(dim))); + // A running turn is a state, not an age: show a spinner instead of + // a stale "x ago" that would count up while the agent is busy. + if self.session_is_streaming(session) { + let frames = jcode_tui_render::swarm_gallery::STRIP_SPINNER_FRAMES; + detail_spans.push(Span::styled( + format!(" {} running", frames[spinner_frame % frames.len()]), + Style::default().fg(rgb(255, 193, 7)), + )); + } else { + let activity = format!(" {active_ago}"); + detail_spans.push(Span::styled(activity, Style::default().fg(dim))); + } let mut rows = vec![line1, Line::from(detail_spans)]; if let Some(reason_line) = Self::crash_reason_line(session) { rows.push(reason_line); diff --git a/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs b/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs index dcc82e9db46..0d9ca2d8add 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/live_presence.rs @@ -43,7 +43,7 @@ fn test_active_filter_shows_only_live_sessions_ready_before_working() { } #[test] -fn test_active_rows_render_age_without_status_badges() { +fn test_running_rows_show_spinner_state_and_ready_rows_show_age() { let live_working = make_session("session_working", "alpha", false, SessionStatus::Active); let live_ready = make_session("session_ready", "beta", false, SessionStatus::Closed); let mut picker = SessionPicker::new(vec![live_working, live_ready]); @@ -70,8 +70,8 @@ fn test_active_rows_render_age_without_status_badges() { .map(line_text) .collect::>() .join("\n"); - assert!(working_text.contains("ago"), "{working_text}"); - assert!(!working_text.contains("working"), "{working_text}"); + assert!(working_text.contains("running"), "{working_text}"); + assert!(!working_text.contains("ago"), "{working_text}"); let first_frame = picker .render_session_item_lines_at_frame(&working, false, 0) @@ -85,8 +85,8 @@ fn test_active_rows_render_age_without_status_badges() { .map(line_text) .collect::>() .join("\n"); - assert!(!first_frame.contains('⠋')); - assert!(!second_frame.contains('⠙')); + assert!(first_frame.contains('⠋'), "{first_frame}"); + assert!(second_frame.contains('⠙'), "{second_frame}"); assert!(picker.has_visible_running_sessions()); let ready_lines = picker.render_session_item_lines(&ready, false); @@ -104,7 +104,7 @@ fn test_active_rows_render_age_without_status_badges() { !ready_text.contains("closed"), "live session must not render as closed: {ready_text}" ); - for row in [working_lines[1].to_string(), ready_lines[1].to_string()] { + for row in [ready_lines[1].to_string()] { for status in ["✓", "●", "▶", "active", "ready", "working"] { assert!(!row.contains(status), "unexpected {status}: {row}"); } @@ -153,7 +153,7 @@ fn test_recent_window_close_boosts_old_transcript_above_newer_messages_and_saved .collect::>() .join("\n"); assert!(!text.contains("just closed"), "{text}"); - assert!(text.contains("ago"), "{text}"); + assert!(text.contains("running"), "{text}"); assert!(!picker.session_recently_closed(picker.visible_session_iter().nth(1).unwrap())); } From cfa379f120cf9e2bb9d174bf4cc19d2511aa964e Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:21:12 -0800 Subject: [PATCH 085/236] Weight /resume transcript matches by source: user prompts over replies over tool output and reasoning --- .../src/tui/session_picker/filter.rs | 10 + .../src/tui/session_picker/relevance_tests.rs | 30 ++ .../tui/session_picker/transcript_search.rs | 423 +++++++++++++----- .../transcript_search/source.rs | 391 ++++++++++++++++ .../transcript_search/text_cache.rs | 147 +++--- .../transcript_search/token_cache.rs | 173 ++++--- .../transcript_search/warmup.rs | 2 +- .../session_picker/transcript_search_tests.rs | 24 +- 8 files changed, 918 insertions(+), 282 deletions(-) create mode 100644 crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs diff --git a/crates/jcode-tui/src/tui/session_picker/filter.rs b/crates/jcode-tui/src/tui/session_picker/filter.rs index 0289bef9ee7..1754e8623bd 100644 --- a/crates/jcode-tui/src/tui/session_picker/filter.rs +++ b/crates/jcode-tui/src/tui/session_picker/filter.rs @@ -278,6 +278,16 @@ impl SessionPicker { .iter() .filter(|token| fields.iter().any(|field| field.contains(token.as_str()))) .count(); + let rank = if self.transcript_search.contains(&session.id) { + rank + } else { + let [title, label, prompt] = &fields; + rank.with_source_weight(transcript_search::metadata_source_weight( + &[title, label, prompt], + &session.search_index, + &tokens, + )) + }; ( identity_search::rank(session, &tokens), rank.with_intent_terms(intent_terms), diff --git a/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs b/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs index 1cb690dd3dc..d0bd7606750 100644 --- a/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/relevance_tests.rs @@ -403,3 +403,33 @@ fn fuzzy_transcript_ranking_uses_matched_words_not_only_recency() { wait(&mut picker); assert_eq!(ids(&picker), ["close", "loose"]); } + +#[test] +fn user_prompt_transcript_hit_beats_recent_tool_output_hit() { + let temp = tempfile::tempdir().unwrap(); + let user_path = temp.path().join("user.jsonl"); + let tool_path = temp.path().join("tool.jsonl"); + std::fs::write( + &user_path, + concat!( + r#"{"type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"please fix zanzibar parsing"}]}}"#, + "\n" + ), + ) + .unwrap(); + std::fs::write( + &tool_path, + concat!( + r#"{"type":"response_item","payload":{"type":"function_call_output","call_id":"c1","output":"grep found zanzibar parsing"}}"#, + "\n" + ), + ) + .unwrap(); + let tool = session("tool", "unrelated", 0, &tool_path); + let user = session("user", "unrelated", 500, &user_path); + let mut picker = SessionPicker::new(vec![tool, user]); + picker.search_query = "zanzibar parsing".into(); + picker.rebuild_items(); + wait(&mut picker); + assert_eq!(ids(&picker), ["user", "tool"]); +} diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs index 7eb225d4e71..fd1e02e646d 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs @@ -3,7 +3,7 @@ //! never fuzzy DP or transcript file reads. use super::loading::search_query_tokens; use super::{ResumeTarget, SessionInfo}; -use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor}; +use serde::de::DeserializeSeed; use std::collections::HashMap; use std::io::{BufRead, BufReader, Read}; use std::path::{Path, PathBuf}; @@ -19,8 +19,10 @@ use std::time::Instant; mod text_cache; mod word_scores; use word_scores::WordScores; +mod source; mod token_cache; mod warmup; +use source::Source; const IMMEDIATE_METADATA_BYTES: usize = 1024 * 1024; @@ -31,6 +33,9 @@ const IMMEDIATE_METADATA_BYTES: usize = 1024 * 1024; pub(super) struct SearchRank { opening_prompt: usize, literal: bool, + /// Sum of per-token origin weights: user prompts beat replies, which beat + /// tool calls, tool output, and reasoning. + source_weight: u32, intent_terms: usize, exact_field: bool, metadata_terms: usize, @@ -68,6 +73,7 @@ pub(super) fn literal_metadata_rank(index: &str, tokens: &[String]) -> SearchRan SearchRank { opening_prompt: 0, literal: metadata_terms == tokens.len(), + source_weight: 0, intent_terms: 0, exact_field: false, metadata_terms, @@ -109,6 +115,11 @@ impl SearchRank { self } + pub(super) fn with_source_weight(mut self, weight: u32) -> Self { + self.source_weight = weight; + self + } + pub(super) fn with_exact_field(mut self, exact: bool) -> Self { self.exact_field = exact; self @@ -143,6 +154,25 @@ struct Candidate { exact_field: bool, opening_prompt: String, intent_text: String, + /// Per-token origin evidence from metadata, before transcript reads. + prior: Vec>, +} + +/// Per-token metadata origins for the UI-thread ranking of sessions the worker +/// has not ranked yet. Uses the already lowercased intent fields. +pub(super) fn metadata_source_weight( + intent_fields: &[&str], + index: &str, + tokens: &[String], +) -> u32 { + source::source_weight(&source::metadata_sources(intent_fields, index, tokens)) +} + +/// Transcript evidence: the first-match fuzzy score plus per-token origins. +#[derive(Clone, Debug, PartialEq, Eq)] +struct Evidence { + score: i64, + sources: Vec>, } /// Rank in memory before publishing results or opening transcripts. Keep this @@ -167,6 +197,11 @@ fn prioritize_candidates( .iter() .filter(|token| intent.contains(token.as_str())) .count(); + let prior = source::metadata_sources(&[&intent], &candidate.search_index, tokens); + drop(intent); + let mut candidate = candidate; + candidate.prior = prior; + rank.source_weight = source::source_weight(&candidate.prior); ranked.push((candidate, rank)); } ranked.sort_by_key(|(_, rank)| std::cmp::Reverse(*rank)); @@ -267,10 +302,27 @@ impl TranscriptSearch { }) .cloned() .collect(); + // Literal metadata matches still go to the worker, which upgrades + // their rank once it knows where in the transcript each word came from, + // unless every word already comes from a user-intent field. if missing_tokens.is_empty() && (tokens.len() < 4 || session.first_user_prompt.is_none()) { - continue; + let intent = [ + session.title.to_lowercase(), + session.save_label.as_deref().unwrap_or("").to_lowercase(), + session + .first_user_prompt + .as_deref() + .unwrap_or("") + .to_lowercase(), + ]; + if tokens + .iter() + .all(|token| intent.iter().any(|field| field.contains(token.as_str()))) + { + continue; + } } let paths = transcript_paths(session); self.candidates.push(Candidate { @@ -290,6 +342,7 @@ impl TranscriptSearch { session.save_label.as_deref().unwrap_or(""), session.first_user_prompt.as_deref().unwrap_or(""), ), + prior: Vec::new(), }); } if !self.candidates.is_empty() { @@ -342,6 +395,9 @@ impl TranscriptSearch { // on the keystroke/render thread. let mut scores = WordScores::default(); let mut fuzzy_candidates = Vec::new(); + // Metadata matches whose words may have come from a user + // prompt rather than a sampled tool dump or reasoning. + let mut classify = Vec::new(); for (mut candidate, priority) in prioritize_candidates(candidates, &query, &tokens, &cancel) { @@ -356,6 +412,7 @@ impl TranscriptSearch { ); rank.opening_prompt = priority.opening_prompt; rank.intent_terms = priority.intent_terms; + rank.source_weight = priority.source_weight; if sender.send((candidate.id.clone(), rank)).is_err() { return; } @@ -364,11 +421,22 @@ impl TranscriptSearch { .missing_tokens .retain(|token| !candidate.search_index.contains(token.as_str())); if candidate.missing_tokens.is_empty() { - let rank = literal_metadata_rank(&candidate.search_index, &tokens) - .with_exact_field(candidate.exact_field); - if sender.send((candidate.id, rank)).is_err() { + let mut rank = literal_metadata_rank(&candidate.search_index, &tokens) + .with_exact_field(candidate.exact_field) + .with_source_weight(priority.source_weight); + rank.opening_prompt = priority.opening_prompt; + rank.intent_terms = priority.intent_terms; + if sender.send((candidate.id.clone(), rank)).is_err() { return; } + if !candidate.paths.is_empty() + && candidate + .prior + .iter() + .any(|source| *source != Some(Source::User)) + { + classify.push((candidate, rank)); + } continue; } fuzzy_candidates.push((candidate, priority)); @@ -387,6 +455,7 @@ impl TranscriptSearch { .with_exact_field(candidate.exact_field); rank.opening_prompt = priority.opening_prompt; rank.intent_terms = priority.intent_terms; + rank.source_weight = priority.source_weight; fuzzy_missing .retain(|token| !scores.matches_text(token, &candidate.search_index)); candidate.search_index = String::new(); @@ -412,17 +481,19 @@ impl TranscriptSearch { } match cached_paths_scan_with_scores( &candidate.paths, - &candidate.missing_tokens, + &tokens, + &candidate.prior, &cancel, false, &mut scores, ) { - Some(Some(_)) => { + Some(Some(evidence)) => { if sender .send(( candidate.id, SearchRank { literal: true, + source_weight: source::source_weight(&evidence.sources), ..rank }, )) @@ -437,19 +508,22 @@ impl TranscriptSearch { } } for (candidate, missing, rank) in warm_fuzzy { + let prior = fuzzy_prior(&tokens, &candidate.prior, &missing); match cached_paths_scan_with_scores( &candidate.paths, - &missing, + &tokens, + &prior, &cancel, true, &mut scores, ) { - Some(Some(score)) => { + Some(Some(evidence)) => { if sender .send(( candidate.id, SearchRank { - score: rank.score + score, + score: rank.score + evidence.score, + source_weight: source::source_weight(&evidence.sources), ..rank }, )) @@ -467,17 +541,20 @@ impl TranscriptSearch { if cancel.load(Ordering::Relaxed) { return; } - if paths_match_mode( + if let Some(evidence) = scan_paths_with_scores( &candidate.paths, - &candidate.missing_tokens, + &tokens, + &candidate.prior, &cancel, - false, + ScanMode::Literal, + &mut scores, ) { if sender .send(( candidate.id, SearchRank { literal: true, + source_weight: source::source_weight(&evidence.sources), ..rank }, )) @@ -489,21 +566,49 @@ impl TranscriptSearch { fuzzy_transcripts.push((candidate, fuzzy_missing, rank)); } } + // Read where metadata matches came from before slower + // typo-tolerant scans, so a session whose words the user + // typed rises above one where they only appear in tool output. + for (candidate, rank) in classify { + if cancel.load(Ordering::Relaxed) { + return; + } + if let Some(evidence) = scan_paths_with_scores( + &candidate.paths, + &tokens, + &candidate.prior, + &cancel, + ScanMode::Classify, + &mut scores, + ) { + let weight = source::source_weight(&evidence.sources); + if weight > rank.source_weight + && sender + .send((candidate.id, rank.with_source_weight(weight))) + .is_err() + { + return; + } + } + } // Exact transcript hits must not wait behind typo-tolerant // scans of unrelated, potentially very large sessions. for (candidate, missing, rank) in fuzzy_transcripts { if cancel.load(Ordering::Relaxed) { return; } - if let Some(score) = scan_paths_with_scores( + let prior = fuzzy_prior(&tokens, &candidate.prior, &missing); + if let Some(evidence) = scan_paths_with_scores( &candidate.paths, - &missing, + &tokens, + &prior, &cancel, - true, + ScanMode::Fuzzy, &mut scores, ) { let rank = SearchRank { - score: rank.score + score, + score: rank.score + evidence.score, + source_weight: source::source_weight(&evidence.sources), ..rank }; if sender.send((candidate.id, rank)).is_err() { @@ -575,51 +680,116 @@ fn transcript_paths(session: &SessionInfo) -> Vec { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ScanMode { + /// Stop once every token is found. + Literal, + /// Typo-tolerant matching. Stop once every token is found. + Fuzzy, + /// Literal matching that keeps reading until every token was seen in a + /// user prompt, so the rank reflects the strongest origin of each word. + Classify, +} + struct Scan<'a> { tokens: &'a [String], found: Vec, + /// Strongest origin each token was seen in so far. + sources: Vec>, cancel: &'a AtomicBool, fuzzy: bool, + classify: bool, score: i64, scores: &'a mut WordScores, } -impl Scan<'_> { +impl<'a> Scan<'a> { + /// `prior` marks tokens already satisfied by metadata and their origin. + fn new( + tokens: &'a [String], + prior: &[Option], + cancel: &'a AtomicBool, + mode: ScanMode, + scores: &'a mut WordScores, + ) -> Self { + let sources: Vec<_> = (0..tokens.len()) + .map(|index| prior.get(index).copied().flatten()) + .collect(); + Self { + tokens, + found: sources.iter().map(Option::is_some).collect(), + sources, + cancel, + fuzzy: mode == ScanMode::Fuzzy, + classify: mode == ScanMode::Classify, + score: 0, + scores, + } + } + fn complete(&self) -> bool { + if self.classify { + self.sources + .iter() + .all(|source| *source == Some(Source::User)) + } else { + self.found.iter().all(|found| *found) + } + } + + fn all_found(&self) -> bool { self.found.iter().all(|found| *found) } - fn text(&mut self, text: &str) -> Result<(), E> { - if self.cancel.load(Ordering::Relaxed) { - return Err(E::custom("search cancelled")); - } - self.normalized_text(&text.to_lowercase()) + fn evidence(&self) -> Option { + self.all_found().then(|| Evidence { + score: self.score, + sources: self.sources.clone(), + }) } - fn normalized_text(&mut self, lower: &str) -> Result<(), E> { + fn normalized_text( + &mut self, + lower: &str, + source: Source, + ) -> Result<(), E> { if self.cancel.load(Ordering::Relaxed) { return Err(E::custom("search cancelled")); } - for (token, found) in self.tokens.iter().zip(&mut self.found) { - if !*found { - if lower.contains(token) { - *found = true; - if self.fuzzy { - self.score += - i64::from(jcode_fuzzy::fuzzy_score(token, token).unwrap_or(0)); - } - } else if self.fuzzy - && token.chars().count() >= 4 - && token.chars().all(char::is_alphabetic) + for ((token, found), best) in self + .tokens + .iter() + .zip(&mut self.found) + .zip(&mut self.sources) + { + if *found { + // Only a stronger origin can change the rank. Skipping weaker + // segments keeps tool dumps cheap once a token is found. + if best.is_none_or(|best| source.weight() > best.weight()) + && lower.contains(token.as_str()) { - // Score the words that actually matched, not unrelated - // metadata. Bound DP work for URLs and minified output. - // Transcript evidence prefers the closest word length - // over an incidental longer prefix or suffix match. - if let Some(score) = self.scores.best_score(token, lower, true) { - *found = true; - self.score += i64::from(score); - } + *best = Some(source); + } + continue; + } + if lower.contains(token.as_str()) { + *found = true; + *best = Some(source); + if self.fuzzy { + self.score += i64::from(jcode_fuzzy::fuzzy_score(token, token).unwrap_or(0)); + } + } else if self.fuzzy + && token.chars().count() >= 4 + && token.chars().all(char::is_alphabetic) + { + // Score the words that actually matched, not unrelated + // metadata. Bound DP work for URLs and minified output. + // Transcript evidence prefers the closest word length + // over an incidental longer prefix or suffix match. + if let Some(score) = self.scores.best_score(token, lower, true) { + *found = true; + *best = Some(source); + self.score += i64::from(score); } } } @@ -632,52 +802,43 @@ impl Scan<'_> { // Decode one JSON string at a time, including escaped Unicode and newlines. // Do not materialize a many-MB session or mistake JSON field names for prose. -impl<'de> DeserializeSeed<'de> for &mut Scan<'_> { - type Value = (); - fn deserialize>(self, deserializer: D) -> Result<(), D::Error> { +impl source::Sink for Scan<'_> { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { if self.cancel.load(Ordering::Relaxed) { - return Err(serde::de::Error::custom("search cancelled")); + return Err("search cancelled"); } - deserializer.deserialize_any(self) + self.normalized_text::(&text.to_lowercase(), source) + .map_err(|_| { + if self.cancel.load(Ordering::Relaxed) { + "search cancelled" + } else { + "search complete" + } + }) } } -impl<'de> Visitor<'de> for &mut Scan<'_> { - type Value = (); - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("transcript JSON") - } - fn visit_str(self, value: &str) -> Result<(), E> { - self.text(value) - } - fn visit_string(self, value: String) -> Result<(), E> { - self.text(&value) - } - fn visit_bool(self, _: bool) -> Result<(), E> { - Ok(()) - } - fn visit_i64(self, _: i64) -> Result<(), E> { - Ok(()) - } - fn visit_u64(self, _: u64) -> Result<(), E> { - Ok(()) - } - fn visit_f64(self, _: f64) -> Result<(), E> { - Ok(()) - } - fn visit_unit(self) -> Result<(), E> { - Ok(()) - } - fn visit_seq>(self, mut seq: A) -> Result<(), A::Error> { - while seq.next_element_seed(&mut *self)?.is_some() {} - Ok(()) - } - fn visit_map>(self, mut map: A) -> Result<(), A::Error> { - while map.next_key::()?.is_some() { - map.next_value_seed(&mut *self)?; - } - Ok(()) - } +/// Tokens outside `missing` were already satisfied by metadata. +fn fuzzy_prior( + tokens: &[String], + prior: &[Option], + missing: &[String], +) -> Vec> { + tokens + .iter() + .enumerate() + .map(|(index, token)| { + if missing.contains(token) { + None + } else { + prior + .get(index) + .copied() + .flatten() + .or(Some(Source::ToolOutput)) + } + }) + .collect() } struct CancellableReader<'a> { @@ -703,6 +864,7 @@ fn paths_match_scored(paths: &[PathBuf], tokens: &[String], cancel: &AtomicBool) scan_paths(paths, tokens, cancel, true) } +#[cfg(test)] fn paths_match_mode( paths: &[PathBuf], tokens: &[String], @@ -712,48 +874,70 @@ fn paths_match_mode( scan_paths(paths, tokens, cancel, fuzzy).is_some() } +#[cfg(test)] +fn mode(fuzzy: bool) -> ScanMode { + if fuzzy { + ScanMode::Fuzzy + } else { + ScanMode::Literal + } +} + +#[cfg(test)] fn scan_paths( paths: &[PathBuf], tokens: &[String], cancel: &AtomicBool, fuzzy: bool, ) -> Option { - scan_paths_with_scores(paths, tokens, cancel, fuzzy, &mut WordScores::default()) + scan_paths_with_scores( + paths, + tokens, + &[], + cancel, + mode(fuzzy), + &mut WordScores::default(), + ) + .map(|evidence| evidence.score) } fn scan_paths_with_scores( paths: &[PathBuf], tokens: &[String], + prior: &[Option], cancel: &AtomicBool, - fuzzy: bool, + mode: ScanMode, scores: &mut WordScores, -) -> Option { - let mut scan = Scan { - tokens, - found: vec![false; tokens.len()], - cancel, - fuzzy, - score: 0, - scores, - }; +) -> Option { + let mut scan = Scan::new(tokens, prior, cancel, mode, scores); for path in paths { if cancel.load(Ordering::Relaxed) { return None; } - scan_file(path, &mut scan); if scan.complete() { - return Some(scan.score); + break; } + scan_file(path, &mut scan); + } + if cancel.load(Ordering::Relaxed) { + return None; } - None + scan.evidence() } -fn apply_token_matches(scan: &mut Scan<'_>, matches: Vec>) { - for (found, score) in scan.found.iter_mut().zip(matches) { - if !*found && let Some(score) = score { +fn apply_token_matches(scan: &mut Scan<'_>, matches: Vec>) { + for ((found, best), hit) in scan + .found + .iter_mut() + .zip(scan.sources.iter_mut()) + .zip(matches) + { + let Some(hit) = hit else { continue }; + if !*found { *found = true; - scan.score += score; + scan.score += hit.score; } + *best = source::stronger(*best, Some(hit.source)); } } @@ -767,24 +951,31 @@ fn cached_paths_scan( cancel: &AtomicBool, fuzzy: bool, ) -> Option> { - cached_paths_scan_with_scores(paths, tokens, cancel, fuzzy, &mut WordScores::default()) + cached_paths_scan_with_scores( + paths, + tokens, + &[], + cancel, + fuzzy, + &mut WordScores::default(), + ) + .map(|evidence| evidence.map(|evidence| evidence.score)) } fn cached_paths_scan_with_scores( paths: &[PathBuf], tokens: &[String], + prior: &[Option], cancel: &AtomicBool, fuzzy: bool, scores: &mut WordScores, -) -> Option> { - let mut scan = Scan { - tokens, - found: vec![false; tokens.len()], - cancel, - fuzzy, - score: 0, - scores, +) -> Option> { + let mode = if fuzzy { + ScanMode::Fuzzy + } else { + ScanMode::Literal }; + let mut scan = Scan::new(tokens, prior, cancel, mode, scores); let mut all_resident = true; for path in paths { if cancel.load(Ordering::Relaxed) { @@ -795,7 +986,7 @@ fn cached_paths_scan_with_scores( { apply_token_matches(&mut scan, matches); if scan.complete() && (!fuzzy || all_resident) { - return Some(Some(scan.score)); + return Some(scan.evidence()); } } else if !std::fs::metadata(path) .is_err_and(|error| error.kind() == std::io::ErrorKind::NotFound) @@ -808,7 +999,7 @@ fn cached_paths_scan_with_scores( if cancel.load(Ordering::Relaxed) || !all_resident { None } else { - Some(scan.complete().then_some(scan.score)) + Some(scan.evidence()) } } @@ -866,8 +1057,8 @@ fn scan_file_streaming(path: &Path, scan: &mut Scan<'_>) { } Ok(_) => { record += 1; - if let Err(error) = - (&mut *scan).deserialize(&mut serde_json::Deserializer::from_slice(&line)) + if let Err(error) = source::Walk::new(&mut *scan) + .deserialize(&mut serde_json::Deserializer::from_slice(&line)) { log_scan_error(path, Some(record), scan, &error); } @@ -882,7 +1073,7 @@ fn scan_file_streaming(path: &Path, scan: &mut Scan<'_>) { let mut deserializer = serde_json::Deserializer::from_reader(reader); // The stream can be one snapshot or multiple JSONL records. loop { - match (&mut *scan).deserialize(&mut deserializer) { + match source::Walk::new(&mut *scan).deserialize(&mut deserializer) { Ok(()) => {} Err(error) => { // EOF is normal between values or in a live, partially written record. diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs new file mode 100644 index 00000000000..85e7bbfd986 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs @@ -0,0 +1,391 @@ +//! Classify transcript strings by origin while streaming JSON. +//! +//! A query word the user typed is much stronger evidence of the conversation +//! they want than the same word inside a tool dump or a reasoning trace. The +//! walker tracks the enclosing message role and content block type so every +//! decoded string carries the source it came from, across jcode, Claude Code, +//! Codex, and similar role/type tagged transcript formats. +use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor}; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub(crate) enum Source { + ToolOutput, + Thinking, + ToolInput, + /// Metadata, system reminders, and untagged strings. + #[default] + Other, + Assistant, + User, +} + +impl Source { + /// Relative strength of a match found in this source. + pub(crate) fn weight(self) -> u32 { + match self { + Source::User => 8, + Source::Assistant => 4, + Source::Other | Source::ToolInput => 2, + Source::Thinking | Source::ToolOutput => 1, + } + } + + fn is_low(self) -> bool { + matches!( + self, + Source::ToolInput | Source::ToolOutput | Source::Thinking + ) + } +} + +/// Weight of the best source per token, summed across query tokens. +pub(crate) fn source_weight(best: &[Option]) -> u32 { + best.iter().flatten().map(|source| source.weight()).sum() +} + +/// Keep the stronger of two per-token observations. +pub(crate) fn stronger(a: Option, b: Option) -> Option { + match (a, b) { + (Some(a), Some(b)) => Some(if b.weight() > a.weight() { b } else { a }), + (a, b) => a.or(b), + } +} + +/// Cheap per-token estimate from in-memory metadata, never transcript IO. +/// Title, saved label, and opening prompt are user intent. Other index hits +/// came from an unclassified transcript sample, so they get the lowest weight +/// and can only be upgraded by a transcript classification pass. +pub(crate) fn metadata_sources( + intent_fields: &[&str], + index: &str, + tokens: &[String], +) -> Vec> { + tokens + .iter() + .map(|token| { + if intent_fields + .iter() + .any(|field| field.contains(token.as_str())) + { + Some(Source::User) + } else if index.contains(token.as_str()) { + Some(Source::ToolOutput) + } else { + None + } + }) + .collect() +} + +pub(crate) trait Sink { + /// Receives each decoded string. An error stops traversal. + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str>; +} + +pub(crate) struct Walk<'a, S> { + pub(crate) sink: &'a mut S, + pub(crate) source: Source, +} + +impl<'a, S: Sink> Walk<'a, S> { + pub(crate) fn new(sink: &'a mut S) -> Self { + Self { + sink, + source: Source::Other, + } + } + + fn child(&mut self, source: Source) -> Walk<'_, S> { + Walk { + sink: &mut *self.sink, + source, + } + } +} + +impl<'de, S: Sink> DeserializeSeed<'de> for Walk<'_, S> { + type Value = (); + fn deserialize>(self, de: D) -> Result<(), D::Error> { + de.deserialize_any(self) + } +} + +impl<'de, S: Sink> Visitor<'de> for Walk<'_, S> { + type Value = (); + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("transcript JSON") + } + fn visit_str(self, value: &str) -> Result<(), E> { + let source = if value.trim_start().starts_with("") { + Source::Other + } else { + self.source + }; + self.sink.text(value, source).map_err(E::custom) + } + fn visit_bool(self, _: bool) -> Result<(), E> { + Ok(()) + } + fn visit_i64(self, _: i64) -> Result<(), E> { + Ok(()) + } + fn visit_u64(self, _: u64) -> Result<(), E> { + Ok(()) + } + fn visit_f64(self, _: f64) -> Result<(), E> { + Ok(()) + } + fn visit_unit(self) -> Result<(), E> { + Ok(()) + } + fn visit_seq>(mut self, mut seq: A) -> Result<(), A::Error> { + let source = self.source; + while seq.next_element_seed(self.child(source))?.is_some() {} + Ok(()) + } + fn visit_map>(mut self, mut map: A) -> Result<(), A::Error> { + let inherited = self.source; + let mut tagged = inherited; + while let Some(key) = map.next_key_seed(KeySeed)? { + let tag = match key { + Key::Role => map.next_value_seed(TagSeed(role_source))?, + Key::Type => map.next_value_seed(TagSeed(type_source))?, + Key::Force(source) => { + map.next_value_seed(self.child(source))?; + continue; + } + Key::Body => { + map.next_value_seed(self.child(tagged))?; + continue; + } + Key::Plain => { + // IDs, paths, and timestamps beside a user prompt are not + // something the user typed. Never raise them above Other. + let source = if tagged.weight() > Source::Other.weight() { + Source::Other + } else { + tagged + }; + map.next_value_seed(self.child(source))?; + continue; + } + }; + // Tags inside tool payloads or reasoning (for example a tool input + // containing `"role": "user"`) must not promote that content. + if let Some(tag) = tag + && !(tagged.is_low() && tag.weight() > tagged.weight()) + { + tagged = tag; + } + } + Ok(()) + } +} + +fn role_source(role: &str) -> Option { + Some(match role.to_ascii_lowercase().as_str() { + "user" | "human" => Source::User, + "assistant" | "model" | "ai" => Source::Assistant, + "tool" | "function" | "toolresult" | "tool_result" => Source::ToolOutput, + "system" | "developer" => Source::Other, + _ => return None, + }) +} + +fn type_source(kind: &str) -> Option { + let kind = kind.to_ascii_lowercase(); + Some(match kind.as_str() { + "thinking" + | "redacted_thinking" + | "reasoning" + | "reasoning_trace" + | "agent_reasoning" + | "agent_reasoning_raw_content" => Source::Thinking, + "tool_use" | "server_tool_use" | "function_call" | "custom_tool_call" | "tool_call" + | "local_shell_call" | "web_search_call" => Source::ToolInput, + "user" | "user_message" => Source::User, + "assistant" | "agent_message" => Source::Assistant, + _ if kind.ends_with("_output") || kind.ends_with("_result") => Source::ToolOutput, + _ => return None, + }) +} + +enum Key { + Role, + Type, + Body, + Plain, + Force(Source), +} + +struct KeySeed; +impl<'de> DeserializeSeed<'de> for KeySeed { + type Value = Key; + fn deserialize>(self, de: D) -> Result { + de.deserialize_str(self) + } +} +impl Visitor<'_> for KeySeed { + type Value = Key; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("object key") + } + fn visit_str(self, key: &str) -> Result { + Ok(match key { + "role" => Key::Role, + "type" => Key::Type, + "content" | "text" | "message" | "parts" | "summary" | "value" | "body" | "prompt" + | "display" => Key::Body, + "thinking" | "signature" | "encrypted_content" | "reasoning" | "reasoning_content" => { + Key::Force(Source::Thinking) + } + "input" | "arguments" | "tool_input" | "params" | "parameters" => { + Key::Force(Source::ToolInput) + } + "toolUseResult" | "tool_use_result" | "output" | "tool_output" | "stdout" + | "stderr" => Key::Force(Source::ToolOutput), + "meta" | "metadata" => Key::Force(Source::Other), + _ => Key::Plain, + }) + } +} + +/// Reads a string tag value. Non-string values are skipped. +struct TagSeed(fn(&str) -> Option); +impl<'de> DeserializeSeed<'de> for TagSeed { + type Value = Option; + fn deserialize>(self, de: D) -> Result { + de.deserialize_any(self) + } +} +impl<'de> Visitor<'de> for TagSeed { + type Value = Option; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("tag") + } + fn visit_str(self, value: &str) -> Result { + Ok((self.0)(value)) + } + fn visit_bool(self, _: bool) -> Result { + Ok(None) + } + fn visit_i64(self, _: i64) -> Result { + Ok(None) + } + fn visit_u64(self, _: u64) -> Result { + Ok(None) + } + fn visit_f64(self, _: f64) -> Result { + Ok(None) + } + fn visit_unit(self) -> Result { + Ok(None) + } + fn visit_seq>(self, mut seq: A) -> Result { + while seq.next_element::()?.is_some() {} + Ok(None) + } + fn visit_map>(self, mut map: A) -> Result { + while map.next_entry::()?.is_some() {} + Ok(None) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Default)] + struct Collect(Vec<(String, Source)>); + impl Sink for Collect { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { + self.0.push((text.to_owned(), source)); + Ok(()) + } + } + + fn classify(json: &str) -> Vec<(String, Source)> { + let mut sink = Collect::default(); + Walk::new(&mut sink) + .deserialize(&mut serde_json::Deserializer::from_str(json)) + .unwrap(); + sink.0 + } + + fn source_of(json: &str, needle: &str) -> Source { + classify(json) + .into_iter() + .find(|(text, _)| text == needle) + .unwrap_or_else(|| panic!("{needle} not found")) + .1 + } + + #[test] + fn jcode_messages_classify_prompts_replies_tools_and_reasoning() { + let json = r#"{"meta":{"title":"TITLE"},"append_messages":[ + {"id":"m1","role":"user","content":[{"type":"text","text":"PROMPT"}]}, + {"id":"m2","role":"user","content":[{"type":"text","text":"REMINDER"}]}, + {"id":"m3","role":"assistant","content":[ + {"type":"reasoning_trace","text":"THOUGHT"}, + {"type":"text","text":"REPLY"}, + {"type":"tool_use","id":"t","name":"bash","input":{"command":"CMD","role":"user"}}]}, + {"id":"m4","role":"user","content":[{"type":"tool_result","tool_use_id":"t","content":"OUT"}]}]}"#; + assert_eq!(source_of(json, "TITLE"), Source::Other); + assert_eq!(source_of(json, "PROMPT"), Source::User); + assert_eq!(source_of(json, "REMINDER"), Source::Other); + assert_eq!(source_of(json, "THOUGHT"), Source::Thinking); + assert_eq!(source_of(json, "REPLY"), Source::Assistant); + assert_eq!(source_of(json, "CMD"), Source::ToolInput); + assert_eq!(source_of(json, "OUT"), Source::ToolOutput); + // Identifiers beside a prompt are not something the user typed. + assert_eq!(source_of(json, "m1"), Source::Other); + } + + #[test] + fn claude_code_and_codex_records_classify() { + let claude = r#"{"cwd":"/repo","type":"user","message":{"role":"user","content":[ + {"type":"tool_result","content":"RESULT"}]},"toolUseResult":{"stdout":"STDOUT"}}"#; + assert_eq!(source_of(claude, "RESULT"), Source::ToolOutput); + assert_eq!(source_of(claude, "STDOUT"), Source::ToolOutput); + assert_eq!(source_of(claude, "/repo"), Source::Other); + let thinking = r#"{"type":"assistant","message":{"role":"assistant","content":[ + {"type":"thinking","thinking":"PONDER"},{"type":"text","text":"ANSWER"}]}}"#; + assert_eq!(source_of(thinking, "PONDER"), Source::Thinking); + assert_eq!(source_of(thinking, "ANSWER"), Source::Assistant); + let codex_prompt = r#"{"type":"response_item","payload":{"type":"message","role":"user", + "content":[{"type":"input_text","text":"ASK"}]}}"#; + assert_eq!(source_of(codex_prompt, "ASK"), Source::User); + let codex_call = r#"{"type":"response_item","payload":{"type":"function_call", + "arguments":"ARGS"}}"#; + assert_eq!(source_of(codex_call, "ARGS"), Source::ToolInput); + let codex_out = r#"{"type":"response_item","payload":{"type":"custom_tool_call_output", + "output":"OUTPUT"}}"#; + assert_eq!(source_of(codex_out, "OUTPUT"), Source::ToolOutput); + let codex_event = + r#"{"type":"event_msg","payload":{"type":"user_message","message":"SAID"}}"#; + assert_eq!(source_of(codex_event, "SAID"), Source::User); + } + + #[test] + fn weights_prefer_user_then_assistant_then_tools_and_thinking() { + assert!(Source::User.weight() > Source::Assistant.weight()); + assert!(Source::Assistant.weight() > Source::ToolInput.weight()); + assert!(Source::ToolInput.weight() > Source::ToolOutput.weight()); + assert_eq!(Source::Thinking.weight(), Source::ToolOutput.weight()); + assert_eq!( + stronger(Some(Source::ToolOutput), Some(Source::User)), + Some(Source::User) + ); + assert_eq!( + stronger(None, Some(Source::Thinking)), + Some(Source::Thinking) + ); + let tokens = vec!["deploy".to_owned(), "api".to_owned(), "zzz".to_owned()]; + let sources = metadata_sources(&["deploy the thing"], "deploy api", &tokens); + assert_eq!( + sources, + [Some(Source::User), Some(Source::ToolOutput), None] + ); + assert_eq!(source_weight(&sources), 9); + } +} diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs index 73845c14b13..6a6c4b82408 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs @@ -1,6 +1,7 @@ //! Process-wide cache of decoded string values, never JSON keys or syntax. use super::super::preview_result; -use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor}; +use super::source::{Sink, Source, Walk}; +use serde::de::DeserializeSeed; use std::collections::HashMap; use std::io::Read; use std::path::{Path, PathBuf}; @@ -46,7 +47,7 @@ impl Fingerprint { } struct Entry { fingerprint: Fingerprint, - text: Arc>, + text: Arc, bytes: usize, used: u64, } @@ -62,7 +63,7 @@ impl Cache { self.bytes -= entry.bytes; } } - fn get(&mut self, path: &Path, fingerprint: Option<&Fingerprint>) -> Option>> { + fn get(&mut self, path: &Path, fingerprint: Option<&Fingerprint>) -> Option> { self.clock = self.clock.wrapping_add(1); if self .entries @@ -75,13 +76,7 @@ impl Cache { entry.used = self.clock; Some(entry.text.clone()) } - fn insert( - &mut self, - path: &Path, - fingerprint: Fingerprint, - text: Arc>, - bytes: usize, - ) { + fn insert(&mut self, path: &Path, fingerprint: Fingerprint, text: Arc, bytes: usize) { self.remove(path); while self.bytes + bytes > MAX_TOTAL || self.entries.len() >= MAX_ENTRIES { let Some(oldest) = self @@ -121,7 +116,7 @@ pub(super) fn is_cached_hint(path: &Path) -> bool { } /// Validate a resident entry on the search worker without opening a transcript. -pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option>> { +pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option> { if cancel.load(Ordering::Relaxed) { return None; } @@ -131,7 +126,7 @@ pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option Option>> { +pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option> { if cancel.load(Ordering::Relaxed) { return None; } @@ -179,71 +174,53 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option, + pub(crate) sources: Vec, +} +impl Segments { + pub(crate) fn iter(&self) -> impl Iterator { + self.text + .iter() + .map(String::as_str) + .zip(self.sources.iter().copied()) + } +} +#[cfg(test)] +impl From> for Segments { + fn from(text: Vec) -> Self { + let sources = vec![Source::Other; text.len()]; + Self { text, sources } + } +} + struct Collector<'a> { - text: Vec, + text: Segments, bytes: usize, cancel: &'a AtomicBool, overflow: bool, } -impl<'de> DeserializeSeed<'de> for &mut Collector<'_> { - type Value = (); - fn deserialize>(self, de: D) -> Result<(), D::Error> { - if self.cancel.load(Ordering::Relaxed) || self.overflow { - return Err(serde::de::Error::custom("collection stopped")); - } - de.deserialize_any(self) - } -} -impl<'de> Visitor<'de> for &mut Collector<'_> { - type Value = (); - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("transcript JSON") - } - fn visit_str(self, value: &str) -> Result<(), E> { +impl Sink for Collector<'_> { + fn text(&mut self, value: &str, source: Source) -> Result<(), &'static str> { if self.cancel.load(Ordering::Relaxed) { - return Err(E::custom("cancelled")); + return Err("cancelled"); } let lower = value.to_lowercase(); - self.bytes += lower.capacity() + std::mem::size_of::(); + self.bytes += lower.capacity() + std::mem::size_of::() + 1; if self.bytes > MAX_TEXT { self.overflow = true; - return Err(E::custom("cache text limit")); - } - self.text.push(lower); - Ok(()) - } - fn visit_string(self, value: String) -> Result<(), E> { - self.visit_str(&value) - } - fn visit_bool(self, _: bool) -> Result<(), E> { - Ok(()) - } - fn visit_i64(self, _: i64) -> Result<(), E> { - Ok(()) - } - fn visit_u64(self, _: u64) -> Result<(), E> { - Ok(()) - } - fn visit_f64(self, _: f64) -> Result<(), E> { - Ok(()) - } - fn visit_unit(self) -> Result<(), E> { - Ok(()) - } - fn visit_seq>(self, mut seq: A) -> Result<(), A::Error> { - while seq.next_element_seed(&mut *self)?.is_some() {} - Ok(()) - } - fn visit_map>(self, mut map: A) -> Result<(), A::Error> { - while map.next_key::()?.is_some() { - map.next_value_seed(&mut *self)?; + return Err("cache text limit"); } + self.text.text.push(lower); + self.text.sources.push(source); Ok(()) } } -fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Vec, usize)> { +fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Segments, usize)> { let mut collector = Collector { - text: Vec::new(), + text: Segments::default(), bytes: 0, cancel, overflow: false, @@ -259,8 +236,8 @@ fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Vec std::io::BufRead::read_until(&mut remaining, b'\n', &mut line), "transcript record read", )?; - if let Err(error) = - (&mut collector).deserialize(&mut serde_json::Deserializer::from_slice(&line)) + if let Err(error) = Walk::new(&mut collector) + .deserialize(&mut serde_json::Deserializer::from_slice(&line)) { // A partial/corrupt journal record must not prevent later records from matching. if !cancel.load(Ordering::Relaxed) && !collector.overflow { @@ -275,14 +252,21 @@ fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Vec } } else { let mut de = serde_json::Deserializer::from_slice(input); - while (&mut collector).deserialize(&mut de).is_ok() {} + while Walk::new(&mut collector).deserialize(&mut de).is_ok() {} } if cancel.load(Ordering::Relaxed) || collector.overflow { return None; } - collector.text.shrink_to_fit(); - let bytes = collector.text.capacity() * std::mem::size_of::() - + collector.text.iter().map(String::capacity).sum::(); + collector.text.text.shrink_to_fit(); + collector.text.sources.shrink_to_fit(); + let bytes = collector.text.text.capacity() * std::mem::size_of::() + + collector.text.sources.capacity() + + collector + .text + .text + .iter() + .map(String::capacity) + .sum::(); Some((collector.text, bytes)) } @@ -295,13 +279,14 @@ mod tests { let input = b"{\"KEY\": [\"FIRST\", 42, {\"ignored\":\"SECOND\"}]}\n{broken\n{\"x\":\"THIRD\"}"; assert_eq!( - decode(input, true, &cancel).unwrap().0, + decode(input, true, &cancel).unwrap().0.text, ["first", "second", "third"] ); assert_eq!( decode(br#"{"KEY":"ONE"} ["TWO", "\u0041"]"#, false, &cancel) .unwrap() - .0, + .0 + .text, ["one", "two", "a"] ); } @@ -314,7 +299,7 @@ mod tests { let first = segments(&path, &cancel).unwrap(); assert!(Arc::ptr_eq(&first, &segments(&path, &cancel).unwrap())); std::fs::write(&path, r#"{"key":"REPLACEMENT"}"#).unwrap(); - assert_eq!(&*segments(&path, &cancel).unwrap(), &["replacement"]); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["replacement"]); std::fs::remove_file(&path).unwrap(); assert!(segments(&path, &cancel).is_none()); cancel.store(true, Ordering::Relaxed); @@ -347,8 +332,8 @@ mod tests { .set_times(std::fs::FileTimes::new().set_modified(modified)) .unwrap(); std::fs::rename(&replacement, &path).unwrap(); - assert_eq!(&*first, &["first"]); - assert_eq!(&*segments(&path, &cancel).unwrap(), &["other"]); + assert_eq!(&first.text, &["first"]); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["other"]); } #[test] @@ -364,15 +349,25 @@ mod tests { cache.insert( Path::new(&index.to_string()), fp.clone(), - Arc::new(vec![]), + Arc::new(Segments::default()), 1, ); } cache.get(Path::new("0"), Some(&fp)).unwrap(); - cache.insert(Path::new("extra"), fp.clone(), Arc::new(vec![]), 1); + cache.insert( + Path::new("extra"), + fp.clone(), + Arc::new(Segments::default()), + 1, + ); assert!(cache.entries.contains_key(Path::new("0"))); assert!(!cache.entries.contains_key(Path::new("1"))); - cache.insert(Path::new("new"), fp.clone(), Arc::new(vec![]), MAX_TOTAL); + cache.insert( + Path::new("new"), + fp.clone(), + Arc::new(Segments::default()), + MAX_TOTAL, + ); assert_eq!(cache.entries.len(), 1); assert_eq!(cache.bytes, MAX_TOTAL); let cancel = AtomicBool::new(false); diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs index 20b6235a70b..90b788fad9d 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs @@ -1,4 +1,6 @@ //! Bounded token evidence tied to validated decoded-text generations. +use super::source::Source; +use super::text_cache::Segments; use std::collections::HashMap; use std::path::Path; use std::sync::{ @@ -11,13 +13,28 @@ const MAX_BYTES: usize = 1024 * 1024; const MAX_TOKEN_BYTES: usize = 4096; const MAX_PREFIX_PROBES: usize = 64; +/// A token found in a transcript: its first-match fuzzy score and the +/// strongest origin it appears in. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Hit { + pub(crate) score: i64, + pub(crate) source: Source, +} +#[cfg(test)] +pub(crate) fn hit(score: i64) -> Hit { + Hit { + score, + source: Source::Other, + } +} + struct Evidence { - outcome: Option, + outcome: Option, used: u64, bytes: usize, } struct Generation { - text: Weak>, + text: Weak, modes: [HashMap; 2], } // Include owned keys, values, and conservative hash-table bookkeeping. Text is @@ -52,7 +69,7 @@ impl Cache { } } } - fn get(&mut self, text: &Arc>, token: &str, fuzzy: bool) -> Option> { + fn get(&mut self, text: &Arc, token: &str, fuzzy: bool) -> Option> { if token.len() > MAX_TOKEN_BYTES { return None; } @@ -115,7 +132,7 @@ impl Cache { self.prune(); } } - fn insert(&mut self, text: &Arc>, token: &str, fuzzy: bool, outcome: Option) { + fn insert(&mut self, text: &Arc, token: &str, fuzzy: bool, outcome: Option) { if token.len() > MAX_TOKEN_BYTES { return; } @@ -175,7 +192,7 @@ pub(super) fn matches( tokens: &[String], cancel: &AtomicBool, fuzzy: bool, -) -> Option>> { +) -> Option>> { matches_with_scores( path, tokens, @@ -191,7 +208,7 @@ pub(super) fn matches_with_scores( cancel: &AtomicBool, fuzzy: bool, scores: &mut super::WordScores, -) -> Option>> { +) -> Option>> { let text = super::text_cache::segments(path, cancel)?; resolve( cache(), @@ -210,7 +227,7 @@ pub(super) fn matches_cached( tokens: &[String], cancel: &AtomicBool, fuzzy: bool, -) -> Option>> { +) -> Option>> { matches_cached_with_scores( path, tokens, @@ -228,7 +245,7 @@ pub(super) fn matches_cached_with_scores( cancel: &AtomicBool, fuzzy: bool, scores: &mut super::WordScores, -) -> Option>> { +) -> Option>> { let text = super::text_cache::cached_segments(path, cancel)?; resolve( cache(), @@ -242,12 +259,12 @@ pub(super) fn matches_cached_with_scores( fn resolve( cache: &Mutex, - text: &Arc>, + text: &Arc, tokens: &[String], cancel: &AtomicBool, fuzzy: bool, - mut scan: impl FnMut(&[String], &str, &AtomicBool, bool) -> Option>, -) -> Option>> { + mut scan: impl FnMut(&Segments, &str, &AtomicBool, bool) -> Option>, +) -> Option>> { let mut outcomes = Vec::with_capacity(tokens.len()); for token in tokens { if cancel.load(Ordering::Relaxed) { @@ -276,11 +293,11 @@ fn resolve( } #[cfg(test)] fn scan_token( - text: &[String], + text: &Segments, token: &str, cancel: &AtomicBool, fuzzy: bool, -) -> Option> { +) -> Option> { scan_token_with_scores( text, token, @@ -290,32 +307,36 @@ fn scan_token( ) } +/// Matches the streaming scanner: the first matching segment sets the score, +/// later literal occurrences can only raise the origin. Stops early once the +/// token is seen in a user prompt, the strongest origin. fn scan_token_with_scores( - text: &[String], + text: &Segments, token: &str, cancel: &AtomicBool, fuzzy: bool, scores: &mut super::WordScores, -) -> Option> { +) -> Option> { let tokens = [token.to_owned()]; - let mut scan = super::Scan { - tokens: &tokens, - found: vec![false], - cancel, - fuzzy, - score: 0, - scores, + let mode = if fuzzy { + super::ScanMode::Fuzzy + } else { + super::ScanMode::Literal }; - for segment in text { + let mut scan = super::Scan::new(&tokens, &[], cancel, mode, scores); + for (segment, source) in text.iter() { if cancel.load(Ordering::Relaxed) { return None; } - let scanned = scan.normalized_text::(segment); - // The visitor uses an error sentinel to stop JSON traversal after a hit. - if scan.complete() { - return Some(Some(scan.score)); + if scan.sources[0] == Some(Source::User) { + break; } - if let Err(error) = scanned { + let scanned = scan.normalized_text::(segment, source); + // The scanner signals completion with an error sentinel. Keep reading + // for a stronger origin, but stop on a genuine failure. + if let Err(error) = scanned + && !scan.all_found() + { if !cancel.load(Ordering::Relaxed) { crate::logging::debug(&format!("Transcript token scan stopped: {error}")); } @@ -325,7 +346,10 @@ fn scan_token_with_scores( if cancel.load(Ordering::Relaxed) { None } else { - Some(None) + Some(scan.sources[0].map(|source| Hit { + score: scan.score, + source, + })) } } @@ -339,7 +363,7 @@ mod tests { #[test] fn long_queries_bound_prefix_probes_and_fall_back_safely() { let mut cache = Cache::default(); - let text = Arc::new(Vec::new()); + let text = Arc::new(Segments::default()); cache.insert(&text, "x", false, None); assert_eq!( cache.get(&text, &"x".repeat(MAX_TOKEN_BYTES + 1), false), @@ -354,7 +378,7 @@ mod tests { #[test] fn completed_tokens_and_backspacing_reuse_positive_and_negative_evidence() { let cache = Mutex::new(Cache::default()); - let text = Arc::new(tokens(&["alpha beta"])); + let text = Arc::new(Segments::from(tokens(&["alpha beta"]))); let cancel = AtomicBool::new(false); let mut calls = 0; for query in [ @@ -374,7 +398,7 @@ mod tests { }, ) .unwrap(); - assert_eq!(result[0], Some(0)); + assert_eq!(result[0], Some(hit(0))); assert_eq!(result[1], None); } assert_eq!(calls, 3); @@ -387,7 +411,10 @@ mod tests { let cancel = AtomicBool::new(false); let query = tokens(&["alpha"]); std::fs::write(&path, "\"alpha\"").unwrap(); - assert_eq!(matches(&path, &query, &cancel, false), Some(vec![Some(0)])); + assert_eq!( + matches(&path, &query, &cancel, false), + Some(vec![Some(hit(0))]) + ); std::fs::write(&path, "\"replacement\"").unwrap(); assert_eq!(matches(&path, &query, &cancel, false), Some(vec![None])); std::fs::remove_file(&path).unwrap(); @@ -396,19 +423,19 @@ mod tests { #[test] fn fuzzy_matches_single_scan_first_segment_scores_and_separate_mode() { - let text = Arc::new(tokens(&["alphabet betta", "alpha beta"])); + let text = Arc::new(Segments::from(tokens(&["alphabet betta", "alpha beta"]))); let query = tokens(&["alpha", "beta", "unmatched", "alp"]); let cancel = AtomicBool::new(false); - let mut scan = super::super::Scan { - tokens: &query, - found: vec![false; query.len()], - cancel: &cancel, - fuzzy: true, - score: 0, - scores: &mut super::super::WordScores::default(), - }; - for segment in text.iter() { - let _ = scan.normalized_text::(segment); + let mut scores = super::super::WordScores::default(); + let mut scan = super::super::Scan::new( + &query, + &[], + &cancel, + super::super::ScanMode::Fuzzy, + &mut scores, + ); + for (segment, source) in text.iter() { + let _ = scan.normalized_text::(segment, source); } let cache = Mutex::new(Cache::default()); let result = resolve(&cache, &text, &query, &cancel, true, scan_token).unwrap(); @@ -418,20 +445,23 @@ mod tests { }), Some(result.clone()) ); - assert_eq!(result.iter().flatten().sum::(), scan.score); + assert_eq!( + result.iter().flatten().map(|hit| hit.score).sum::(), + scan.score + ); assert_eq!( result.iter().map(Option::is_some).collect::>(), scan.found ); let literal = resolve(&cache, &text, &query, &cancel, false, scan_token).unwrap(); - assert!(literal.iter().flatten().all(|score| *score == 0)); - assert!(result[0].unwrap() > 0); + assert!(literal.iter().flatten().all(|hit| hit.score == 0)); + assert!(result[0].unwrap().score > 0); } #[test] fn cancellation_keeps_completed_work_not_incomplete_negatives() { let cache = Mutex::new(Cache::default()); - let text = Arc::new(tokens(&["alpha"])); + let text = Arc::new(Segments::from(tokens(&["alpha"]))); let cancel = AtomicBool::new(false); let query = tokens(&["alpha", "absent", "missing"]); assert!( @@ -452,7 +482,7 @@ mod tests { .is_none() ); let mut guard = cache.lock().unwrap(); - assert_eq!(guard.get(&text, "alpha", false), Some(Some(0))); + assert_eq!(guard.get(&text, "alpha", false), Some(Some(hit(0)))); assert_eq!(guard.get(&text, "absent", false), Some(None)); assert_eq!(guard.get(&text, "missing", false), None); drop(guard); @@ -460,7 +490,7 @@ mod tests { cancel.store(false, Ordering::Relaxed); assert_eq!( resolve(&cache, &text, &query, &cancel, false, scan_token), - Some(vec![Some(0), None, None]) + Some(vec![Some(hit(0)), None, None]) ); } @@ -477,7 +507,7 @@ mod tests { None ); assert!(super::super::text_cache::cached_segments(&path, &cancel).is_none()); - let expected = Some(vec![Some(0), None]); + let expected = Some(vec![Some(hit(0)), None]); assert_eq!( matches_with_scores(&path, &query, &cancel, false, &mut scores), expected @@ -513,9 +543,10 @@ mod tests { .into_iter() .enumerate() { + let segments = Segments::from(text.clone()); let expected = query .iter() - .map(|token| scan_token(&text, token, &cancel, true).unwrap()) + .map(|token| scan_token(&segments, token, &cancel, true).unwrap()) .collect::>(); let path = dir.path().join(format!("{index}.json")); std::fs::write(&path, serde_json::to_vec(&text).unwrap()).unwrap(); @@ -533,7 +564,7 @@ mod tests { #[test] fn literal_negative_extensions_skip_scans_but_not_backspacing_or_fuzzy() { let cache = Mutex::new(Cache::default()); - let text = Arc::new(tokens(&["alphabet"])); + let text = Arc::new(Segments::from(tokens(&["alphabet"]))); let cancel = AtomicBool::new(false); assert_eq!( resolve(&cache, &text, &tokens(&["zz"]), &cancel, false, scan_token), @@ -569,12 +600,12 @@ mod tests { } assert_eq!(calls, 2); let mut guard = cache.lock().unwrap(); - guard.insert(&text, "alpha", false, Some(0)); + guard.insert(&text, "alpha", false, Some(hit(0))); assert_eq!(guard.get(&text, "alphabeta", false), None); guard.insert(&text, "missing", true, None); assert_eq!(guard.get(&text, "missinglonger", true), None); assert_eq!(guard.get(&text, "missinglonger", false), None); - let other = Arc::new(tokens(&["zzmore"])); + let other = Arc::new(Segments::from(tokens(&["zzmore"]))); assert_eq!(guard.get(&other, "zzmore", false), None); guard.insert(&text, "界", false, None); assert_eq!(guard.get(&text, "界面", false), Some(None)); @@ -584,21 +615,21 @@ mod tests { #[test] fn warm_lookups_do_not_prune_unrelated_generations() { let mut cache = Cache::default(); - let stale = Arc::new(tokens(&["stale"])); + let stale = Arc::new(Segments::from(tokens(&["stale"]))); let stale_key = Arc::as_ptr(&stale) as usize; - cache.insert(&stale, "old", false, Some(0)); + cache.insert(&stale, "old", false, Some(hit(0))); drop(stale); - let live = Arc::new(tokens(&["live"])); - cache.insert(&live, "hit", false, Some(0)); + let live = Arc::new(Segments::from(tokens(&["live"]))); + cache.insert(&live, "hit", false, Some(hit(0))); let insertions = cache.insertions; for _ in 0..PRUNE_EVERY * 2 { - assert_eq!(cache.get(&live, "hit", false), Some(Some(0))); + assert_eq!(cache.get(&live, "hit", false), Some(Some(hit(0)))); assert_eq!(cache.get(&live, "unknown", true), None); } assert_eq!(cache.insertions, insertions); assert!(cache.generations.contains_key(&stale_key)); for i in 0..PRUNE_EVERY { - cache.insert(&live, &format!("new-{i}"), false, Some(0)); + cache.insert(&live, &format!("new-{i}"), false, Some(hit(0))); } assert!(!cache.generations.contains_key(&stale_key)); assert_eq!(cache.entries, PRUNE_EVERY + 1); @@ -607,30 +638,30 @@ mod tests { #[test] fn generation_maps_and_batch_lru_keep_recent_hits_at_full_capacity() { let mut cache = Cache::default(); - let texts: Vec<_> = (0..256).map(|_| Arc::new(Vec::new())).collect(); + let texts: Vec<_> = (0..256).map(|_| Arc::new(Segments::default())).collect(); for text in &texts { for token in 0..16 { - cache.insert(text, &format!("token-{token}!"), false, Some(0)); + cache.insert(text, &format!("token-{token}!"), false, Some(hit(0))); } } assert_eq!(cache.entries, MAX_ENTRIES); assert_eq!(cache.generations.len(), 256); - assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(0))); + assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(hit(0)))); assert_eq!(cache.get(&texts[0], "token-0!", true), None); - cache.insert(&texts[255], "new", false, Some(0)); + cache.insert(&texts[255], "new", false, Some(hit(0))); assert!(cache.entries <= MAX_ENTRIES * 7 / 8); - assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(0))); + assert_eq!(cache.get(&texts[0], "token-0!", false), Some(Some(hit(0)))); assert_eq!(cache.get(&texts[0], "token-1!", false), None); - assert_eq!(cache.get(&texts[255], "new", false), Some(Some(0))); + assert_eq!(cache.get(&texts[255], "new", false), Some(Some(hit(0)))); assert!(cache.bytes <= MAX_BYTES); } #[test] fn weak_generations_lru_entry_and_key_byte_limits() { let mut cache = Cache::default(); - let text = Arc::new(Vec::new()); + let text = Arc::new(Segments::default()); cache.insert(&text, "old", false, None); - let distinct_generation = Arc::new(Vec::new()); + let distinct_generation = Arc::new(Segments::default()); assert_eq!(cache.get(&distinct_generation, "old", false), None); let weak = Arc::downgrade(&text); drop(text); @@ -638,12 +669,12 @@ mod tests { cache.prune(); assert_eq!(cache.entries, 0); assert!(cache.generations.is_empty()); - let text = Arc::new(Vec::new()); + let text = Arc::new(Segments::default()); for i in 0..MAX_ENTRIES { cache.insert(&text, &format!("{i}!"), false, None); } assert_eq!(cache.get(&text, "0!", false), Some(None)); - cache.insert(&text, "new", false, Some(0)); + cache.insert(&text, "new", false, Some(hit(0))); assert_eq!(cache.get(&text, "1!", false), None); assert_eq!(cache.get(&text, "0!", false), Some(None)); for i in 0..MAX_ENTRIES { diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs index f2c4be6c82d..f51ffbe5398 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs @@ -85,7 +85,7 @@ mod tests { std::thread::sleep(Duration::from_millis(1)); } let text = text_cache::cached_segments(&path, &AtomicBool::new(false)).unwrap(); - assert_eq!(&*text, &["prepared while typing"]); + assert_eq!(&text.text, &["prepared while typing"]); assert!(text_cache::cached_segments(&path, &query_cancel).is_none()); } diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs index de6779f9042..211859cb5bf 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs @@ -130,14 +130,8 @@ fn cached_path_scores_preserve_first_path_and_cross_file_tokens() { ("absent", false), ] { let tokens = search_query_tokens(query); - let mut reference = Scan { - tokens: &tokens, - found: vec![false; tokens.len()], - cancel: &cancel, - fuzzy, - score: 0, - scores: &mut WordScores::default(), - }; + let mut scores = WordScores::default(); + let mut reference = Scan::new(&tokens, &[], &cancel, mode(fuzzy), &mut scores); for path in &paths { scan_file_streaming(path, &mut reference); if reference.complete() { @@ -146,7 +140,7 @@ fn cached_path_scores_preserve_first_path_and_cross_file_tokens() { } assert_eq!( cached_paths_scan(&paths, &tokens, &cancel, fuzzy), - Some(reference.complete().then_some(reference.score)) + Some(reference.all_found().then_some(reference.score)) ); } } @@ -184,16 +178,9 @@ fn incremental_token_sequences_match_fresh_streaming_results_and_scores() { for second in words { let tokens = vec![first.to_string(), second.to_string()]; let mut scores = WordScores::default(); - let mut reference = Scan { - tokens: &tokens, - found: vec![false; tokens.len()], - cancel: &cancel, - fuzzy, - score: 0, - scores: &mut scores, - }; + let mut reference = Scan::new(&tokens, &[], &cancel, mode(fuzzy), &mut scores); scan_file_streaming(&path, &mut reference); - let expected = reference.complete().then_some(reference.score); + let expected = reference.all_found().then_some(reference.score); assert_eq!( cached_paths_scan(std::slice::from_ref(&path), &tokens, &cancel, fuzzy), Some(expected), @@ -411,6 +398,7 @@ fn candidate_priority_prefers_opening_then_intent_and_preserves_ties() { exact_field: false, opening_prompt: opening.into(), intent_text: intent.into(), + prior: Vec::new(), }; let candidates = vec![ candidate("weak-first", "unrelated", ""), From 2f8957ca6cd9eea4ea0713ffcd5947fc8077b45b Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:21:57 -0800 Subject: [PATCH 086/236] Cut idle battery drain: reap exec-inherited children, MCP process groups and exponential reconnect backoff, stamp-gated git polling --- crates/jcode-app-core/src/server.rs | 4 + crates/jcode-base/src/inherited_children.rs | 142 ++++++++++++++++++++ crates/jcode-base/src/lib.rs | 1 + crates/jcode-base/src/mcp/client.rs | 22 ++- crates/jcode-base/src/mcp/pool.rs | 40 +++++- crates/jcode-tui/src/tui/app/helpers.rs | 83 ++++++++++++ 6 files changed, 289 insertions(+), 3 deletions(-) create mode 100644 crates/jcode-base/src/inherited_children.rs diff --git a/crates/jcode-app-core/src/server.rs b/crates/jcode-app-core/src/server.rs index eace686427e..6b2183c0dde 100644 --- a/crates/jcode-app-core/src/server.rs +++ b/crates/jcode-app-core/src/server.rs @@ -2356,6 +2356,10 @@ impl Server { #[cfg(unix)] let _daemon_lock = acquire_daemon_lock_for(&self.socket_path)?; + // Reap zombies and stop orphaned MCP servers left by a previous image + // of this PID (exec reload). Must run before this image spawns children. + crate::inherited_children::start(); + if socket_has_live_listener(&self.socket_path).await { anyhow::bail!( "Refusing to replace active server socket at {}", diff --git a/crates/jcode-base/src/inherited_children.rs b/crates/jcode-base/src/inherited_children.rs new file mode 100644 index 00000000000..7e6337b409d --- /dev/null +++ b/crates/jcode-base/src/inherited_children.rs @@ -0,0 +1,142 @@ +//! Reap child processes inherited across an `exec` reload. +//! +//! The daemon reloads itself with `exec`, which keeps the PID. Children the +//! previous image spawned (MCP servers, power-inhibit helpers, shell tools) +//! stay our children, but the new image holds no `Child` handle for them. Their +//! pipes are close-on-exec, so most exit right after the reload and then sit +//! as zombies forever because nobody calls `waitpid`. Over a day of self-dev +//! reloads that reached hundreds of zombie `npm`/`node` entries. +//! +//! Only PIDs present at startup are touched. Children spawned by this image +//! are owned by tokio/std handles, and reaping them here would steal their +//! exit status. An inherited PID cannot be reused while it is unreaped, so the +//! snapshot stays accurate until we reap it. + +/// Environment marker set on MCP stdio children so a fresh image can tell +/// leaked MCP servers (safe to kill: the new image reconnects its own) apart +/// from detached work that is meant to outlive a reload. +pub const MCP_CHILD_MARKER_ENV: &str = "JCODE_MCP_CHILD"; + +#[cfg(target_os = "linux")] +mod imp { + use std::collections::HashSet; + use std::time::Duration; + + const REAP_INTERVAL: Duration = Duration::from_secs(30); + + fn children_of(pid: u32) -> HashSet { + let mut out = HashSet::new(); + let Ok(tasks) = std::fs::read_dir(format!("/proc/{pid}/task")) else { + return out; + }; + for task in tasks.flatten() { + let path = task.path().join("children"); + if let Ok(text) = std::fs::read_to_string(path) { + out.extend( + text.split_whitespace() + .filter_map(|p| p.parse::().ok()), + ); + } + } + out + } + + fn has_mcp_marker(pid: i32) -> bool { + let Ok(environ) = std::fs::read(format!("/proc/{pid}/environ")) else { + return false; + }; + let needle = format!("{}=", super::MCP_CHILD_MARKER_ENV); + environ + .split(|b| *b == 0) + .any(|entry| entry.starts_with(needle.as_bytes())) + } + + /// Reap whatever has exited. Returns the PIDs still alive. + fn reap(pids: &mut HashSet) { + pids.retain(|&pid| { + let mut status = 0; + // SAFETY: plain syscall on a PID we inherited and nobody else owns. + let rc = unsafe { libc::waitpid(pid, &mut status, libc::WNOHANG) }; + rc == 0 + }); + } + + pub fn start() { + let mut inherited = children_of(std::process::id()); + if inherited.is_empty() { + return; + } + let mut killed = 0usize; + for &pid in &inherited { + if has_mcp_marker(pid) { + // MCP children run in their own process group (see mcp client), + // so this also stops `npx` wrappers' node grandchildren. + // SAFETY: signalling a process group we created. + unsafe { + libc::kill(-pid, libc::SIGKILL); + libc::kill(pid, libc::SIGKILL); + } + killed += 1; + } + } + let before = inherited.len(); + reap(&mut inherited); + crate::logging::info(&format!( + "inherited_children: {before} inherited, killed {killed} stale MCP servers, reaped {}", + before - inherited.len() + )); + if inherited.is_empty() { + return; + } + let _ = std::thread::Builder::new() + .name("jcode-inherited-reaper".to_string()) + .spawn(move || { + while !inherited.is_empty() { + std::thread::sleep(REAP_INTERVAL); + reap(&mut inherited); + } + }); + } + + #[cfg(test)] + mod tests { + #[test] + fn reaps_exited_inherited_child() { + let child = std::process::Command::new("true").spawn().unwrap(); + let pid = child.id() as i32; + std::mem::forget(child); + std::thread::sleep(std::time::Duration::from_millis(200)); + let mut set = std::collections::HashSet::from([pid]); + super::reap(&mut set); + assert!(set.is_empty()); + assert!(!std::path::Path::new(&format!("/proc/{pid}")).exists()); + } + + #[test] + fn detects_mcp_marker() { + let mut child = std::process::Command::new("sleep") + .arg("5") + .env(super::super::MCP_CHILD_MARKER_ENV, "x") + .spawn() + .unwrap(); + let mut plain = std::process::Command::new("sleep") + .arg("5") + .spawn() + .unwrap(); + assert!(super::has_mcp_marker(child.id() as i32)); + assert!(!super::has_mcp_marker(plain.id() as i32)); + assert!(super::children_of(std::process::id()).contains(&(child.id() as i32))); + let _ = child.kill(); + let _ = plain.kill(); + let _ = child.wait(); + let _ = plain.wait(); + } + } +} + +/// Kill stale MCP servers and reap exited children inherited from a previous +/// process image. Call once at daemon startup, before spawning anything. +pub fn start() { + #[cfg(target_os = "linux")] + imp::start(); +} diff --git a/crates/jcode-base/src/lib.rs b/crates/jcode-base/src/lib.rs index 69bf6a36bbb..76eba5840f6 100644 --- a/crates/jcode-base/src/lib.rs +++ b/crates/jcode-base/src/lib.rs @@ -54,6 +54,7 @@ pub mod hooks; pub mod id; pub mod image_normalize; pub mod import; +pub mod inherited_children; pub mod jev; pub mod kv_cache_monitor; pub mod live_tests; diff --git a/crates/jcode-base/src/mcp/client.rs b/crates/jcode-base/src/mcp/client.rs index 2140a204156..b097936f5b8 100644 --- a/crates/jcode-base/src/mcp/client.rs +++ b/crates/jcode-base/src/mcp/client.rs @@ -224,9 +224,15 @@ impl McpClient { command .args(&config.args) .envs(&env) + .env(crate::inherited_children::MCP_CHILD_MARKER_ENV, "1") .stdin(Stdio::piped()) .stdout(Stdio::piped()) - .stderr(Stdio::piped()); + .stderr(Stdio::piped()) + .kill_on_drop(true); + // Own process group so shutdown also stops `npx` wrapper descendants + // (node), which otherwise outlive the wrapper and keep running. + #[cfg(unix)] + command.process_group(0); if let Some(dir) = working_dir { command.current_dir(dir); } @@ -568,6 +574,7 @@ impl McpClient { tokio::time::sleep(std::time::Duration::from_millis(100)).await; + kill_process_group(child); let _ = child.kill().await; } Backend::Remote(connection) => connection.shutdown().await, @@ -656,11 +663,24 @@ fn mcp_child_env( impl Drop for McpClient { fn drop(&mut self) { if let Backend::Stdio(child) = &mut self.backend { + kill_process_group(child); let _ = child.start_kill(); } } } +fn kill_process_group(child: &Child) { + #[cfg(unix)] + if let Some(pid) = child.id() { + // SAFETY: the child leads its own process group (process_group(0)). + unsafe { + libc::kill(-(pid as i32), libc::SIGKILL); + } + } + #[cfg(not(unix))] + let _ = child; +} + #[cfg(all(test, unix))] mod tests { use super::{McpClient, is_sensitive_inherited_env_key, mcp_child_env}; diff --git a/crates/jcode-base/src/mcp/pool.rs b/crates/jcode-base/src/mcp/pool.rs index 7822ec58a2d..24fc11d72e4 100644 --- a/crates/jcode-base/src/mcp/pool.rs +++ b/crates/jcode-base/src/mcp/pool.rs @@ -18,11 +18,28 @@ use std::time::{Duration, Instant}; use tokio::sync::{Mutex, Notify, RwLock}; const FAILED_CONNECT_RETRY_COOLDOWN: Duration = Duration::from_secs(30); +/// Repeated failures (e.g. `mcp-remote` stuck waiting for OAuth) back off +/// exponentially up to this cap instead of respawning `npx` every 30s. +const MAX_FAILED_CONNECT_RETRY_COOLDOWN: Duration = Duration::from_secs(30 * 60); #[derive(Clone)] struct FailedConnectRecord { message: String, failed_at: Instant, + failures: u32, +} + +impl FailedConnectRecord { + fn cooldown(&self) -> Duration { + failed_connect_cooldown(self.failures) + } +} + +fn failed_connect_cooldown(failures: u32) -> Duration { + let shift = failures.saturating_sub(1).min(10); + FAILED_CONNECT_RETRY_COOLDOWN + .saturating_mul(1 << shift) + .min(MAX_FAILED_CONNECT_RETRY_COOLDOWN) } enum ConnectAttempt { @@ -342,11 +359,13 @@ impl SharedMcpPool { } Err(error) => { let mut errors = self.last_errors.write().await; + let failures = errors.get(name).map_or(0, |r| r.failures) + 1; errors.insert( name.to_string(), FailedConnectRecord { message: format!("{:#}", error), failed_at: Instant::now(), + failures, }, ); } @@ -372,7 +391,8 @@ impl SharedMcpPool { config: McpServerConfig, ) -> std::result::Result { if let Some(record) = self.recent_failure(&name).await { - let retry_after = FAILED_CONNECT_RETRY_COOLDOWN + let retry_after = record + .cooldown() .saturating_sub(record.failed_at.elapsed()) .as_secs() .max(1); @@ -455,7 +475,7 @@ impl SharedMcpPool { .read() .await .get(name) - .filter(|record| record.failed_at.elapsed() < FAILED_CONNECT_RETRY_COOLDOWN) + .filter(|record| record.failed_at.elapsed() < record.cooldown()) .cloned() } } @@ -601,3 +621,19 @@ mod tests { ); } } + +#[cfg(test)] +mod backoff_tests { + use super::*; + + #[test] + fn failed_connect_cooldown_grows_and_caps() { + assert_eq!(failed_connect_cooldown(1), Duration::from_secs(30)); + assert_eq!(failed_connect_cooldown(2), Duration::from_secs(60)); + assert_eq!(failed_connect_cooldown(4), Duration::from_secs(240)); + assert_eq!( + failed_connect_cooldown(50), + MAX_FAILED_CONNECT_RETRY_COOLDOWN + ); + } +} diff --git a/crates/jcode-tui/src/tui/app/helpers.rs b/crates/jcode-tui/src/tui/app/helpers.rs index be857f9ef85..769c4efe12c 100644 --- a/crates/jcode-tui/src/tui/app/helpers.rs +++ b/crates/jcode-tui/src/tui/app/helpers.rs @@ -23,6 +23,51 @@ type GitInfoCacheEntry = (std::time::Instant, Option, bool); static GIT_INFO_CACHE: Mutex> = Mutex::new(None); static GIT_LINE_CACHE: std::sync::LazyLock> = std::sync::LazyLock::new(|| Mutex::new(git_line_cache::GitLineCache::default())); +/// Cheap `.git` metadata stamp from the last full probe, plus when that probe +/// ran. While the stamp is unchanged, TTL expiry only re-stats a few files +/// instead of spawning six git processes every 5s per client. +static GIT_PROBE_STAMP: Mutex> = Mutex::new(None); + +/// Full probes still run this often with an unchanged stamp, because editor +/// writes to tracked files do not touch `.git` until something stages them. +#[cfg(not(test))] +const GIT_FULL_PROBE_INTERVAL: Duration = Duration::from_secs(60); + +type GitStamp = Vec<(std::path::PathBuf, Option<(std::time::SystemTime, u64)>)>; + +/// mtimes of the files that change on commit, checkout, stage, fetch, and +/// branch updates. `None` when the git dir cannot be resolved cheaply. +fn git_stamp(repo_root: &std::path::Path) -> Option { + let git_dir = repo_root.join(".git"); + if !git_dir.is_dir() { + return None; + } + let mut paths = vec![ + git_dir.join("HEAD"), + git_dir.join("index"), + git_dir.join("FETCH_HEAD"), + git_dir.join("packed-refs"), + ]; + if let Ok(head) = std::fs::read_to_string(git_dir.join("HEAD")) + && let Some(reference) = head.trim().strip_prefix("ref: ") + { + paths.push(git_dir.join(reference)); + if let Some(branch) = reference.strip_prefix("refs/heads/") { + paths.push(git_dir.join("refs/remotes/origin").join(branch)); + } + } + Some( + paths + .into_iter() + .map(|path| { + let meta = std::fs::metadata(&path) + .ok() + .and_then(|m| Some((m.modified().ok()?, m.len()))); + (path, meta) + }) + .collect(), + ) +} /// Stale-while-revalidate cache for per-session todos plus their goal-level /// assessments (closed feedback loop etc.). Module-level so the app can force a @@ -76,6 +121,9 @@ pub(crate) fn invalidate_git_info_cache() { *ts = backdated_now(Duration::from_secs(3600)); *refreshing = false; } + if let Ok(mut stamp) = GIT_PROBE_STAMP.lock() { + *stamp = None; + } } /// Pin the git-status widget to a fixed value for deterministic renders. @@ -1236,6 +1284,17 @@ pub(super) fn gather_git_info() -> Option { if *refreshing { return cached.clone(); } + // Nothing in .git moved and the last full probe is recent: + // extend the entry without spawning any git process. + if let Some(root) = cached.as_ref().and_then(|info| info.repo_root.as_deref()) + && let Ok(stamp_guard) = GIT_PROBE_STAMP.lock() + && let Some((probed_at, stamp)) = stamp_guard.as_ref() + && probed_at.elapsed() < GIT_FULL_PROBE_INTERVAL + && git_stamp(root).as_ref() == Some(stamp) + { + *ts = std::time::Instant::now(); + return cached.clone(); + } let stale = cached.clone(); *refreshing = true; spawn_git_info_refresh(); @@ -1257,6 +1316,13 @@ pub(super) fn gather_git_info() -> Option { fn spawn_git_info_refresh() { std::thread::spawn(|| { let result = gather_git_info_inner(); + if let Ok(mut stamp) = GIT_PROBE_STAMP.lock() { + *stamp = result + .as_ref() + .and_then(|info| info.repo_root.as_deref()) + .and_then(git_stamp) + .map(|s| (std::time::Instant::now(), s)); + } let changed = if let Ok(mut guard) = GIT_INFO_CACHE.lock() { let changed = guard .as_ref() @@ -1803,3 +1869,20 @@ pub(crate) fn porcelain_status_letter(index: u8, worktree: u8) -> char { #[cfg(test)] #[path = "helpers/account_notice_tests.rs"] mod account_notice_tests; + +#[cfg(test)] +mod git_stamp_tests { + #[test] + fn stamp_changes_when_head_ref_moves() { + let dir = tempfile::tempdir().unwrap(); + let git = dir.path().join(".git"); + std::fs::create_dir_all(git.join("refs/heads")).unwrap(); + std::fs::write(git.join("HEAD"), "ref: refs/heads/main\n").unwrap(); + std::fs::write(git.join("refs/heads/main"), "a\n").unwrap(); + let before = super::git_stamp(dir.path()).unwrap(); + assert_eq!(super::git_stamp(dir.path()).unwrap(), before); + std::fs::write(git.join("refs/heads/main"), "bb\n").unwrap(); + assert_ne!(super::git_stamp(dir.path()).unwrap(), before); + assert!(super::git_stamp(&dir.path().join("missing")).is_none()); + } +} From 9d4ed83bb3c3060778344e622f218c007f5d656d Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:38:34 -0800 Subject: [PATCH 087/236] Reap inherited children in every process image, not only the server Clients exec-reload too, and git probes in flight at reload became zombies nobody waited on (27 seen across 7 clients). Start the reaper at process startup and make start() idempotent so the server's call is a no-op. --- crates/jcode-base/src/inherited_children.rs | 8 +++++++- src/main.rs | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/jcode-base/src/inherited_children.rs b/crates/jcode-base/src/inherited_children.rs index 7e6337b409d..ea9d7888296 100644 --- a/crates/jcode-base/src/inherited_children.rs +++ b/crates/jcode-base/src/inherited_children.rs @@ -137,6 +137,12 @@ mod imp { /// Kill stale MCP servers and reap exited children inherited from a previous /// process image. Call once at daemon startup, before spawning anything. pub fn start() { + // Idempotent: process startup calls this for every mode (clients exec + // reload too), and the server calls it again. A second snapshot taken + // after this image spawned children would steal their exit statuses. + static ONCE: std::sync::Once = std::sync::Once::new(); #[cfg(target_os = "linux")] - imp::start(); + ONCE.call_once(imp::start); + #[cfg(not(target_os = "linux"))] + ONCE.call_once(|| {}); } diff --git a/src/main.rs b/src/main.rs index 5ccb73ad379..ab3ed7b6d0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,6 +137,10 @@ fn run_main() -> Result<()> { return jcode::cli::hibernate_wait::run(); } + // Clients exec-reload too: reap git probes and other children a previous + // image left running, before this image spawns anything of its own. + jcode::inherited_children::start(); + let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build()?; From b771fd11d2e156718c5b4d60739d296cc43ebce2 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:41:38 -0800 Subject: [PATCH 088/236] Weight /resume metadata index hits by source section: user prompts over replies over tool output --- .../src/tui/session_picker/loading.rs | 44 ++-- .../tui/session_picker/loading_search_text.rs | 41 +++- .../tui/session_picker/transcript_search.rs | 2 +- .../transcript_search/source.rs | 199 +++++++++++++++++- 4 files changed, 257 insertions(+), 29 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/loading.rs b/crates/jcode-tui/src/tui/session_picker/loading.rs index f9a504b4dc1..7e3418b7c30 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading.rs @@ -415,8 +415,12 @@ fn send_session_list_updates_with_discovery( #[path = "loading_search_text.rs"] mod search_text; +use super::transcript_search::source::{ + BucketSink, Source, SourceBuckets, Walk, message_role_source, push_mark, +}; pub(super) use search_text::{ - build_search_index, push_sampled_search_text, raw_value_display_text, raw_value_search_excerpt, + build_search_index, classified_search_excerpt, push_sampled_search_text, + raw_value_display_text, raw_value_search_excerpt, }; #[cfg(test)] @@ -1271,7 +1275,7 @@ struct SessionMessageSummaryData { assistant_message_count: usize, estimated_tokens: usize, first_user_prompt: Option, - search_text: String, + search_text: SourceBuckets, /// Newest stored message timestamp, of any role (prompts, replies, tools). last_message_at: Option>, } @@ -1300,12 +1304,9 @@ impl SessionMessageSummaryData { .estimated_tokens .saturating_add(usage.total_tokens() as usize); } - if let Some(raw_content) = message.content_raw.as_deref() { - push_sampled_search_text( - &mut self.search_text, - raw_content, - INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES, - ); + if let Some(content) = &message.content_search { + self.search_text + .merge(content, INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES); } } @@ -1327,11 +1328,8 @@ impl SessionMessageSummaryData { if self.first_user_prompt.is_none() { self.first_user_prompt = other.first_user_prompt; } - push_sampled_search_text( - &mut self.search_text, - &other.search_text, - INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES, - ); + self.search_text + .merge(&other.search_text, INITIAL_TRANSCRIPT_SEARCH_BUDGET_BYTES); } } @@ -1397,7 +1395,8 @@ struct SessionMessageSummary { // allocating or walking it. After display metadata is known, we inspect only // the raw prefix for old snapshots that predate `display_role`. content_starts_with_system_reminder: bool, - content_raw: Option, + /// Bounded per-source samples of the content, classified by block type. + content_search: Option, content_text: Option, display_role: Option, token_usage: Option, @@ -1463,8 +1462,9 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { let content_starts_with_system_reminder = matches!(role, Role::User) && display_role.is_none() && content.is_some_and(raw_content_starts_with_system_reminder); - let content_raw = if display_role.is_none() && !content_starts_with_system_reminder { - content.and_then(|raw| raw_value_search_excerpt(raw, self.content_excerpt_budget)) + let content_search = if display_role.is_none() && !content_starts_with_system_reminder { + content + .and_then(|raw| classified_search_excerpt(raw, &role, self.content_excerpt_budget)) } else { None }; @@ -1476,7 +1476,7 @@ impl<'de> Visitor<'de> for SessionMessageSummaryVisitor { Ok(SessionMessageSummary { role, content_starts_with_system_reminder, - content_raw, + content_search, content_text, display_role, token_usage, @@ -1779,7 +1779,11 @@ fn parse_jcode_session_info( &title, session.working_dir.as_deref(), session.save_label.as_deref(), - &session.messages.search_text, + &{ + let mut text = String::new(); + session.messages.search_text.render(&mut text); + text + }, ); Some(SessionInfo { @@ -2867,9 +2871,11 @@ fn include_cloud_sessions( } session.server_name = Some(format!("cloud: {host}")); session.server_icon = Some("☁".to_string()); + // Host metadata, not transcript text from the preceding section. + push_mark(&mut session.search_index, Source::Other); session .search_index - .push_str(&format!(" cloud {}", host.to_lowercase())); + .push_str(&format!("cloud {}", host.to_lowercase())); } } } diff --git a/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs b/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs index 1d854537c3d..2b7d8d0ec4d 100644 --- a/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs +++ b/crates/jcode-tui/src/tui/session_picker/loading_search_text.rs @@ -104,12 +104,19 @@ pub(crate) fn build_search_index( } let mut budget = SEARCH_CONTENT_BUDGET_BYTES; + let mut current = None; for msg in messages_preview { let content = msg.content.trim(); if content.is_empty() { continue; } - combined.push(' '); + let source = message_role_source(&msg.role); + if current != Some(source) { + push_mark(&mut combined, source); + current = Some(source); + } else { + combined.push(' '); + } push_with_byte_budget(&mut combined, content, &mut budget); if budget == 0 { break; @@ -168,3 +175,35 @@ pub(crate) fn raw_value_display_text(raw: &RawValue) -> Option { let text = text.trim(); (!text.is_empty()).then(|| text.to_string()) } + +/// Per-source samples of one stored message's content. Content blocks are +/// classified by type, so tool results and reasoning inside a user or +/// assistant message are not indexed as that role's own words. +pub(crate) fn classified_search_excerpt( + raw: &RawValue, + role: &crate::message::Role, + budget: usize, +) -> Option { + use serde::de::DeserializeSeed; + if budget == 0 { + return None; + } + let source = match role { + crate::message::Role::User => Source::User, + crate::message::Role::Assistant => Source::Assistant, + }; + let mut buckets = SourceBuckets::default(); + let mut sink = BucketSink { + buckets: &mut buckets, + total: budget.min(MESSAGE_SEARCH_EXCERPT_BYTES), + }; + let walk = Walk { + sink: &mut sink, + source, + }; + let mut de = serde_json::Deserializer::from_str(raw.get()); + if let Err(error) = walk.deserialize(&mut de) { + crate::logging::debug(&format!("Invalid transcript search content: {error}")); + } + (!buckets.is_empty()).then_some(buckets) +} diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs index fd1e02e646d..68d761e68d6 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs @@ -19,7 +19,7 @@ use std::time::Instant; mod text_cache; mod word_scores; use word_scores::WordScores; -mod source; +pub(super) mod source; mod token_cache; mod warmup; use source::Source; diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs index 85e7bbfd986..ebfe00b64c7 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/source.rs @@ -51,10 +51,79 @@ pub(crate) fn stronger(a: Option, b: Option) -> Option { } } +/// Starts a section of the in-memory search index. The marker is a separate +/// whitespace-delimited word, so it never joins a query word, and a control +/// character, so a typed query never matches it. +const MARK: char = '\u{1f}'; + +impl Source { + const ALL: [Source; 6] = [ + Source::ToolOutput, + Source::Thinking, + Source::ToolInput, + Source::Other, + Source::Assistant, + Source::User, + ]; + + fn tag(self) -> char { + match self { + Source::ToolOutput => '0', + Source::Thinking => '1', + Source::ToolInput => '2', + Source::Other => '3', + Source::Assistant => '4', + Source::User => '5', + } + } + + fn from_tag(tag: char) -> Option { + Self::ALL.into_iter().find(|source| source.tag() == tag) + } + + /// Index of this source in [`SourceBuckets`]. + fn slot(self) -> usize { + self.tag() as usize - '0' as usize + } +} + +/// Marks where the following search index text came from. +pub(crate) fn push_mark(index: &mut String, source: Source) { + index.push(' '); + index.push(MARK); + index.push(source.tag()); + index.push(' '); +} + +/// Origin of the index text at byte `pos`. Unmarked text, including indexes +/// built before sections existed, is treated as untagged metadata. +fn source_at(index: &str, pos: usize) -> Source { + index[..pos] + .rfind(MARK) + .and_then(|at| index[at + MARK.len_utf8()..].chars().next()) + .and_then(Source::from_tag) + .unwrap_or(Source::Other) +} + +/// Bounds per-token work on the keystroke path for very common words. +const MAX_CLASSIFIED_MATCHES: usize = 64; + +/// Best origin among a token's occurrences in a sectioned search index. +fn best_index_source(index: &str, token: &str) -> Option { + let mut best = None; + for (pos, _) in index.match_indices(token).take(MAX_CLASSIFIED_MATCHES) { + best = stronger(best, Some(source_at(index, pos))); + if best == Some(Source::User) { + break; + } + } + best +} + /// Cheap per-token estimate from in-memory metadata, never transcript IO. /// Title, saved label, and opening prompt are user intent. Other index hits -/// came from an unclassified transcript sample, so they get the lowest weight -/// and can only be upgraded by a transcript classification pass. +/// use the section they were indexed under, and a transcript classification +/// pass can still upgrade them. pub(crate) fn metadata_sources( intent_fields: &[&str], index: &str, @@ -68,15 +137,82 @@ pub(crate) fn metadata_sources( .any(|field| field.contains(token.as_str())) { Some(Source::User) - } else if index.contains(token.as_str()) { - Some(Source::ToolOutput) } else { - None + best_index_source(index, token) } }) .collect() } +/// Bounded per-source transcript samples. Sampling each source separately +/// keeps a flood of tool output from evicting the user's prompts. +#[derive(Clone, Debug, Default)] +pub(crate) struct SourceBuckets { + text: [String; 6], +} + +impl SourceBuckets { + /// Byte budget for one source, out of `total` for all of them. User prompts + /// and replies get most of it since they are the strongest evidence. + fn budget(source: Source, total: usize) -> usize { + match source { + Source::User => total * 3 / 8, + Source::Assistant => total / 4, + _ => total / 16, + } + } + + pub(crate) fn push(&mut self, source: Source, text: &str, total: usize) { + crate::tui::session_picker::loading::push_sampled_search_text( + &mut self.text[source.slot()], + text, + Self::budget(source, total), + ); + } + + pub(crate) fn merge(&mut self, other: &Self, total: usize) { + for source in Source::ALL { + self.push(source, &other.text[source.slot()], total); + } + } + + pub(crate) fn is_empty(&self) -> bool { + self.text.iter().all(String::is_empty) + } + + /// Appends every non-empty sample under its section marker, strongest first. + pub(crate) fn render(&self, index: &mut String) { + for source in Source::ALL.into_iter().rev() { + let text = self.text[source.slot()].trim(); + if !text.is_empty() { + push_mark(index, source); + index.push_str(text); + } + } + } +} + +/// Collects decoded transcript strings into per-source samples. +pub(crate) struct BucketSink<'a> { + pub(crate) buckets: &'a mut SourceBuckets, + pub(crate) total: usize, +} + +impl Sink for BucketSink<'_> { + fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str> { + let text = text.trim(); + if !text.is_empty() { + self.buckets.push(source, text, self.total); + } + Ok(()) + } +} + +/// Source of a preview or stored message role, such as `user` or `tool`. +pub(crate) fn message_role_source(role: &str) -> Source { + role_source(role).unwrap_or(Source::Other) +} + pub(crate) trait Sink { /// Receives each decoded string. An error stops traversal. fn text(&mut self, text: &str, source: Source) -> Result<(), &'static str>; @@ -382,10 +518,57 @@ mod tests { ); let tokens = vec!["deploy".to_owned(), "api".to_owned(), "zzz".to_owned()]; let sources = metadata_sources(&["deploy the thing"], "deploy api", &tokens); + assert_eq!(sources, [Some(Source::User), Some(Source::Other), None]); + assert_eq!(source_weight(&sources), 10); + } + + #[test] + fn sectioned_index_reports_strongest_section_per_token() { + let mut index = String::from("title words"); + push_mark(&mut index, Source::ToolOutput); + index.push_str("grep zebra kiwi"); + push_mark(&mut index, Source::User); + index.push_str("find the kiwi"); + push_mark(&mut index, Source::Thinking); + index.push_str("pondering mango"); + let tokens: Vec = ["zebra", "kiwi", "mango", "title"].map(String::from).into(); + assert_eq!( + metadata_sources(&[], &index, &tokens), + [ + Some(Source::ToolOutput), + Some(Source::User), + Some(Source::Thinking), + Some(Source::Other), + ] + ); + } + + #[test] + fn buckets_keep_user_prompts_when_tool_output_floods_the_budget() { + let mut buckets = SourceBuckets::default(); + let mut sink = BucketSink { + buckets: &mut buckets, + total: 1024, + }; + Walk { + sink: &mut sink, + source: Source::User, + } + .deserialize(&mut serde_json::Deserializer::from_str( + r#"[{"type":"text","text":"ORIGINAL ASK"}]"#, + )) + .unwrap(); + let flood = "x".repeat(4096); + for _ in 0..8 { + buckets.push(Source::ToolOutput, &flood, 1024); + } + let mut index = String::new(); + buckets.render(&mut index); + let tokens = vec!["original".to_owned(), "ask".to_owned()]; + assert!(index.len() < 1024); assert_eq!( - sources, - [Some(Source::User), Some(Source::ToolOutput), None] + metadata_sources(&[], &index.to_lowercase(), &tokens), + [Some(Source::User), Some(Source::User)] ); - assert_eq!(source_weight(&sources), 9); } } From f4dd8a7e815559d2742e53fb155eff3e40e5cbdc Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:42:11 -0800 Subject: [PATCH 089/236] Draw the /resume preview with the real chat renderer Add an isolated secondary-render mode to ui::draw so a read-only SessionView of the selected session renders through the same header, transcript, todo band, composer, status line and info widgets as the opened chat, without touching the live session's published layout, copy, scroll or frame state. Search matches are highlighted on the drawn buffer. --- crates/jcode-tui/src/tui/info_widget.rs | 10 +- crates/jcode-tui/src/tui/mod.rs | 5 + crates/jcode-tui/src/tui/session_picker.rs | 384 ++++---------- .../tui/session_picker/performance_tests.rs | 48 +- .../src/tui/session_picker/preview_chrome.rs | 5 +- .../src/tui/session_picker/session_view.rs | 474 ++++++++++++++++++ .../jcode-tui/src/tui/session_picker_tests.rs | 37 +- .../tui/session_picker_tests/resume_canvas.rs | 19 +- crates/jcode-tui/src/tui/ui.rs | 161 +++++- crates/jcode-tui/src/tui/ui_animations.rs | 4 +- crates/jcode-tui/src/tui/ui_frame_metrics.rs | 3 + crates/jcode-tui/src/tui/ui_input.rs | 10 +- .../src/tui/ui_panel_image_preview.rs | 3 + crates/jcode-tui/src/tui/ui_prepare.rs | 7 + crates/jcode-tui/src/tui/ui_viewport.rs | 3 + 15 files changed, 806 insertions(+), 367 deletions(-) create mode 100644 crates/jcode-tui/src/tui/session_picker/session_view.rs diff --git a/crates/jcode-tui/src/tui/info_widget.rs b/crates/jcode-tui/src/tui/info_widget.rs index 7e7c635f833..037fcecbb06 100644 --- a/crates/jcode-tui/src/tui/info_widget.rs +++ b/crates/jcode-tui/src/tui/info_widget.rs @@ -1020,9 +1020,17 @@ impl Default for WidgetsState { /// Global widget state (for polling across frames) static WIDGETS_STATE: Mutex> = Mutex::new(None); +/// Separate widget state for secondary renders (resume preview) so their +/// placements and animations never disturb the live session's widgets. +static SECONDARY_WIDGETS_STATE: Mutex> = Mutex::new(None); fn get_or_init_state() -> std::sync::MutexGuard<'static, Option> { - let mut guard = WIDGETS_STATE.lock().unwrap_or_else(|e| e.into_inner()); + let state = if super::ui::secondary_render_active() { + &SECONDARY_WIDGETS_STATE + } else { + &WIDGETS_STATE + }; + let mut guard = state.lock().unwrap_or_else(|e| e.into_inner()); if guard.is_none() { *guard = Some(WidgetsState::default()); } diff --git a/crates/jcode-tui/src/tui/mod.rs b/crates/jcode-tui/src/tui/mod.rs index 815cf4f7805..65d320d6f47 100644 --- a/crates/jcode-tui/src/tui/mod.rs +++ b/crates/jcode-tui/src/tui/mod.rs @@ -1170,6 +1170,11 @@ pub trait TuiState { fn git_branch(&self) -> Option { None } + /// Remote machine this session runs on (SSH host or cloud VM), shown on + /// the status line. Embedded views override it with their own host. + fn remote_machine(&self) -> Option { + ssh_remote_host() + } /// Monotonic clock for viewport animations fn now_millis(&self) -> u64; /// UI state for live copy badge highlighting / feedback diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index a7cc583541b..a4d0511d3c2 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -56,6 +56,8 @@ mod preview_refresh; mod progressive_preview; mod render; mod saved_placement; +mod session_view; +use session_view::highlight_buffer_matches; mod transcript_search; #[cfg(test)] @@ -245,6 +247,8 @@ struct PreviewCacheKey { /// materializes the visible window. struct PreviewRenderCache { key: PreviewCacheKey, + /// Transcript handed to the real chat renderer. + messages: Vec, /// Wrapped lines at the final (post-scrollbar-decision) width. wrapped_lines: Vec>, image_regions: Vec, @@ -1642,12 +1646,6 @@ impl SessionPicker { return; } - let inner = - preview_chrome::draw(frame, inner, session, self.preview_extras.get(&session.id)); - let todos_payload = self - .preview_extras - .get(&session.id) - .and_then(|extras| extras.todos_payload.clone()); let preview_still_loading = session.messages_preview.is_empty() && self .pending_preview_load @@ -1656,11 +1654,6 @@ impl SessionPicker { let Some(content_hash) = self.selected_preview_content_hash(session_ref, diff_mode) else { return; }; - - // Build (or reuse) the fully-wrapped preview content. The expensive - // markdown render + wrap only runs on a cache miss; scrolling, focus - // changes, and idle redraws reuse the cached wrapped lines. This mirrors - // the main chat viewport, whose prepared frame is cached the same way. let key = PreviewCacheKey { content_hash, inner_width: inner.width, @@ -1674,181 +1667,36 @@ impl SessionPicker { let Some(session) = self.session_by_ref(session_ref) else { return; }; - // Shared markdown renderers also center structured blocks via a - // thread-local setting. Scope it so lists/code/tools stay flush - // left here without changing the chat rendered behind the picker. - let rebuilt = markdown::with_center_code_blocks(false, || { - self.build_preview_cache(session, area, inner, key, align, diff_mode) - }); + let rebuilt = self.build_preview_cache(session, area, inner, key, align, diff_mode); self.install_preview(rebuilt); } - // Read cache geometry through a short-lived borrow so the scroll-offset - // clamp below can take `&mut self` without conflict. - let (show_scrollbar, total_lines, first_match_line) = { - let Some(cache) = self.preview_cache.as_ref() else { - return; - }; - ( - cache.show_scrollbar, - cache.wrapped_lines.len(), - cache.first_match_line, - ) - }; - let (text_area, scrollbar_area) = - super::ui::split_native_scrollbar_area(inner, show_scrollbar); - let inset = super::ui::left_aligned_content_inset(text_area.width, false); - let text_area = Rect { - x: text_area.x + inset, - width: text_area.width.saturating_sub(inset), - ..text_area - }; - // The picker is an overlay, not a narrower conversation viewport. - // Keep prompt and todo wrapping identical to the opened session. - let band_width = text_area.width; - - // Pinned todo band, rendered beneath the sticky previous-prompt header - // exactly like the live chat viewport (`display.pin_todos`). - let (todo_band, _) = super::ui::viewport::pinned_todo_band_lines_for_payload( - todos_payload.as_deref(), - false, - diff_mode, - band_width, - text_area.height, - ); - let todo_lines = todo_band.len() as u16; - - // Max scroll accounts for the rows the top bands reserve, iterating - // like the live viewport since the header height depends on scroll. - let mut max_scroll = - total_lines.saturating_sub(text_area.height.saturating_sub(todo_lines) as usize); - for _ in 0..4 { - let header = self - .preview_prompt_header_lines(max_scroll, band_width, align) - .len() as u16; - let top_band = (header + todo_lines).min(text_area.height); - let body = Rect { - y: text_area.y + top_band, - height: text_area.height - top_band, - ..text_area - }; - let adjusted = total_lines.saturating_sub(body.height as usize); - if adjusted == max_scroll { - break; - } - max_scroll = adjusted; - } - let max_scroll = max_scroll.min(u16::MAX as usize) as u16; - self.preview_max_scroll = max_scroll; - // The transcript for the selected session may still be loading on a - // background thread, in which case this frame only shows a "Loading…" - // placeholder (max_scroll == 0). Keep the auto-scroll armed until the - // real content lands; otherwise the flag is consumed on the placeholder - // frame and the populated transcript stays pinned at the top, hiding the - // sticky "previous prompt" header that should appear once we snap to the - // bottom. - if self.auto_scroll_preview { - // When a search is active and the selected session has a match in the - // preview body, scroll the first hit into view (a few lines of lead-in - // context) instead of jumping to the bottom of the transcript. - self.scroll_offset = match first_match_line { - Some(line) if !self.search_query.trim().is_empty() => { - (line.saturating_sub(2) as u16).min(max_scroll) - } - _ => max_scroll, - }; - if !preview_still_loading { - self.auto_scroll_preview = false; + let tokens = self.active_highlight_tokens(); + let mut scroll = if self.auto_scroll_preview { + match self.preview_cache.as_ref().and_then(|c| c.first_match_line) { + Some(line) if !tokens.is_empty() => Some(line.saturating_sub(2)), + _ => None, } } else { - self.scroll_offset = self.scroll_offset.min(max_scroll); - } - let scroll = self.scroll_offset as usize; - - let header_lines = self.preview_prompt_header_lines(scroll, band_width, align); - let header_height = (header_lines.len() as u16).min(text_area.height); - let top_band = (header_height + todo_lines).min(text_area.height); - let content_area = Rect { - x: text_area.x, - y: text_area.y + top_band, - width: text_area.width, - height: text_area.height - top_band, - }; - let visible_height = content_area.height as usize; - - // Materialize only the visible window of wrapped lines instead of cloning - // and `.scroll()`ing the whole preview every frame (the main chat - // viewport uses the same visible-slice strategy). This makes a scroll - // tick O(viewport height) rather than O(total wrapped lines). - let visible_end = (scroll + visible_height).min(total_lines); - let (visible_lines, source_rows): (Vec>, Vec) = { - let Some(cache) = self.preview_cache.as_ref() else { - return; - }; - if scroll < visible_end { - ( - cache.wrapped_lines[scroll..visible_end].to_vec(), - (scroll..visible_end).collect(), - ) - } else { - (Vec::new(), Vec::new()) - } + Some(self.scroll_offset as usize) }; - frame.render_widget(Paragraph::new(visible_lines.clone()), content_area); - if let Some(cache) = self.preview_cache.as_ref() { - preview_chrome::draw_images( - frame, - content_area, - &cache.image_regions, - &source_rows, - None, - ); + let mut geometry = self.draw_selected_view(frame, inner, session_ref, scroll); + // The first match is only known once the renderer prepared the frame. + if self.auto_scroll_preview + && scroll.is_none() + && !tokens.is_empty() + && let Some(line) = self.preview_cache.as_ref().and_then(|c| c.first_match_line) + { + scroll = Some(line.saturating_sub(2)); + geometry = self.draw_selected_view(frame, inner, session_ref, scroll); } - if let (Some(session), Some(cache)) = ( - self.session_by_ref(session_ref), - self.preview_cache.as_ref(), - ) { - preview_chrome::draw_widgets( - frame, - content_area, - &visible_lines, - session, - self.preview_extras.get(&session.id), - &cache.image_regions, - scroll, - ); + if !tokens.is_empty() { + highlight_buffer_matches(frame.buffer_mut(), inner, &tokens); } - - if todo_lines > 0 && header_height < text_area.height { - let band_area = Rect { - x: text_area.x, - y: text_area.y + header_height, - width: band_width, - height: todo_lines.min(text_area.height - header_height), - }; - frame.render_widget(Clear, band_area); - frame.render_widget(Paragraph::new(todo_band), band_area); - } - if header_height > 0 { - let header_area = Rect { - x: text_area.x, - y: text_area.y, - width: band_width.saturating_sub(1), - height: header_height, - }; - frame.render_widget(Clear, header_area); - frame.render_widget(Paragraph::new(header_lines), header_area); - } - - if let Some(scrollbar_area) = scrollbar_area { - super::ui::render_native_scrollbar( - frame, - scrollbar_area, - scroll, - total_lines, - visible_height, - self.focus == PaneFocus::Preview, - ); + self.preview_max_scroll = geometry.max_scroll.min(u16::MAX as usize) as u16; + self.scroll_offset = geometry.scroll.min(u16::MAX as usize) as u16; + if self.auto_scroll_preview && !preview_still_loading { + self.auto_scroll_preview = false; } self.latency_transcript_rendered = !preview_still_loading && !self.pending_preview_load.as_ref().is_some_and(|pending| { @@ -1859,6 +1707,70 @@ impl SessionPicker { .contains(&self.latency_selection_id().unwrap_or_default()); } + /// Draw the selected session through the real chat renderer. + fn draw_selected_view( + &mut self, + frame: &mut Frame, + area: Rect, + session_ref: SessionRef, + scroll: Option, + ) -> super::ui::SecondaryGeometry { + let Some(session) = self.session_by_ref(session_ref) else { + return Default::default(); + }; + let Some(cache) = self.preview_cache.as_ref() else { + return Default::default(); + }; + let extras = self.preview_extras.get(&session.id); + let host = (session.server_icon.as_deref() == Some("☁")) + .then(|| session.server_name.as_deref()?.strip_prefix("cloud: ")) + .flatten() + .map(str::to_string); + let view = session_view::SessionView { + messages: &cache.messages, + version: session_view::preview_messages_version(cache.key.content_hash), + hidden_prompts: extras.map_or(0, |extras| extras.hidden_prompts), + info: preview_chrome::selected_info(session, extras), + todos_payload: extras.and_then(|extras| extras.todos_payload.as_deref()), + session_name: Some(session.short_name.clone()), + model: session.model.clone(), + provider: session.provider_key.clone(), + server_name: session.server_name.clone(), + server_icon: session.server_icon.clone(), + is_canary: session.is_canary, + working_dir: session.working_dir.clone(), + local_auth: host.is_none(), + machine: host, + scroll, + live: None, + now_ms: std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as u64), + }; + let (geometry, prepared) = session_view::draw_session_view(frame, area, &view); + if let (Some(prepared), Some(cache)) = (prepared, self.preview_cache.as_mut()) + && cache.wrapped_lines.is_empty() + { + cache.wrapped_lines = prepared.materialize_all_lines(); + cache.image_regions = prepared.image_regions.clone(); + cache.show_scrollbar = geometry.max_scroll > 0; + cache.user_prompt_markers = prepared + .wrapped_user_prompt_starts + .iter() + .zip(prepared.user_prompt_texts.iter()) + .enumerate() + .map(|(idx, (&line, text))| (line, idx + 1, text.clone())) + .collect(); + let tokens = loading::search_query_tokens(&cache.key.search_query); + cache.first_match_line = cache.wrapped_lines.iter().position(|line| { + let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); + let lower = text.to_lowercase(); + tokens.iter().any(|token| lower.contains(token.as_str())) + }); + } + geometry + } + /// Fingerprint everything that affects the *content* of the preview pane so /// the wrapped-line cache can be reused across frames (notably while /// scrolling). Mirrors the main chat viewport's prepared-frame cache key. @@ -1921,100 +1833,14 @@ impl SessionPicker { fn build_preview_cache( &self, session: &SessionInfo, - area: Rect, - inner: Rect, + _area: Rect, + _inner: Rect, key: PreviewCacheKey, _align: Alignment, - diff_mode: crate::config::DiffDisplayMode, + _diff_mode: crate::config::DiffDisplayMode, ) -> PreviewRenderCache { - let hidden_prompts = self - .preview_extras - .get(&session.id) - .map_or(0, |extras| extras.hidden_prompts); - let messages: Vec = session - .messages_preview - .iter() - .map(|msg| DisplayMessage { - role: msg.role.clone(), - content: msg.content.clone(), - tool_calls: msg.tool_calls.clone(), - duration_secs: msg.duration_secs, - title: msg.title.clone(), - tool_data: msg.tool_data.clone(), - }) - .collect(); - let wide_width = inner - .width - .saturating_sub(super::ui::left_aligned_content_inset(inner.width, false)); - let mut header = super::ui::header::build_preview_header(session, wide_width); - let mut padding = super::ui::prepare::initial_header_pad_top(area.height, header.len()); - let render_width = inner.width.saturating_sub(1); - let narrow_width = - render_width.saturating_sub(super::ui::left_aligned_content_inset(render_width, false)); - let header_height = padding + header.len(); - let (prepared, show_scrollbar) = if inner.width <= 1 || inner.height == 0 { - ( - super::ui::prepare::prepare_history_preview( - &messages, - hidden_prompts, - wide_width, - diff_mode, - ), - false, - ) - } else if header_height > inner.height as usize { - ( - super::ui::prepare::prepare_history_preview( - &messages, - hidden_prompts, - narrow_width, - diff_mode, - ), - true, - ) - } else { - super::ui::prepare::prepare_history_preview_for_viewport( - &messages, - hidden_prompts, - wide_width, - narrow_width, - diff_mode, - inner.height as usize - header_height, - ) - }; - if show_scrollbar { - header = super::ui::header::build_preview_header(session, narrow_width); - padding = super::ui::prepare::initial_header_pad_top(area.height, header.len()); - } - // The real startup header is a scrolling transcript section. Never - // pin a synthetic jcode/client/model banner above a long conversation. - let header_height = padding + header.len(); - let mut wrapped_lines = vec![Line::default(); padding]; - wrapped_lines.extend(header); - let body_empty = prepared.wrapped_lines.is_empty(); - wrapped_lines.extend(prepared.wrapped_lines); - let mut image_regions = prepared.image_regions; - for region in &mut image_regions { - region.abs_line_idx += header_height; - region.end_line += header_height; - } - // The live viewport consumes these marker rows as image metadata, never - // printable text. Preserve their reserved rows and draw the images below. - for region in &image_regions { - if let Some(line) = wrapped_lines.get_mut(region.abs_line_idx) { - *line = Line::default(); - } - } - let user_prompt_markers = prepared - .wrapped_user_prompt_starts - .iter() - .zip(prepared.user_prompt_texts.iter()) - .enumerate() - .map(|(idx, (&line, text))| { - (line + header_height, hidden_prompts + idx + 1, text.clone()) - }) - .collect(); - if body_empty { + let mut messages = session_view::display_messages_from_preview(&session.messages_preview); + if messages.is_empty() { let loading = self .pending_preview_load .as_ref() @@ -2026,22 +1852,16 @@ impl SessionPicker { } else { "(empty session)" }; - wrapped_lines.push(Line::styled(text, Style::default().fg(rgb(80, 80, 80)))); + messages.push(DisplayMessage::system(text)); } - // Highlight active search matches in the wrapped preview body and record - // the first wrapped line that contains a hit, so the caller can scroll it - // into view. Highlighting after wrapping keeps wrapped-line indices exact. - let highlight_tokens = self.active_highlight_tokens(); - let first_match_line = - Self::highlight_lines_in_place(&mut wrapped_lines, &highlight_tokens); - PreviewRenderCache { key, - wrapped_lines, - image_regions, - user_prompt_markers, - show_scrollbar, - first_match_line, + messages, + wrapped_lines: Vec::new(), + image_regions: Vec::new(), + user_prompt_markers: Vec::new(), + show_scrollbar: false, + first_match_line: None, } } diff --git a/crates/jcode-tui/src/tui/session_picker/performance_tests.rs b/crates/jcode-tui/src/tui/session_picker/performance_tests.rs index a8ae7868fc2..a513daa5f06 100644 --- a/crates/jcode-tui/src/tui/session_picker/performance_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/performance_tests.rs @@ -72,40 +72,6 @@ fn cold_preview_viewport_matches_full_wide_then_narrow_layout() { for width in [1, 2, 3, 20, 80, 120] { for height in [0, 1, 2, 5, 30, 200] { let area = Rect::new(0, 0, width, height); - let wide = width - .saturating_sub(super::super::ui::left_aligned_content_inset(width, false)); - let mut prepared = - super::super::ui::prepare::prepare_history_preview(&messages, 0, wide, mode); - let mut header = super::super::ui::header::build_preview_header(&item, wide); - let mut padding = - super::super::ui::prepare::initial_header_pad_top(height, header.len()); - let scrollbar = super::super::ui::native_scrollbar_visible( - width > 1, - padding + header.len() + prepared.wrapped_lines.len(), - height as usize, - ); - if scrollbar { - let render_width = width.saturating_sub(1); - let narrow = render_width.saturating_sub( - super::super::ui::left_aligned_content_inset(render_width, false), - ); - prepared = super::super::ui::prepare::prepare_history_preview( - &messages, 0, narrow, mode, - ); - header = super::super::ui::header::build_preview_header(&item, narrow); - padding = - super::super::ui::prepare::initial_header_pad_top(height, header.len()); - } - let body_empty = prepared.wrapped_lines.is_empty(); - let mut expected = vec![Line::default(); padding]; - expected.extend(header); - expected.extend(prepared.wrapped_lines); - if body_empty { - expected.push(Line::styled( - "(empty session)", - Style::default().fg(rgb(80, 80, 80)), - )); - } let actual = picker.build_preview_cache( &item, area, @@ -114,12 +80,15 @@ fn cold_preview_viewport_matches_full_wide_then_narrow_layout() { Alignment::Left, mode, ); + // Wrapping and chrome belong to the real chat renderer now. + let roles: Vec<_> = actual.messages.iter().map(|m| m.role.clone()).collect(); + let expected: Vec<_> = if messages.is_empty() { + vec!["system".to_string()] + } else { + messages.iter().map(|m| m.role.clone()).collect() + }; assert_eq!( - actual.show_scrollbar, scrollbar, - "turns={turns} width={width} height={height}" - ); - assert_eq!( - actual.wrapped_lines, expected, + roles, expected, "turns={turns} width={width} height={height}" ); } @@ -332,6 +301,7 @@ fn tiny_cache(id: u64, bytes: usize) -> PreviewRenderCache { diff_mode: crate::config::DiffDisplayMode::Off, search_query: String::new(), }, + messages: Vec::new(), wrapped_lines: vec![Line::from("x".repeat(bytes))], image_regions: Vec::new(), user_prompt_markers: Vec::new(), diff --git a/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs b/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs index 03e7d5ce136..8fbcfca18d5 100644 --- a/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs +++ b/crates/jcode-tui/src/tui/session_picker/preview_chrome.rs @@ -99,7 +99,10 @@ fn snapshot_account_info( (auth, usage) } -fn selected_info(session: &SessionInfo, extras: Option<&PreviewExtras>) -> InfoWidgetData { +pub(super) fn selected_info( + session: &SessionInfo, + extras: Option<&PreviewExtras>, +) -> InfoWidgetData { let mut data = extras .and_then(|extras| extras.info_data.clone()) .unwrap_or_else(|| InfoWidgetData { diff --git a/crates/jcode-tui/src/tui/session_picker/session_view.rs b/crates/jcode-tui/src/tui/session_picker/session_view.rs new file mode 100644 index 00000000000..c4812bf90f7 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/session_view.rs @@ -0,0 +1,474 @@ +//! A read-only [`TuiState`] for the selected resume candidate. +//! +//! The preview draws through the real chat renderer (`ui::draw`) instead of a +//! copied layout, so header, transcript, pinned todos, composer, status line, +//! and info widgets match the opened session by construction. Every fact comes +//! from the selected session's persisted snapshot (or its live feed), never +//! from the App behind the picker. +use crate::message::ToolCall; +use crate::tui::info_widget::InfoWidgetData; +use crate::tui::{DisplayMessage, ProcessingStatus, TuiState, markdown}; +use ratatui::text::Line; +use std::time::Duration; + +/// Live in-flight turn state for a running session, when a feed is attached. +#[derive(Clone, Debug, Default)] +pub(crate) struct LiveTurn { + pub streaming_text: String, + pub status: ProcessingStatus, + pub elapsed: Option, + pub tokens: (u64, u64), +} + +pub(crate) struct SessionView<'a> { + pub messages: &'a [DisplayMessage], + /// Unique per transcript content so the shared prepare caches never + /// return the live session's frame (or another preview's). + pub version: u64, + pub hidden_prompts: usize, + pub info: InfoWidgetData, + pub todos_payload: Option<&'a str>, + pub session_name: Option, + pub model: Option, + pub provider: Option, + pub server_name: Option, + pub server_icon: Option, + pub is_canary: bool, + pub working_dir: Option, + /// Cloud VM or SSH host the session belongs to. + pub machine: Option, + /// Local credentials only describe sessions that resume on this machine. + pub local_auth: bool, + /// Top-anchored scroll when paused; `None` follows the tail. + pub scroll: Option, + pub live: Option, + pub now_ms: u64, +} + +/// Distinguish preview versions from the live App's small monotonic counter. +pub(crate) fn preview_messages_version(content_hash: u64) -> u64 { + content_hash | (1 << 63) +} + +pub(crate) fn display_messages_from_preview( + preview: &[super::PreviewMessage], +) -> Vec { + preview + .iter() + .map(|msg| DisplayMessage { + role: msg.role.clone(), + content: msg.content.clone(), + tool_calls: msg.tool_calls.clone(), + duration_secs: msg.duration_secs, + title: msg.title.clone(), + tool_data: msg.tool_data.clone(), + }) + .collect() +} + +impl SessionView<'_> { + fn live_status(&self) -> ProcessingStatus { + self.live + .as_ref() + .map(|live| live.status.clone()) + .unwrap_or_default() + } +} + +impl TuiState for SessionView<'_> { + fn display_messages(&self) -> &[DisplayMessage] { + self.messages + } + fn display_user_message_count(&self) -> usize { + self.messages.iter().filter(|m| m.role == "user").count() + } + fn compacted_hidden_user_prompts(&self) -> usize { + self.hidden_prompts + } + fn has_display_edit_tool_messages(&self) -> bool { + self.messages.iter().any(|message| { + message + .tool_data + .as_ref() + .is_some_and(|tool| crate::tui::ui::tools_ui::is_edit_tool_name(&tool.name)) + }) + } + fn pinned_todos_payload(&self) -> Option<&str> { + self.todos_payload + } + fn side_pane_images(&self) -> Vec { + Vec::new() + } + fn display_messages_version(&self) -> u64 { + self.version + } + fn streaming_text(&self) -> &str { + self.live + .as_ref() + .map_or("", |live| live.streaming_text.as_str()) + } + fn input(&self) -> &str { + "" + } + fn cursor_pos(&self) -> usize { + 0 + } + fn is_processing(&self) -> bool { + !matches!(self.live_status(), ProcessingStatus::Idle) + } + fn queued_messages(&self) -> &[String] { + &[] + } + fn interleave_message(&self) -> Option<&str> { + None + } + fn pending_soft_interrupts(&self) -> &[String] { + &[] + } + fn scroll_offset(&self) -> usize { + self.scroll.unwrap_or(0) + } + fn auto_scroll_paused(&self) -> bool { + self.scroll.is_some() + } + fn provider_name(&self) -> String { + self.provider.clone().unwrap_or_default() + } + fn provider_model(&self) -> String { + self.model.clone().unwrap_or_default() + } + fn upstream_provider(&self) -> Option { + None + } + fn connection_type(&self) -> Option { + None + } + fn status_detail(&self) -> Option { + None + } + fn mcp_servers(&self) -> Vec<(String, usize)> { + Vec::new() + } + fn available_skills(&self) -> Vec { + Vec::new() + } + fn streaming_tokens(&self) -> (u64, u64) { + self.live.as_ref().map_or((0, 0), |live| live.tokens) + } + fn streaming_cache_tokens(&self) -> (Option, Option) { + (None, None) + } + fn output_tps(&self) -> Option { + None + } + fn streaming_tool_calls(&self) -> Vec { + Vec::new() + } + fn elapsed(&self) -> Option { + self.live.as_ref().and_then(|live| live.elapsed) + } + fn status(&self) -> ProcessingStatus { + self.live_status() + } + fn command_suggestions(&self) -> Vec<(String, &'static str)> { + Vec::new() + } + fn active_skill(&self) -> Option { + None + } + fn subagent_status(&self) -> Option { + None + } + fn batch_progress(&self) -> Option { + None + } + fn time_since_activity(&self) -> Option { + None + } + fn total_session_tokens(&self) -> Option<(u64, u64)> { + None + } + fn is_remote_mode(&self) -> bool { + false + } + fn is_canary(&self) -> bool { + self.is_canary + } + fn is_replay(&self) -> bool { + false + } + fn diff_mode(&self) -> crate::config::DiffDisplayMode { + crate::config::config().display.diff_mode + } + fn current_session_id(&self) -> Option { + None + } + fn session_display_name(&self) -> Option { + self.session_name.clone() + } + fn server_display_name(&self) -> Option { + self.server_name.clone() + } + fn server_display_icon(&self) -> Option { + self.server_icon.clone() + } + fn server_sessions(&self) -> Vec { + Vec::new() + } + fn connected_clients(&self) -> Option { + None + } + fn status_notice(&self) -> Option { + None + } + fn remote_startup_phase_active(&self) -> bool { + false + } + fn dictation_key_label(&self) -> Option { + None + } + fn animation_elapsed(&self) -> f32 { + self.now_ms as f32 / 1000.0 + } + fn rate_limit_remaining(&self) -> Option { + None + } + fn queue_mode(&self) -> bool { + false + } + fn has_stashed_input(&self) -> bool { + false + } + fn context_info(&self) -> crate::prompt::ContextInfo { + Default::default() + } + fn context_limit(&self) -> Option { + self.info.context_limit + } + fn client_update_available(&self) -> bool { + false + } + fn server_update_available(&self) -> Option { + None + } + fn info_widget_data(&self) -> InfoWidgetData { + self.info.clone() + } + fn render_streaming_markdown(&self, width: usize) -> Vec> { + markdown::render_markdown_with_width(self.streaming_text(), Some(width)) + } + fn centered_mode(&self) -> bool { + // The opened session uses the user's alignment preference. + crate::config::config().display.centered + } + fn auth_status(&self) -> crate::auth::AuthStatus { + if self.local_auth && !crate::tui::is_ssh_remote() { + crate::auth::AuthStatus::check_fast_nonblocking() + } else { + Default::default() + } + } + fn update_cost(&mut self) {} + fn diagram_mode(&self) -> crate::config::DiagramDisplayMode { + crate::config::DiagramDisplayMode::None + } + fn diagram_focus(&self) -> bool { + false + } + fn diagram_index(&self) -> usize { + 0 + } + fn diagram_scroll(&self) -> (i32, i32) { + (0, 0) + } + fn diagram_pane_ratio(&self) -> u8 { + 50 + } + fn diagram_pane_ratio_user_adjusted(&self) -> bool { + false + } + fn diagram_pane_animating(&self) -> bool { + false + } + fn diagram_pane_enabled(&self) -> bool { + false + } + fn diagram_pane_position(&self) -> crate::config::DiagramPanePosition { + Default::default() + } + fn diagram_zoom(&self) -> u8 { + 100 + } + fn diff_pane_scroll(&self) -> usize { + 0 + } + fn diff_pane_scroll_x(&self) -> i32 { + 0 + } + fn side_panel_image_zoom_percent(&self) -> u8 { + 100 + } + fn diff_pane_focus(&self) -> bool { + false + } + fn side_panel(&self) -> &crate::side_panel::SidePanelSnapshot { + static EMPTY: std::sync::LazyLock = + std::sync::LazyLock::new(crate::side_panel::SidePanelSnapshot::default); + &EMPTY + } + fn pin_images(&self) -> bool { + false + } + fn chat_native_scrollbar(&self) -> bool { + crate::config::config().display.native_scrollbars.chat + } + fn side_panel_native_scrollbar(&self) -> bool { + false + } + fn inline_interactive_state(&self) -> Option<&crate::tui::InlineInteractiveState> { + None + } + fn changelog_scroll(&self) -> Option { + None + } + fn help_scroll(&self) -> Option { + None + } + fn session_picker_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn login_picker_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn account_picker_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn usage_overlay( + &self, + ) -> Option<&std::cell::RefCell> { + None + } + fn working_dir(&self) -> Option { + self.working_dir.clone() + } + fn remote_machine(&self) -> Option { + self.machine.clone() + } + fn now_millis(&self) -> u64 { + self.now_ms + } + fn copy_badge_ui(&self) -> crate::tui::CopyBadgeUiState { + Default::default() + } + fn copy_selection_mode(&self) -> bool { + false + } + fn copy_selection_range(&self) -> Option { + None + } + fn copy_selection_status(&self) -> Option { + None + } + fn suggestion_prompts(&self) -> Vec<(String, String)> { + Vec::new() + } + fn cache_ttl_status(&self) -> Option { + None + } +} + +/// Render `view` with the real chat renderer into `area` of `frame`, isolated +/// from the live session's published state. Returns the resolved geometry and +/// the prepared transcript it drew. +pub(crate) fn draw_session_view( + frame: &mut ratatui::Frame, + area: ratatui::layout::Rect, + view: &SessionView<'_>, +) -> ( + crate::tui::ui::SecondaryGeometry, + Option>, +) { + use ratatui::{Terminal, backend::TestBackend}; + if area.width == 0 || area.height == 0 { + return (Default::default(), None); + } + let center = crate::tui::markdown::center_code_blocks(); + let (rendered, geometry) = crate::tui::markdown::with_center_code_blocks(center, || { + crate::tui::ui::with_secondary_render(|| { + jcode_tui_mermaid::with_image_frame_offset(area.x, area.y, || { + let mut terminal = Terminal::new(TestBackend::new(area.width, area.height)).ok()?; + let completed = terminal + .draw(|inner| crate::tui::ui::draw(inner, view)) + .ok()?; + Some(completed.buffer.clone()) + }) + }) + }); + let prepared = crate::tui::ui::take_secondary_chat_frame(); + if let Some(buf) = rendered { + let dst = frame.buffer_mut(); + for y in 0..area.height.min(buf.area.height) { + for x in 0..area.width.min(buf.area.width) { + if let (Some(src), Some(cell)) = + (buf.cell((x, y)), dst.cell_mut((area.x + x, area.y + y))) + { + *cell = src.clone(); + } + } + } + } + (geometry, prepared) +} + +/// Restyle visible search matches in the rendered preview. Operating on the +/// drawn buffer keeps highlighting independent of how the renderer wrapped. +pub(crate) fn highlight_buffer_matches( + buf: &mut ratatui::buffer::Buffer, + area: ratatui::layout::Rect, + tokens: &[String], +) { + use ratatui::style::{Color, Modifier}; + for y in area.top()..area.bottom() { + let mut cells: Vec<(u16, char)> = Vec::new(); + for x in area.left()..area.right() { + if let Some(cell) = buf.cell((x, y)) { + let mut chars = cell.symbol().chars(); + if let (Some(ch), None) = (chars.next(), chars.next()) { + cells.push((x, ch.to_lowercase().next().unwrap_or(ch))); + } else { + cells.push((x, '\0')); + } + } + } + for token in tokens { + let needle: Vec = token.chars().collect(); + if needle.is_empty() || needle.len() > cells.len() { + continue; + } + let mut i = 0; + while i + needle.len() <= cells.len() { + if cells[i..i + needle.len()] + .iter() + .zip(&needle) + .all(|((_, c), n)| c == n) + { + for (x, _) in &cells[i..i + needle.len()] { + if let Some(cell) = buf.cell_mut((*x, y)) { + cell.set_fg(Color::Rgb(255, 214, 90)); + cell.modifier.insert(Modifier::BOLD); + } + } + i += needle.len(); + } else { + i += 1; + } + } + } + } +} diff --git a/crates/jcode-tui/src/tui/session_picker_tests.rs b/crates/jcode-tui/src/tui/session_picker_tests.rs index 2b7707276c7..0ee387140e3 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests.rs @@ -1344,6 +1344,15 @@ fn make_session_with_many_turns(id: &str, turns: usize) -> SessionInfo { session } +fn picker_buffer(picker: &mut SessionPicker, w: u16, h: u16) -> ratatui::buffer::Buffer { + let backend = ratatui::backend::TestBackend::new(w, h); + let mut terminal = ratatui::Terminal::new(backend).expect("test terminal"); + terminal + .draw(|frame| picker.render(frame)) + .expect("render picker"); + terminal.backend().buffer().clone() +} + fn buffer_text(picker: &mut SessionPicker, w: u16, h: u16) -> String { let backend = ratatui::backend::TestBackend::new(w, h); let mut terminal = ratatui::Terminal::new(backend).expect("test terminal"); @@ -1719,12 +1728,7 @@ fn test_preview_transcript_omits_picker_metadata() { text.contains("client: Metadata-monkey"), "the real startup header belongs to the scrolling transcript" ); - for metadata in [ - "metadata-video-title", - "/metadata-only-directory", - "▶ Active", - "────", - ] { + for metadata in ["metadata-video-title", "▶ Active", "────"] { assert!( !text.contains(metadata), "unexpected picker metadata: {text}" @@ -2604,18 +2608,15 @@ fn search_highlights_match_in_preview_and_scrolls_to_it() { "matched term should be visible in the preview after scrolling" ); - // The match should be highlighted (bold) in the cached wrapped lines. - let highlighted = picker - .preview_cache - .as_ref() - .expect("preview cache built") - .wrapped_lines - .iter() - .flat_map(|line| line.spans.iter()) - .any(|s| { - s.content.to_lowercase().contains("flibbertigibbet") - && s.style.add_modifier.contains(Modifier::BOLD) - }); + // The match should be highlighted (bold) where the renderer drew it. + let buffer = picker_buffer(&mut picker, w, h); + let highlighted = (0..h).any(|y| { + let row: String = (0..w).map(|x| buffer[(x, y)].symbol()).collect(); + row.find("flibbertigibbet").is_some_and(|byte| { + let x = row[..byte].chars().count() as u16; + buffer[(x, y)].modifier.contains(Modifier::BOLD) + }) + }); assert!( highlighted, "matched term in preview body should be highlighted" diff --git a/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs b/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs index 5b9c923f082..7094dc9baf8 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests/resume_canvas.rs @@ -219,20 +219,13 @@ fn preview_cache_preserves_live_tool_metadata_and_invalidates_on_changes() { tool_data: msg.tool_data.clone(), }) .collect(); - let text_width = width - u16::from(cache.show_scrollbar); - let text_width = text_width.saturating_sub( - super::super::super::ui::left_aligned_content_inset(text_width, false), - ); - let live = super::super::super::ui::prepare::prepare_history_preview( - &messages, 0, text_width, mode, - ); - let header = super::super::super::ui::header::build_preview_header(&session, text_width); - let offset = header.len() - + super::super::super::ui::prepare::initial_header_pad_top(inner.height, header.len()); + assert_eq!(cache.messages.len(), messages.len(), "width={width}"); + let last = cache.messages.last().unwrap(); + assert_eq!(last.duration_secs, Some(2.3)); + assert_eq!(last.title.as_deref(), Some("Saved tool title")); assert_eq!( - cache.wrapped_lines[offset..], - live.wrapped_lines, - "width={width}" + last.tool_data.as_ref().map(|t| t.name.as_str()), + Some("bash") ); } session.messages_preview.last_mut().unwrap().duration_secs = Some(4.5); diff --git a/crates/jcode-tui/src/tui/ui.rs b/crates/jcode-tui/src/tui/ui.rs index e17080d73df..8302d40a7f0 100644 --- a/crates/jcode-tui/src/tui/ui.rs +++ b/crates/jcode-tui/src/tui/ui.rs @@ -262,7 +262,64 @@ pub fn last_max_scroll() -> usize { } } +thread_local! { + static SECONDARY_RENDER: std::cell::Cell = const { std::cell::Cell::new(false) }; + static SECONDARY_FRAME: std::cell::RefCell>> = + const { std::cell::RefCell::new(None) }; + static SECONDARY_GEOMETRY: std::cell::Cell = + const { std::cell::Cell::new(SecondaryGeometry { max_scroll: 0, scroll: 0, total_lines: 0, viewport_height: 0 }) }; +} + +/// Transcript geometry a secondary render resolved. The live session's +/// globals stay untouched, so embedders read their own results from here. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) struct SecondaryGeometry { + pub max_scroll: usize, + pub scroll: usize, + pub total_lines: usize, + pub viewport_height: usize, +} + +fn note_secondary_geometry(update: impl FnOnce(&mut SecondaryGeometry)) { + SECONDARY_GEOMETRY.with(|cell| { + let mut geometry = cell.get(); + update(&mut geometry); + cell.set(geometry); + }); +} + +/// True while drawing a non-live view (for example the resume preview). +/// Such renders must not publish layout, scroll, copy, or status state that +/// the live session's input handling reads back. +pub(crate) fn secondary_render_active() -> bool { + SECONDARY_RENDER.with(std::cell::Cell::get) +} + +/// Run `f` with secondary-render isolation enabled on this thread. +/// Returns the geometry the render resolved alongside `f`'s result. +pub(crate) fn with_secondary_render(f: impl FnOnce() -> R) -> (R, SecondaryGeometry) { + struct Reset(bool); + impl Drop for Reset { + fn drop(&mut self) { + SECONDARY_RENDER.with(|c| c.set(self.0)); + } + } + SECONDARY_GEOMETRY.with(|cell| cell.set(SecondaryGeometry::default())); + SECONDARY_FRAME.with(|slot| slot.borrow_mut().take()); + let _reset = Reset(SECONDARY_RENDER.with(|c| c.replace(true))); + let result = f(); + (result, SECONDARY_GEOMETRY.with(std::cell::Cell::get)) +} + +/// The prepared transcript the most recent secondary render drew. +pub(crate) fn take_secondary_chat_frame() -> Option> { + SECONDARY_FRAME.with(|slot| slot.borrow_mut().take()) +} + fn set_last_chat_scrollbar_visible(visible: bool) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_CHAT_SCROLLBAR_VISIBLE.with(|state| state.set(visible)); @@ -279,6 +336,9 @@ fn set_last_chat_scrollbar_visible(visible: bool) { /// instead of both the wide and narrow variants (which thrashes the prep caches /// on long transcripts during streaming). fn last_chat_scrollbar_visible() -> bool { + if secondary_render_active() { + return false; + } #[cfg(test)] { return TEST_LAST_CHAT_SCROLLBAR_VISIBLE.with(Cell::get); @@ -328,6 +388,10 @@ pub fn last_diff_pane_max_scroll() -> usize { } pub(crate) fn set_last_max_scroll(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.max_scroll = value); + return; + } #[cfg(test)] { TEST_LAST_MAX_SCROLL.with(|cell| cell.set(value)); @@ -340,6 +404,9 @@ pub(crate) fn set_last_max_scroll(value: usize) { } pub(crate) fn set_pinned_pane_total_lines(value: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_PINNED_PANE_TOTAL_LINES.with(|cell| cell.set(value)); @@ -352,6 +419,9 @@ pub(crate) fn set_pinned_pane_total_lines(value: usize) { } pub(crate) fn set_last_diff_pane_effective_scroll(value: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_DIFF_PANE_EFFECTIVE_SCROLL.with(|cell| cell.set(value)); @@ -364,6 +434,9 @@ pub(crate) fn set_last_diff_pane_effective_scroll(value: usize) { } pub(crate) fn set_last_diff_pane_max_scroll(value: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_DIFF_PANE_MAX_SCROLL.with(|cell| cell.set(value)); @@ -389,6 +462,10 @@ pub fn last_total_wrapped_lines() -> usize { } pub(crate) fn set_last_total_wrapped_lines(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.total_lines = value); + return; + } #[cfg(test)] { TEST_LAST_TOTAL_WRAPPED_LINES.with(|cell| cell.set(value)); @@ -414,6 +491,10 @@ pub(crate) fn last_chat_viewport_height() -> usize { } pub(crate) fn set_last_chat_viewport_height(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.viewport_height = value); + return; + } #[cfg(test)] { TEST_LAST_CHAT_VIEWPORT_HEIGHT.with(|cell| cell.set(value)); @@ -428,6 +509,9 @@ pub(crate) fn set_last_chat_viewport_height(value: usize) { /// The chat scroll offset the renderer actually used on the most recent frame /// (after clamping and after resolving any pending history anchor). pub fn last_resolved_chat_scroll() -> usize { + if secondary_render_active() { + return 0; + } #[cfg(test)] { return TEST_LAST_RESOLVED_CHAT_SCROLL.with(Cell::get); @@ -439,6 +523,10 @@ pub fn last_resolved_chat_scroll() -> usize { } pub(crate) fn set_last_resolved_chat_scroll(value: usize) { + if secondary_render_active() { + note_secondary_geometry(|g| g.scroll = value); + return; + } #[cfg(test)] { TEST_LAST_RESOLVED_CHAT_SCROLL.with(|cell| cell.set(value)); @@ -464,6 +552,9 @@ pub(crate) fn tail_catchup_active() -> bool { } pub(crate) fn set_tail_catchup_active(active: bool) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_TAIL_CATCHUP_ACTIVE.with(|cell| cell.set(active)); @@ -495,6 +586,9 @@ pub(crate) fn request_tail_follow_snap() { } pub(crate) fn take_tail_follow_snap_request() -> bool { + if secondary_render_active() { + return false; + } #[cfg(test)] { return TEST_TAIL_FOLLOW_SNAP_PENDING.with(|cell| cell.replace(false)); @@ -599,6 +693,9 @@ fn visible_expand_edit_badge_rect_state() -> &'static Mutex> { } pub(crate) fn set_visible_expand_edit_badge_rect(rect: Option) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_VISIBLE_EXPAND_EDIT_BADGE_RECT.with(|state| state.set(rect)); @@ -629,6 +726,9 @@ pub(crate) fn visible_expand_edit_badge_at(column: u16, row: u16) -> bool { } pub(crate) fn set_visible_expand_edit_badge(visible: bool, line: Option) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_VISIBLE_EXPAND_EDIT_BADGE.with(|state| state.set(visible)); @@ -682,6 +782,9 @@ pub(crate) fn visible_expand_edit_badge_line() -> Option { } fn set_visible_copy_targets(targets: Vec) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_VISIBLE_COPY_TARGETS.with(|state| { @@ -802,6 +905,9 @@ fn active_prompt_entry_animation(now_ms: u64) -> Option } fn record_prompt_viewport(visible_start: usize, visible_end: usize) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_PROMPT_VIEWPORT_STATE.with(|state| { @@ -832,6 +938,9 @@ fn update_prompt_entry_animation( visible_end: usize, now_ms: u64, ) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_PROMPT_VIEWPORT_STATE.with(|state| { @@ -1502,6 +1611,9 @@ fn last_status_area_state() -> &'static Mutex> { } pub(crate) fn record_status_area(area: Rect) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_STATUS_AREA.with(|snapshot| { @@ -1592,6 +1704,9 @@ pub fn record_layout_snapshot( diff_pane_area: Option, input_area: Option, ) { + if secondary_render_active() { + return; + } #[cfg(test)] { TEST_LAST_LAYOUT.with(|snapshot| { @@ -1651,6 +1766,10 @@ fn terminal_clear_start(app: &dyn TuiState, prepared: &PreparedChatFrame) -> Opt /// Record the prepared transcript frame the renderer just drew. pub(crate) fn set_last_chat_frame(frame: Arc) { + if secondary_render_active() { + SECONDARY_FRAME.with(|slot| *slot.borrow_mut() = Some(frame)); + return; + } #[cfg(test)] { TEST_LAST_CHAT_FRAME.with(|slot| *slot.borrow_mut() = Some(frame)); @@ -1974,6 +2093,9 @@ fn copy_snapshot_for_pane(pane: crate::tui::CopySelectionPane) -> Option]) { + if secondary_render_active() { + return; + } // Only retain styled lines when they contain links. The common plain-text // pane path should not duplicate an entire rendered document every frame. let lines = lines @@ -2770,7 +2901,9 @@ pub(crate) fn debug_chat_image_regions_json() -> String { mod overflow; use overflow::transcript_definitely_overflows; pub fn draw(frame: &mut Frame, app: &dyn TuiState) { - record_idle_animation_area(None); + if !secondary_render_active() { + record_idle_animation_area(None); + } if let Some(frozen) = app.hibernation_frame() && frozen.area == frame.area() { @@ -2810,7 +2943,9 @@ pub fn draw(frame: &mut Frame, app: &dyn TuiState) { capture .render_order .push("frozen_hibernation_frame".to_string()); - visual_debug::record_frame(capture.build()); + if !secondary_render_active() { + visual_debug::record_frame(capture.build()); + } } return; } @@ -2833,20 +2968,26 @@ pub fn draw(frame: &mut Frame, app: &dyn TuiState) { // Classic Kitty placements must follow all text writes. Unlike Unicode // placeholders they survive scrolling, so finalize even an image-free frame. crate::tui::mermaid::finish_terminal_image_frame(frame.buffer_mut()); - crate::tui::cloud_latency::rendered(); + if !secondary_render_active() { + crate::tui::cloud_latency::rendered(); + } } fn draw_inner(frame: &mut Frame, app: &dyn TuiState) { - panel_image_preview::clear_regions(); + let secondary = secondary_render_active(); + if !secondary { + panel_image_preview::clear_regions(); + } let area = frame.area().intersection(*frame.buffer_mut().area()); if area.width == 0 || area.height == 0 { return; } let total_start = Instant::now(); - reset_frame_perf_stats(); - begin_frame_resource_sample(); - - clear_copy_viewport_snapshot(); + if !secondary { + reset_frame_perf_stats(); + begin_frame_resource_sample(); + clear_copy_viewport_snapshot(); + } // Clear full frame to prevent stale cells from prior layouts. // This is critical on macOS terminals where ratatui's diff-based updates @@ -3857,7 +3998,9 @@ fn draw_inner(frame: &mut Frame, app: &dyn TuiState) { capture.side_panel = crate::tui::side_panel_debug_json(); capture.markdown = crate::tui::markdown::debug_stats_json(); capture.theme = overlays::debug_palette_json(); - visual_debug::record_frame(capture.build()); + if !secondary_render_active() { + visual_debug::record_frame(capture.build()); + } } finalize_frame_metrics( diff --git a/crates/jcode-tui/src/tui/ui_animations.rs b/crates/jcode-tui/src/tui/ui_animations.rs index 94fbec4b18e..a22336cabf9 100644 --- a/crates/jcode-tui/src/tui/ui_animations.rs +++ b/crates/jcode-tui/src/tui/ui_animations.rs @@ -291,7 +291,9 @@ pub(crate) fn idle_donut_reserved_height(show_donut: bool, input_height: u16) -> pub(super) fn draw_idle_animation(frame: &mut Frame, app: &dyn TuiState, area: Rect) { // Remember the animated rows so the run loop can repaint only them between // full frames. Sizes the animation rejects are recorded as "no animation". - record_idle_animation_area((area.width >= 4 && area.height >= 2).then_some(area)); + if !super::secondary_render_active() { + record_idle_animation_area((area.width >= 4 && area.height >= 2).then_some(area)); + } render_idle_animation(frame.buffer_mut(), area, app.animation_elapsed()); } diff --git a/crates/jcode-tui/src/tui/ui_frame_metrics.rs b/crates/jcode-tui/src/tui/ui_frame_metrics.rs index 7901dc0cbc1..942eb226402 100644 --- a/crates/jcode-tui/src/tui/ui_frame_metrics.rs +++ b/crates/jcode-tui/src/tui/ui_frame_metrics.rs @@ -1103,6 +1103,9 @@ pub(super) fn finalize_frame_metrics( draw_elapsed: Duration, messages_ms: Option, ) { + if super::secondary_render_active() { + return; + } if profile_enabled() { record_profile(prep_elapsed, draw_elapsed, total_start.elapsed()); } diff --git a/crates/jcode-tui/src/tui/ui_input.rs b/crates/jcode-tui/src/tui/ui_input.rs index d8562eea059..38b98fc7daf 100644 --- a/crates/jcode-tui/src/tui/ui_input.rs +++ b/crates/jcode-tui/src/tui/ui_input.rs @@ -821,7 +821,9 @@ fn live_status_snapshot(app: &dyn TuiState, elapsed: f32) -> crate::protocol::Sw pub(super) fn draw_status(frame: &mut Frame, app: &dyn TuiState, area: Rect, pending_count: usize) { if app.status_notice().as_deref() == Some(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) { - crate::tui::app::oauth_telemetry::note_status_render(area); + if !super::secondary_render_active() { + crate::tui::app::oauth_telemetry::note_status_render(area); + } frame.render_widget( Paragraph::new(crate::telemetry::OAUTH_ACCOUNT_NOTICE_TEXT) .style(Style::default().fg(accent_color())), @@ -1524,7 +1526,7 @@ pub(super) fn draw_overscroll_status(frame: &mut Frame, app: &dyn TuiState, area .filter(|p| !p.is_empty()) .unwrap_or_else(|| app.provider_name()); let facts = OverscrollFacts { - machine: crate::tui::ssh_remote_host(), + machine: app.remote_machine(), dir: overscroll_directory_transition( data.original_working_dir.as_deref(), app.working_dir().as_deref(), @@ -2383,7 +2385,9 @@ pub(super) fn draw_input( if app.input_drag_scroll_offset().is_some() { return None; } - frame.set_cursor_position(cursor); + if !super::secondary_render_active() { + frame.set_cursor_position(cursor); + } Some(cursor) } diff --git a/crates/jcode-tui/src/tui/ui_panel_image_preview.rs b/crates/jcode-tui/src/tui/ui_panel_image_preview.rs index d9a4f054c46..ba38713c115 100644 --- a/crates/jcode-tui/src/tui/ui_panel_image_preview.rs +++ b/crates/jcode-tui/src/tui/ui_panel_image_preview.rs @@ -13,6 +13,9 @@ pub(crate) fn clear_regions() { } pub(super) fn record_image(area: Rect, hash: u64) { + if super::secondary_render_active() { + return; + } if area.width > 0 && area.height > 0 { IMAGE_REGIONS.with(|regions| regions.borrow_mut().push((area, hash))); } diff --git a/crates/jcode-tui/src/tui/ui_prepare.rs b/crates/jcode-tui/src/tui/ui_prepare.rs index fca26b40d2d..d0959c6b121 100644 --- a/crates/jcode-tui/src/tui/ui_prepare.rs +++ b/crates/jcode-tui/src/tui/ui_prepare.rs @@ -867,6 +867,13 @@ struct HeaderPrepCacheState { fn header_prep_cache() -> &'static std::sync::Mutex> { static CACHE: std::sync::OnceLock>> = std::sync::OnceLock::new(); + // Single-slot cache: a secondary (preview) render must not evict the live + // session's header every frame, so it gets a slot of its own. + static SECONDARY: std::sync::OnceLock>> = + std::sync::OnceLock::new(); + if super::secondary_render_active() { + return SECONDARY.get_or_init(|| std::sync::Mutex::new(None)); + } CACHE.get_or_init(|| std::sync::Mutex::new(None)) } diff --git a/crates/jcode-tui/src/tui/ui_viewport.rs b/crates/jcode-tui/src/tui/ui_viewport.rs index 26785a513ed..74a9df10a4b 100644 --- a/crates/jcode-tui/src/tui/ui_viewport.rs +++ b/crates/jcode-tui/src/tui/ui_viewport.rs @@ -1485,6 +1485,9 @@ pub(crate) fn pinned_todo_band_lines_for_payload( static PINNED_TODO_MORE_AREA: std::sync::Mutex> = std::sync::Mutex::new(None); fn set_pinned_todo_more_area(area: Option) { + if super::secondary_render_active() { + return; + } if let Ok(mut current) = PINNED_TODO_MORE_AREA.lock() { *current = area; } From 2cfbf03f48c3a2e19be2fb5010ca6f50f19a39d8 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:42:13 -0800 Subject: [PATCH 090/236] Session picker resumes onto the active build, not its own frozen binary A picker window left open for hours resumed sessions with current_exe(), reviving old versioned builds that never get reload notices. Use the same client_update_candidate that reload uses. --- src/cli/tui_launch.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/cli/tui_launch.rs b/src/cli/tui_launch.rs index 7e866693b8f..0afe26511d1 100644 --- a/src/cli/tui_launch.rs +++ b/src/cli/tui_launch.rs @@ -7,7 +7,7 @@ use std::io::{self, Write}; use std::process::Command as ProcessCommand; use crate::{ - id, logging, replay, server, session, setup_hints, startup_profile, tui, video_export, + build, id, logging, replay, server, session, setup_hints, startup_profile, tui, video_export, }; use super::hot_exec::{execute_requested_action, has_requested_action}; @@ -591,6 +591,17 @@ fn cloud_resume_target_args( Some(args) } +/// Binary a session picker launches its selection with. A picker window can +/// sit open for hours while builds move on, and `current_exe()` is the frozen +/// versioned binary it started from. Resume onto the active channel instead, +/// the same candidate reload uses, so stale pickers do not revive old builds. +fn picker_resume_exe() -> Result { + match build::client_update_candidate(false) { + Some((exe, _)) => Ok(exe), + None => Ok(std::env::current_exe()?), + } +} + pub fn list_sessions() -> Result<()> { fn build_resume_target_command( exe: &std::path::Path, @@ -703,7 +714,7 @@ pub fn list_sessions() -> Result<()> { else { anyhow::bail!("Claude takeover did not produce a Jcode session"); }; - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let mut session_cwd = std::env::current_dir()?; if let Ok(sess) = session::Session::load(session_id) && let Some(dir) = sess.working_dir.as_deref() @@ -723,7 +734,7 @@ pub fn list_sessions() -> Result<()> { tui::session_picker::PickerResult::Selected(targets) | tui::session_picker::PickerResult::SelectedInCurrentTerminal(targets), ) => { - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let cwd = std::env::current_dir()?; if targets.len() == 1 { @@ -803,7 +814,7 @@ pub fn list_sessions() -> Result<()> { } } Some(tui::session_picker::PickerResult::SelectedInNewTerminal(targets)) => { - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let cwd = std::env::current_dir()?; let mut spawned = 0usize; let mut warned_no_terminal = false; @@ -866,7 +877,7 @@ pub fn list_sessions() -> Result<()> { recovered.len() ); - let exe = std::env::current_exe()?; + let exe = picker_resume_exe()?; let cwd = std::env::current_dir()?; let mut spawned = 0usize; let mut warned_no_terminal = false; From 89381e299bf99a30802c9b137cc91f2970116bc4 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:44:47 -0800 Subject: [PATCH 091/236] Follow running sessions live in the /resume preview Refresh a streaming session's transcript every second, keep a bottom-parked preview following the tail, and pass the turn's streaming state and elapsed time to the real status line. --- crates/jcode-tui/src/tui/session_picker.rs | 28 ++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index a4d0511d3c2..0c94ad99a6d 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -1155,13 +1155,24 @@ impl SessionPicker { return; }; + // A running turn keeps appending to its transcript. Follow it closely + // so the preview tracks the session like its own chat view would. + let refresh_after = if self + .live_presence + .get(&cache_session_id) + .is_some_and(|presence| presence.streaming) + { + Duration::from_secs(1) + } else { + Duration::from_secs(15) + }; if self .preview_extras .get(&cache_session_id) .is_some_and(|extras| { extras .loaded_at - .is_none_or(|at| at.elapsed() < Duration::from_secs(15)) + .is_none_or(|at| at.elapsed() < refresh_after) }) || self.preview_load_failures.contains(&cache_session_id) || self @@ -1677,6 +1688,9 @@ impl SessionPicker { Some(line) if !tokens.is_empty() => Some(line.saturating_sub(2)), _ => None, } + } else if self.scroll_offset >= self.preview_max_scroll { + // Parked at the bottom: keep following a transcript that grows. + None } else { Some(self.scroll_offset as usize) }; @@ -1742,7 +1756,17 @@ impl SessionPicker { local_auth: host.is_none(), machine: host, scroll, - live: None, + live: self + .live_presence + .get(&session.id) + .filter(|presence| presence.streaming) + .map(|presence| session_view::LiveTurn { + status: super::ProcessingStatus::Streaming, + elapsed: presence + .streaming_since + .and_then(|since| std::time::SystemTime::now().duration_since(since).ok()), + ..Default::default() + }), now_ms: std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_or(0, |d| d.as_millis() as u64), From 3f0c9d829404e77c90f14e0de189a9848b209246 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:46:47 -0800 Subject: [PATCH 092/236] Event-driven git widget: inotify watcher on .git and non-ignored worktree dirs replaces polling --- crates/jcode-tui/src/tui/app/helpers.rs | 31 +- .../src/tui/app/helpers/git_watch.rs | 392 ++++++++++++++++++ 2 files changed, 422 insertions(+), 1 deletion(-) create mode 100644 crates/jcode-tui/src/tui/app/helpers/git_watch.rs diff --git a/crates/jcode-tui/src/tui/app/helpers.rs b/crates/jcode-tui/src/tui/app/helpers.rs index 769c4efe12c..543d19898cf 100644 --- a/crates/jcode-tui/src/tui/app/helpers.rs +++ b/crates/jcode-tui/src/tui/app/helpers.rs @@ -2,6 +2,8 @@ mod clipboard_helper; mod git_line_cache; +#[cfg_attr(test, allow(dead_code))] +mod git_watch; pub(crate) mod model_names; use crate::todo::TodoItem; @@ -1284,6 +1286,11 @@ pub(super) fn gather_git_info() -> Option { if *refreshing { return cached.clone(); } + // The inotify watcher refreshes on change. Nothing to poll. + if git_watch::is_healthy() { + *ts = std::time::Instant::now(); + return cached.clone(); + } // Nothing in .git moved and the last full probe is recent: // extend the entry without spawning any git process. if let Some(root) = cached.as_ref().and_then(|info| info.repo_root.as_deref()) @@ -1315,7 +1322,29 @@ pub(super) fn gather_git_info() -> Option { #[cfg(not(test))] fn spawn_git_info_refresh() { std::thread::spawn(|| { + run_git_info_refresh(); + }); +} + +/// Watcher callback: probe now, on the watcher thread (it is already +/// debounced and off the render path). +#[cfg(not(test))] +fn git_watch_changed() { + if let Ok(mut guard) = GIT_INFO_CACHE.lock() + && let Some((_, _, refreshing)) = guard.as_mut() + { + *refreshing = true; + } + run_git_info_refresh(); +} + +#[cfg(not(test))] +fn run_git_info_refresh() { + { let result = gather_git_info_inner(); + if let Some(root) = result.as_ref().and_then(|info| info.repo_root.as_deref()) { + git_watch::ensure_started(root, git_watch_changed); + } if let Ok(mut stamp) = GIT_PROBE_STAMP.lock() { *stamp = result .as_ref() @@ -1335,7 +1364,7 @@ fn spawn_git_info_refresh() { if changed { crate::bus::Bus::global().publish(crate::bus::BusEvent::GitInfoRefreshed); } - }); + } } /// Start the git probe and give it up to `budget` to land before returning. diff --git a/crates/jcode-tui/src/tui/app/helpers/git_watch.rs b/crates/jcode-tui/src/tui/app/helpers/git_watch.rs new file mode 100644 index 00000000000..db5327d083b --- /dev/null +++ b/crates/jcode-tui/src/tui/app/helpers/git_watch.rs @@ -0,0 +1,392 @@ +//! Event-driven git status refresh (Linux inotify). +//! +//! Watches the repository's `.git` metadata (HEAD, index, refs) plus every +//! non-ignored work-tree directory. Any relevant change schedules one debounced +//! probe. While the watcher is healthy the git widget never polls, so an idle +//! client spawns zero processes and wakes the CPU zero times. +//! +//! Falls back to the stamp-gated polling in `helpers.rs` when inotify is +//! unavailable, the kernel watch limit is hit, or the event queue overflows. + +use std::path::Path; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// True while a watcher is running and has not lost events. +static HEALTHY: AtomicBool = AtomicBool::new(false); +static STARTED: AtomicBool = AtomicBool::new(false); + +pub(super) fn is_healthy() -> bool { + HEALTHY.load(Ordering::Acquire) +} + +/// Start the watcher for `repo_root` once per process. `on_change` runs on the +/// watcher thread after a quiet period following any relevant event. +pub(super) fn ensure_started(repo_root: &Path, on_change: fn()) { + if STARTED.swap(true, Ordering::AcqRel) { + return; + } + #[cfg(target_os = "linux")] + { + let root = repo_root.to_path_buf(); + let spawned = std::thread::Builder::new() + .name("jcode-git-watch".to_string()) + .spawn(move || imp::run(&root, on_change)); + if spawned.is_err() { + HEALTHY.store(false, Ordering::Release); + } + } + #[cfg(not(target_os = "linux"))] + let _ = (repo_root, on_change); +} + +/// Whether an event on `name` inside the git dir can change the widget. +/// Lock files and temp objects churn during every git command. +pub(super) fn git_dir_event_relevant(name: &str) -> bool { + !(name.ends_with(".lock") || name.starts_with("tmp_") || name == "ORIG_HEAD.lock") +} + +/// Directories never worth watching even if not gitignored. +pub(super) fn skip_dir(name: &str) -> bool { + name == ".git" +} + +#[cfg(target_os = "linux")] +mod imp { + use super::{HEALTHY, git_dir_event_relevant, skip_dir}; + use std::collections::{BTreeSet, HashMap}; + use std::ffi::CString; + use std::os::unix::ffi::OsStrExt; + use std::path::{Path, PathBuf}; + use std::sync::atomic::Ordering; + use std::time::Duration; + + /// Coalesce bursts (a checkout touches thousands of files) into one probe. + const DEBOUNCE: Duration = Duration::from_millis(250); + + const WORKTREE_MASK: u32 = libc::IN_CLOSE_WRITE + | libc::IN_CREATE + | libc::IN_DELETE + | libc::IN_MOVED_FROM + | libc::IN_MOVED_TO + | libc::IN_DELETE_SELF + | libc::IN_ONLYDIR; + const GIT_MASK: u32 = WORKTREE_MASK; + + struct Watcher { + fd: i32, + dirs: HashMap, // wd -> (path, is_git_metadata) + root: PathBuf, + } + + impl Watcher { + fn add(&mut self, path: &Path, git_meta: bool) -> bool { + let Ok(c) = CString::new(path.as_os_str().as_bytes()) else { + return true; + }; + let mask = if git_meta { GIT_MASK } else { WORKTREE_MASK }; + // SAFETY: valid fd and NUL-terminated path. + let wd = unsafe { libc::inotify_add_watch(self.fd, c.as_ptr(), mask) }; + if wd < 0 { + let err = std::io::Error::last_os_error(); + // ENOSPC: out of watches. Anything else (ENOENT race) is benign. + return err.raw_os_error() != Some(libc::ENOSPC); + } + self.dirs.insert(wd, (path.to_path_buf(), git_meta)); + true + } + + fn add_tree(&mut self, dirs: impl IntoIterator) -> bool { + dirs.into_iter().all(|dir| self.add(&dir, false)) + } + } + + impl Drop for Watcher { + fn drop(&mut self) { + // SAFETY: closing our own fd. + unsafe { libc::close(self.fd) }; + } + } + + fn git(root: &Path, args: &[&str]) -> Option { + let out = std::process::Command::new("git") + .current_dir(root) + .env("GIT_OPTIONAL_LOCKS", "0") + .args(args) + .stdin(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .output() + .ok()?; + out.status + .success() + .then(|| String::from_utf8_lossy(&out.stdout).into_owned()) + } + + /// Directories holding tracked or untracked-but-not-ignored files, plus + /// all their ancestors. One git call instead of walking ignored trees + /// such as `target/` or `node_modules/`. + fn worktree_dirs(root: &Path) -> Option> { + let listing = git( + root, + &[ + "ls-files", + "-z", + "--cached", + "--others", + "--exclude-standard", + ], + )?; + let mut dirs = BTreeSet::from([root.to_path_buf()]); + for file in listing.split('\0').filter(|f| !f.is_empty()) { + let mut dir = Path::new(file).parent(); + while let Some(d) = dir.filter(|d| !d.as_os_str().is_empty()) { + if !dirs.insert(root.join(d)) { + break; + } + dir = d.parent(); + } + } + Some(dirs) + } + + fn git_meta_dirs(git_dir: &Path) -> Vec { + let mut out = vec![git_dir.to_path_buf()]; + let mut stack = vec![git_dir.join("refs")]; + while let Some(dir) = stack.pop() { + if let Ok(entries) = std::fs::read_dir(&dir) { + for entry in entries.flatten() { + if entry.file_type().is_ok_and(|t| t.is_dir()) { + stack.push(entry.path()); + } + } + out.push(dir); + } + } + out + } + + fn is_ignored(root: &Path, path: &Path) -> bool { + let Ok(rel) = path.strip_prefix(root) else { + return true; + }; + std::process::Command::new("git") + .current_dir(root) + .env("GIT_OPTIONAL_LOCKS", "0") + .args(["check-ignore", "-q", "--"]) + .arg(rel) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .is_ok_and(|s| s.success()) + } + + pub(super) fn run(root: &Path, on_change: fn()) { + let git_dir = root.join(".git"); + if !git_dir.is_dir() { + // Worktrees/submodules use a .git file; keep polling there. + return; + } + // SAFETY: plain syscall. + let fd = unsafe { libc::inotify_init1(libc::IN_CLOEXEC) }; + if fd < 0 { + return; + } + let mut w = Watcher { + fd, + dirs: HashMap::new(), + root: root.to_path_buf(), + }; + let ok = git_meta_dirs(&git_dir).iter().all(|d| w.add(d, true)) + && worktree_dirs(root).is_some_and(|dirs| w.add_tree(dirs)); + if !ok { + crate::logging::info("git_watch: inotify watch limit reached; using polling"); + return; + } + crate::logging::info(&format!( + "git_watch: watching {} directories under {}", + w.dirs.len(), + root.display() + )); + HEALTHY.store(true, Ordering::Release); + + let mut buf = vec![0u8; 64 * 1024]; + let mut pending = false; + loop { + // Block indefinitely when idle; once dirty, wait out the debounce. + let timeout = if pending { + DEBOUNCE.as_millis() as i32 + } else { + -1 + }; + let mut pfd = libc::pollfd { + fd: w.fd, + events: libc::POLLIN, + revents: 0, + }; + // SAFETY: one valid pollfd. + let ready = unsafe { libc::poll(&mut pfd, 1, timeout) }; + if ready < 0 { + if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { + continue; + } + break; + } + if ready == 0 { + pending = false; + on_change(); + continue; + } + // SAFETY: reading into our buffer. + let n = unsafe { libc::read(w.fd, buf.as_mut_ptr().cast(), buf.len()) }; + if n <= 0 { + break; + } + let mut off = 0usize; + let header = std::mem::size_of::(); + let mut new_dirs = Vec::new(); + while off + header <= n as usize { + // SAFETY: kernel guarantees a full event at this offset. + let ev: libc::inotify_event = + unsafe { std::ptr::read_unaligned(buf.as_ptr().add(off).cast()) }; + let name_bytes = &buf[off + header..off + header + ev.len as usize]; + let name_end = name_bytes + .iter() + .position(|b| *b == 0) + .unwrap_or(name_bytes.len()); + let name = String::from_utf8_lossy(&name_bytes[..name_end]).into_owned(); + off += header + ev.len as usize; + + if ev.mask & libc::IN_Q_OVERFLOW != 0 { + // Lost events: refresh now and stay healthy; a probe + // reads full state anyway. + pending = true; + continue; + } + if ev.mask & libc::IN_IGNORED != 0 { + w.dirs.remove(&ev.wd); + continue; + } + let Some((dir, git_meta)) = w.dirs.get(&ev.wd).cloned() else { + continue; + }; + if git_meta { + if git_dir_event_relevant(&name) { + pending = true; + } + if ev.mask & libc::IN_ISDIR != 0 + && ev.mask & (libc::IN_CREATE | libc::IN_MOVED_TO) != 0 + { + new_dirs.push((dir.join(&name), true)); + } + continue; + } + if skip_dir(&name) { + continue; + } + if ev.mask & libc::IN_ISDIR != 0 + && ev.mask & (libc::IN_CREATE | libc::IN_MOVED_TO) != 0 + { + new_dirs.push((dir.join(&name), false)); + } + pending = true; + } + for (dir, git_meta) in new_dirs { + if !git_meta && is_ignored(&w.root, &dir) { + continue; + } + if !w.add(&dir, git_meta) { + crate::logging::info("git_watch: watch limit reached; falling back to polling"); + HEALTHY.store(false, Ordering::Release); + return; + } + // Files may have landed before the watch existed. + if !git_meta && let Some(sub) = worktree_dirs(&dir) { + let root = w.root.clone(); + let extra: Vec<_> = sub + .into_iter() + .filter(|d| d.starts_with(&root) && d != &dir) + .collect(); + if !w.add_tree(extra) { + HEALTHY.store(false, Ordering::Release); + return; + } + } + } + } + HEALTHY.store(false, Ordering::Release); + } + + #[cfg(test)] + mod tests { + use std::process::Command; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::time::{Duration, Instant}; + + static HITS: AtomicUsize = AtomicUsize::new(0); + fn bump() { + HITS.fetch_add(1, Ordering::SeqCst); + } + + fn wait_for(target: usize) -> bool { + let deadline = Instant::now() + Duration::from_secs(5); + while Instant::now() < deadline { + if HITS.load(Ordering::SeqCst) >= target { + return true; + } + std::thread::sleep(Duration::from_millis(20)); + } + false + } + + #[test] + fn fires_on_worktree_and_git_changes_but_not_ignored_dirs() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().to_path_buf(); + let git = |args: &[&str]| { + assert!( + Command::new("git") + .current_dir(&root) + .args(args) + .output() + .unwrap() + .status + .success() + ); + }; + git(&["init", "-q"]); + std::fs::write(root.join(".gitignore"), "target/\n").unwrap(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::create_dir_all(root.join("target")).unwrap(); + std::fs::write(root.join("src/a.txt"), "a").unwrap(); + + let r = root.clone(); + std::thread::spawn(move || super::run(&r, bump)); + let deadline = Instant::now() + Duration::from_secs(5); + while !super::HEALTHY.load(Ordering::Acquire) && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert!(super::HEALTHY.load(Ordering::Acquire)); + + // Ignored dir: no event. + std::fs::write(root.join("target/junk"), "x").unwrap(); + std::thread::sleep(Duration::from_millis(600)); + assert_eq!(HITS.load(Ordering::SeqCst), 0); + + // Unstaged edit in a nested dir. + std::fs::write(root.join("src/a.txt"), "b").unwrap(); + assert!(wait_for(1)); + + // New directory created after start is picked up. + std::fs::create_dir_all(root.join("src/new")).unwrap(); + std::thread::sleep(Duration::from_millis(400)); + let before = HITS.load(Ordering::SeqCst); + std::fs::write(root.join("src/new/b.txt"), "b").unwrap(); + assert!(wait_for(before + 1)); + + // Git metadata change (staging). + let before = HITS.load(Ordering::SeqCst); + git(&["add", "-A"]); + assert!(wait_for(before + 1)); + } + } +} From e8d0cbe6a1a1026279acca6be2bea9db502c7aa1 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:47:52 -0800 Subject: [PATCH 093/236] Show running cloud sessions in /resume Poll each cloud host's streaming markers over one bounded SSH read every 10s, off the render thread, and overlay them on local presence so cloud turns get the running spinner and a live-following preview. --- crates/jcode-tui/src/tui/session_picker.rs | 6 + .../src/tui/session_picker/cloud_presence.rs | 214 ++++++++++++++++++ .../src/tui/session_picker_presence.rs | 6 +- .../jcode-tui/src/tui/session_picker_tests.rs | 4 + 4 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 crates/jcode-tui/src/tui/session_picker/cloud_presence.rs diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index 0c94ad99a6d..da06235961f 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -38,6 +38,7 @@ mod benchmark_io; #[cfg(test)] mod benchmark_matrix; mod canvas; +mod cloud_presence; mod cloud_preview; mod filter; mod identity_search; @@ -359,6 +360,8 @@ pub struct SessionPicker { recent_window_closures: HashMap>, /// When `live_presence` was last snapshotted (throttles periodic refresh). live_presence_refreshed_at: Option, + /// Running turns on cloud hosts, polled off the render thread. + cloud_presence: cloud_presence::CloudPresence, /// ID of the session the picker was opened from, labeled "current" in the /// list so the user can orient themselves in the Active view. current_session_id: Option, @@ -427,6 +430,7 @@ impl SessionPicker { live_presence: std::collections::HashMap::new(), recent_window_closures: HashMap::new(), live_presence_refreshed_at: None, + cloud_presence: Default::default(), current_session_id: None, pending_claude_takeover: None, }; @@ -487,6 +491,7 @@ impl SessionPicker { live_presence: std::collections::HashMap::new(), recent_window_closures: HashMap::new(), live_presence_refreshed_at: None, + cloud_presence: Default::default(), current_session_id: None, pending_claude_takeover: None, } @@ -579,6 +584,7 @@ impl SessionPicker { live_presence: std::collections::HashMap::new(), recent_window_closures: HashMap::new(), live_presence_refreshed_at: None, + cloud_presence: Default::default(), current_session_id: None, pending_claude_takeover: None, }; diff --git a/crates/jcode-tui/src/tui/session_picker/cloud_presence.rs b/crates/jcode-tui/src/tui/session_picker/cloud_presence.rs new file mode 100644 index 00000000000..f1da903bfb4 --- /dev/null +++ b/crates/jcode-tui/src/tui/session_picker/cloud_presence.rs @@ -0,0 +1,214 @@ +//! Running-turn presence for sessions owned by a cloud host. +//! +//! Local presence comes from `~/.jcode/streaming_pids`. Cloud sessions keep +//! the same markers on their VM, so one bounded SSH read per host reports +//! which turns are running there. Never runs on the render thread. +use super::*; +use std::sync::mpsc::{Receiver, TryRecvError}; + +const REFRESH_INTERVAL: Duration = Duration::from_secs(10); + +/// Lists ` ` for markers whose owner is alive. +const SCRIPT: &str = r#"d="$HOME/.jcode/streaming_pids" +[ -d "$d" ] || exit 0 +for f in "$d"/*; do + [ -f "$f" ] || continue + p=$(cat "$f" 2>/dev/null) + case "$p" in ''|*[!0-9]*) continue ;; esac + kill -0 "$p" 2>/dev/null || continue + printf '%s %s\n' "$(basename "$f")" "$(stat -c %Y "$f" 2>/dev/null || echo 0)" +done +"#; + +#[derive(Default)] +pub(super) struct CloudPresence { + /// Running cloud sessions from the last completed poll, by session id. + pub(super) streaming: HashMap>, + pending: Option>>>, + started_at: Option, +} + +pub(super) fn parse(output: &str) -> HashMap> { + output + .lines() + .filter_map(|line| { + let (id, mtime) = line.trim().split_once(' ')?; + let valid = !id.is_empty() + && id + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'); + valid.then(|| { + let since = mtime + .trim() + .parse::() + .ok() + .filter(|secs| *secs > 0) + .map(|secs| std::time::UNIX_EPOCH + Duration::from_secs(secs)); + (id.to_string(), since) + }) + }) + .collect() +} + +fn poll_host(host: &str) -> Option>> { + if host.is_empty() || host.starts_with('-') { + return None; + } + let config = crate::storage::jcode_dir().ok()?.join("cloud/ssh_config"); + let command = crate::tui::cloud_git::script_command( + Some(host), + config.is_file().then_some(config.as_path()), + ".", + SCRIPT, + ); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .ok()?; + let (status, bytes) = runtime.block_on(crate::tui::cloud_git::bounded_output_for( + command, + Duration::from_secs(10), + 64 * 1024, + crate::tui::cloud_git::WorkspaceOperation::CloudStatus, + ))?; + status + .success() + .then(|| parse(&String::from_utf8_lossy(&bytes))) +} + +impl SessionPicker { + fn cloud_hosts(&self) -> Vec { + let mut hosts: Vec = self + .all_sessions + .iter() + .filter(|session| session.server_icon.as_deref() == Some("☁")) + .filter_map(|session| { + session + .server_name + .as_deref()? + .strip_prefix("cloud: ") + .map(str::to_string) + }) + .collect(); + hosts.sort(); + hosts.dedup(); + hosts + } + + /// Collect a finished poll and start the next one when due. Returns true + /// when the set of running cloud sessions changed. + pub(super) fn maybe_refresh_cloud_presence(&mut self) -> bool { + let mut changed = false; + if let Some(receiver) = self.cloud_presence.pending.as_ref() { + match receiver.try_recv() { + Ok(streaming) => { + changed = streaming != self.cloud_presence.streaming; + self.cloud_presence.streaming = streaming; + self.cloud_presence.pending = None; + } + Err(TryRecvError::Disconnected) => self.cloud_presence.pending = None, + Err(TryRecvError::Empty) => {} + } + } + let due = self + .cloud_presence + .started_at + .is_none_or(|at| at.elapsed() >= REFRESH_INTERVAL); + if self.cloud_presence.pending.is_none() && due { + let hosts = self.cloud_hosts(); + self.cloud_presence.started_at = Some(std::time::Instant::now()); + if hosts.is_empty() { + if !self.cloud_presence.streaming.is_empty() { + self.cloud_presence.streaming.clear(); + changed = true; + } + } else { + let (tx, rx) = std::sync::mpsc::channel(); + let spawned = std::thread::Builder::new() + .name("jcode-cloud-presence".into()) + .spawn(move || { + let mut all = HashMap::new(); + for host in hosts { + if let Some(found) = poll_host(&host) { + all.extend(found); + } + } + let _ = tx.send(all); + }); + if spawned.is_ok() { + self.cloud_presence.pending = Some(rx); + } + } + } + if changed { + self.merge_cloud_presence(); + if self.filter_mode == SessionFilterMode::Active { + self.rebuild_items(); + } + } + changed + } + + /// Overlay remote running turns onto the local presence snapshot. + pub(super) fn merge_cloud_presence(&mut self) { + self.live_presence + .retain(|_, presence| !(presence.pid == 0 && presence.streaming)); + for (session_id, since) in &self.cloud_presence.streaming { + let is_cloud = self.all_sessions.iter().any(|session| { + session.id == *session_id && session.server_icon.as_deref() == Some("☁") + }); + if !is_cloud { + continue; + } + self.live_presence.insert( + session_id.clone(), + crate::session::SessionPresence { + session_id: session_id.clone(), + pid: 0, + streaming: true, + streaming_since: *since, + internal: false, + }, + ); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_live_markers_and_rejects_unsafe_ids() { + let parsed = parse("session_a_1 1700000000\nbad/id 5\nsession_b_2 0\n\n"); + assert_eq!(parsed.len(), 2); + assert_eq!( + parsed["session_a_1"], + Some(std::time::UNIX_EPOCH + Duration::from_secs(1_700_000_000)) + ); + assert_eq!(parsed["session_b_2"], None); + } + + #[test] + fn cloud_running_session_shows_running_state() { + let mut session = make_cloud_session(); + session.id = "session_cloud_1".into(); + let mut picker = SessionPicker::new(vec![session.clone()]); + picker + .cloud_presence + .streaming + .insert(session.id.clone(), None); + picker.merge_cloud_presence(); + assert!(picker.session_is_streaming(&session)); + // A local refresh must not drop the remote running state. + picker.refresh_live_presence(); + assert!(picker.session_is_streaming(&session)); + } + + fn make_cloud_session() -> SessionInfo { + let mut session = super::super::tests::make_session_for_presence("cloud"); + session.server_name = Some("cloud: jcode-cloud".into()); + session.server_icon = Some("☁".into()); + session + } +} diff --git a/crates/jcode-tui/src/tui/session_picker_presence.rs b/crates/jcode-tui/src/tui/session_picker_presence.rs index a2f925653a6..7f493509183 100644 --- a/crates/jcode-tui/src/tui/session_picker_presence.rs +++ b/crates/jcode-tui/src/tui/session_picker_presence.rs @@ -26,6 +26,7 @@ impl SessionPicker { crate::session::recent_window_closures(std::time::Duration::from_secs( crate::config::config().display.resume_recent_close_seconds, )); + self.merge_cloud_presence(); self.live_presence_refreshed_at = Some(std::time::Instant::now()); } @@ -38,11 +39,12 @@ impl SessionPicker { if self.loading_message.is_some() { return false; } + let cloud_changed = self.maybe_refresh_cloud_presence(); let due = self .live_presence_refreshed_at .is_none_or(|at| at.elapsed() >= LIVE_PRESENCE_REFRESH_INTERVAL); if !due { - return false; + return cloud_changed; } let before = std::mem::take(&mut self.live_presence); let closures_before = std::mem::take(&mut self.recent_window_closures); @@ -52,7 +54,7 @@ impl SessionPicker { if closures_changed || (changed && self.filter_mode == SessionFilterMode::Active) { self.rebuild_items(); } - changed + changed || cloud_changed } /// A recent final-client EOF, not recent messages or a daemon-owned Agent. diff --git a/crates/jcode-tui/src/tui/session_picker_tests.rs b/crates/jcode-tui/src/tui/session_picker_tests.rs index 0ee387140e3..c83ac73c2c8 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests.rs @@ -26,6 +26,10 @@ fn line_text(line: &Line<'_>) -> String { .collect::() } +pub(super) fn make_session_for_presence(id: &str) -> SessionInfo { + make_session(id, id, false, SessionStatus::Closed) +} + fn make_session(id: &str, short_name: &str, is_debug: bool, status: SessionStatus) -> SessionInfo { make_session_with_flags(id, short_name, is_debug, false, status) } From a37f0bff20891fb30c8dddc74d9bbe9f2d907ece Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:49:47 -0800 Subject: [PATCH 094/236] session picker search: keep decode work across keystrokes Cache transcripts up to 16 MB, decode only appended journal records (verified against boundary bytes), let started decodes finish and be shared instead of cancelled per keystroke, and prefetch recent sessions when the picker loads. --- crates/jcode-tui/src/tui/session_picker.rs | 6 + .../tui/session_picker/transcript_search.rs | 20 ++ .../transcript_search/text_cache.rs | 299 ++++++++++++++++-- .../transcript_search/warmup.rs | 24 ++ .../session_picker/transcript_search_tests.rs | 2 +- 5 files changed, 321 insertions(+), 30 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index da06235961f..bd8061a73bb 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -773,6 +773,12 @@ impl SessionPicker { } else { (Vec::new(), orphan_sessions) }; + transcript_search::prefetch_recent( + all_sessions + .iter() + .chain(all_orphan_sessions.iter()) + .chain(server_groups.iter().flat_map(|g| g.sessions.iter())), + ); self.all_sessions = all_sessions; self.all_server_groups = server_groups; self.all_orphan_sessions = all_orphan_sessions; diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs index 68d761e68d6..c283d3995b1 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search.rs @@ -647,6 +647,26 @@ impl TranscriptSearch { } } +/// Sessions decoded ahead of the first query, most recent first. Bounded so +/// prefetch stays well inside the text cache's entry and byte budgets. +const PREFETCH_SESSIONS: usize = 96; + +/// Start decoding the most recent transcripts as soon as the picker has its +/// session list, so typing begins against a warm cache. +pub(super) fn prefetch_recent<'a>(sessions: impl Iterator) { + if cfg!(test) { + return; + } + let mut recent: Vec<&SessionInfo> = sessions.collect(); + recent.sort_by(|a, b| b.last_message_time.cmp(&a.last_message_time)); + let paths = recent + .into_iter() + .take(PREFETCH_SESSIONS) + .flat_map(|session| transcript_paths(session).into_iter().take(1)) + .collect(); + warmup::prefetch(paths); +} + fn transcript_paths(session: &SessionInfo) -> Vec { match &session.resume_target { ResumeTarget::JcodeSession { session_id } => { diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs index 6a6c4b82408..d7f8a8d0197 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/text_cache.rs @@ -11,9 +11,12 @@ use std::sync::{ }; use std::time::SystemTime; -const MAX_FILE: usize = 2 * 1024 * 1024; -const MAX_TEXT: usize = 4 * 1024 * 1024; -const MAX_TOTAL: usize = 32 * 1024 * 1024; +// Long sessions are exactly the ones a streaming rescan hurts most: a 10 MB +// transcript scanned per keystroke and cancelled by the next one never +// finishes. Decode it once, off the query path, and keep it resident. +const MAX_FILE: usize = 16 * 1024 * 1024; +const MAX_TEXT: usize = 48 * 1024 * 1024; +const MAX_TOTAL: usize = 256 * 1024 * 1024; const MAX_ENTRIES: usize = 256; #[derive(Clone, Debug, PartialEq, Eq)] @@ -50,6 +53,28 @@ struct Entry { text: Arc, bytes: usize, used: u64, + /// Bytes of whole JSONL records reflected in `text`. A torn trailing + /// record failed to parse, so the next append decode restarts there. + records_end: Boundary, +} +/// End of the decoded JSONL prefix plus the bytes just before it. A rewrite +/// that keeps the inode and grows looks like an append by metadata alone, so +/// these bytes are re-read and compared before the prefix is reused. +#[derive(Clone, Default)] +pub(super) struct Boundary { + end: u64, + check: Vec, +} +const BOUNDARY_CHECK: usize = 256; +impl Fingerprint { + /// Same file, only grown: an append-only journal gained records. + fn is_append_of(&self, older: &Fingerprint) -> bool { + #[cfg(unix)] + let same_file = self.identity.0 == older.identity.0 && self.identity.1 == older.identity.1; + #[cfg(not(unix))] + let same_file = true; + same_file && self.len > older.len + } } #[derive(Default)] struct Cache { @@ -63,20 +88,37 @@ impl Cache { self.bytes -= entry.bytes; } } + /// A stale entry for the same journal that has since only grown, plus + /// the byte length it covers. Only meaningful for append-only JSONL. + fn stale_prefix( + &mut self, + path: &Path, + fingerprint: &Fingerprint, + ) -> Option<(Arc, Boundary)> { + let entry = self.entries.get(path)?; + fingerprint + .is_append_of(&entry.fingerprint) + .then(|| (entry.text.clone(), entry.records_end.clone())) + } fn get(&mut self, path: &Path, fingerprint: Option<&Fingerprint>) -> Option> { self.clock = self.clock.wrapping_add(1); - if self - .entries - .get(path) - .is_some_and(|entry| Some(&entry.fingerprint) != fingerprint) - { - self.remove(path); - } let entry = self.entries.get_mut(path)?; + // Keep a stale entry: a grown journal reuses it as a decoded prefix. + // insert() replaces it and LRU eviction bounds it otherwise. + if Some(&entry.fingerprint) != fingerprint { + return None; + } entry.used = self.clock; Some(entry.text.clone()) } - fn insert(&mut self, path: &Path, fingerprint: Fingerprint, text: Arc, bytes: usize) { + fn insert( + &mut self, + path: &Path, + fingerprint: Fingerprint, + text: Arc, + bytes: usize, + records_end: Boundary, + ) { self.remove(path); while self.bytes + bytes > MAX_TOTAL || self.entries.len() >= MAX_ENTRIES { let Some(oldest) = self @@ -98,6 +140,7 @@ impl Cache { text, bytes, used: self.clock, + records_end, }, ); } @@ -126,6 +169,10 @@ pub(super) fn cached_segments(path: &Path, cancel: &AtomicBool) -> Option Option> { if cancel.load(Ordering::Relaxed) { return None; @@ -136,11 +183,107 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option { return Some(text); } - let fingerprint = fingerprint?; + let _claim = loop { + if let Some(claim) = DecodeClaim::try_acquire(path) { + break claim; + } + if cancel.load(Ordering::Relaxed) { + return None; + } + // Another worker is decoding this file. Its result lands in the cache. + let fingerprint = Fingerprint::read(path); + if let Some(text) = + preview_result(cache().lock(), "text cache lock")?.get(path, fingerprint.as_ref()) + { + return Some(text); + } + }; + // Recheck: the previous claimant may have just published this generation. + let fingerprint = Fingerprint::read(path); + if let Some(text) = + preview_result(cache().lock(), "text cache lock")?.get(path, fingerprint.as_ref()) + { + return Some(text); + } + let text = decode_and_publish(path, fingerprint?, &NEVER_CANCEL)?; + (!cancel.load(Ordering::Relaxed)).then_some(text) +} + +static NEVER_CANCEL: AtomicBool = AtomicBool::new(false); + +/// One decode per path at a time, across query workers and warmup. +struct DecodeClaim(PathBuf); +fn in_flight() -> &'static ( + Mutex>, + std::sync::Condvar, +) { + static IN_FLIGHT: OnceLock<( + Mutex>, + std::sync::Condvar, + )> = OnceLock::new(); + IN_FLIGHT.get_or_init(Default::default) +} +impl DecodeClaim { + /// Claims the path, or waits briefly for the current claimant and + /// returns None so the caller can recheck the cache and its cancel flag. + fn try_acquire(path: &Path) -> Option { + let (lock, wake) = in_flight(); + let mut claimed = preview_result(lock.lock(), "decode claim lock")?; + if claimed.insert(path.to_owned()) { + return Some(Self(path.to_owned())); + } + let _ = wake.wait_timeout(claimed, std::time::Duration::from_millis(10)); + None + } +} +impl Drop for DecodeClaim { + fn drop(&mut self) { + let (lock, wake) = in_flight(); + if let Ok(mut claimed) = lock.lock() { + claimed.remove(&self.0); + } + wake.notify_all(); + } +} + +/// Whether the file still holds the bytes that ended the cached prefix. +fn boundary_matches(file: &mut std::fs::File, boundary: &Boundary) -> bool { + use std::io::Seek; + let Some(from) = boundary.end.checked_sub(boundary.check.len() as u64) else { + return false; + }; + let mut bytes = vec![0; boundary.check.len()]; + file.seek(std::io::SeekFrom::Start(from)).is_ok() + && file.read_exact(&mut bytes).is_ok() + && bytes == boundary.check +} + +fn decode_and_publish( + path: &Path, + fingerprint: Fingerprint, + cancel: &AtomicBool, +) -> Option> { if fingerprint.len > MAX_FILE as u64 { return None; } - let file = preview_result(std::fs::File::open(path), "transcript open")?; + let jsonl = path.extension().is_some_and(|ext| ext == "jsonl"); + // A live journal only appends records. Decode just the new tail and share + // the already decoded prefix instead of re-reading the whole session. + let prior = if jsonl { + preview_result(cache().lock(), "text cache lock")?.stale_prefix(path, &fingerprint) + } else { + None + }; + let mut file = preview_result(std::fs::File::open(path), "transcript open")?; + let prior = prior.filter(|(_, boundary)| boundary_matches(&mut file, boundary)); + let start = prior.as_ref().map_or(0, |(_, boundary)| boundary.end); + { + use std::io::Seek; + preview_result( + file.seek(std::io::SeekFrom::Start(start)), + "transcript seek", + )?; + } let mut input = Vec::new(); // Bounded even when a live journal grows after stat. let mut reader = super::CancellableReader { @@ -149,19 +292,68 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option }; preview_result( (&mut reader) - .take(MAX_FILE as u64 + 1) + .take(fingerprint.len - start) .read_to_end(&mut input), "transcript read", )?; - if input.len() > MAX_FILE || cancel.load(Ordering::Relaxed) { + if cancel.load(Ordering::Relaxed) { return None; } - let (text, bytes) = decode( - &input, - path.extension().is_some_and(|ext| ext == "jsonl"), - cancel, - )?; - if Fingerprint::read(path).as_ref() != Some(&fingerprint) || cancel.load(Ordering::Relaxed) { + let records_end = start + + if jsonl { + let whole = input + .iter() + .rposition(|byte| *byte == b'\n') + .map_or(0, |end| end + 1); + // A final record without a newline is complete when it parses, + // e.g. a finished external transcript. A torn live write is not. + let tail = &input[whole..]; + if tail.trim_ascii().is_empty() + || serde_json::from_slice::(tail).is_ok() + { + input.len() as u64 + } else { + whole as u64 + } + } else { + input.len() as u64 + }; + // Decode whole records only. A record still being written is picked up, + // exactly once, by the next append decode. + let input = &input[..(records_end - start) as usize]; + let boundary = Boundary { + end: records_end, + check: input[input.len().saturating_sub(BOUNDARY_CHECK)..].to_vec(), + }; + let (text, bytes) = match prior { + Some((prefix, _)) => { + // Only whole records were cached, so the tail starts at a record. + let (tail, _) = decode(input, true, cancel)?; + let mut text = Segments { + text: Vec::with_capacity(prefix.text.len() + tail.text.len()), + sources: Vec::with_capacity(prefix.sources.len() + tail.sources.len()), + }; + text.text.extend(prefix.text.iter().cloned()); + text.text.extend(tail.text); + text.sources.extend(prefix.sources.iter().copied()); + text.sources.extend(tail.sources); + let bytes = segments_bytes(&text); + if bytes > MAX_TEXT { + return None; + } + (text, bytes) + } + None => decode(input, jsonl, cancel)?, + }; + let current = Fingerprint::read(path); + let unchanged = current.as_ref() == Some(&fingerprint); + // A live journal that grew while we decoded still yields a valid prefix: + // publish it so the next decode reads only the new records. + let grown = jsonl + && current + .as_ref() + .is_some_and(|current| current.is_append_of(&fingerprint)); + if !unchanged && !grown { return None; } let text = Arc::new(text); @@ -170,7 +362,11 @@ pub(super) fn segments(path: &Path, cancel: &AtomicBool) -> Option fingerprint, text.clone(), bytes, + boundary, ); + if !unchanged || cancel.load(Ordering::Relaxed) { + return None; + } Some(text) } @@ -259,17 +455,16 @@ fn decode(input: &[u8], jsonl: bool, cancel: &AtomicBool) -> Option<(Segments, u } collector.text.text.shrink_to_fit(); collector.text.sources.shrink_to_fit(); - let bytes = collector.text.text.capacity() * std::mem::size_of::() - + collector.text.sources.capacity() - + collector - .text - .text - .iter() - .map(String::capacity) - .sum::(); + let bytes = segments_bytes(&collector.text); Some((collector.text, bytes)) } +fn segments_bytes(text: &Segments) -> usize { + text.text.capacity() * std::mem::size_of::() + + text.sources.capacity() + + text.text.iter().map(String::capacity).sum::() +} + #[cfg(test)] mod tests { use super::*; @@ -306,6 +501,49 @@ mod tests { assert!(segments(&path, &cancel).is_none()); } #[test] + fn journal_appends_decode_only_new_whole_records() { + use std::io::Write; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("live.journal.jsonl"); + let cancel = AtomicBool::new(false); + std::fs::write(&path, "{\"t\":\"ONE\"}\n").unwrap(); + let first = segments(&path, &cancel).unwrap(); + assert_eq!(&first.text, &["one"]); + let mut file = std::fs::OpenOptions::new() + .append(true) + .open(&path) + .unwrap(); + // A torn record is left for the next decode. + file.write_all(b"{\"t\":\"TWO\"}\n{\"t\":\"TH").unwrap(); + file.flush().unwrap(); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["one", "two"]); + file.write_all(b"REE\"}\n").unwrap(); + file.flush().unwrap(); + let last = segments(&path, &cancel).unwrap(); + assert_eq!(&last.text, &["one", "two", "three"]); + // Unchanged file reuses the published entry. + assert!(Arc::ptr_eq(&last, &segments(&path, &cancel).unwrap())); + // A rewrite (not an append) decodes from scratch. + std::fs::remove_file(&path).unwrap(); + std::fs::write( + &path, + "{\"t\":\"FRESH\"}\n{\"t\":\"FILE\"}\n{\"t\":\"HERE\"}\n", + ) + .unwrap(); + assert_eq!( + &segments(&path, &cancel).unwrap().text, + &["fresh", "file", "here"] + ); + } + #[test] + fn complete_unterminated_final_record_is_kept() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("external.jsonl"); + std::fs::write(&path, "{\"t\":\"A\"}\n{\"t\":\"B\"}").unwrap(); + let cancel = AtomicBool::new(false); + assert_eq!(&segments(&path, &cancel).unwrap().text, &["a", "b"]); + } + #[test] fn large_files_request_streaming_fallback() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("large.json"); @@ -351,6 +589,7 @@ mod tests { fp.clone(), Arc::new(Segments::default()), 1, + Boundary::default(), ); } cache.get(Path::new("0"), Some(&fp)).unwrap(); @@ -359,6 +598,7 @@ mod tests { fp.clone(), Arc::new(Segments::default()), 1, + Boundary::default(), ); assert!(cache.entries.contains_key(Path::new("0"))); assert!(!cache.entries.contains_key(Path::new("1"))); @@ -367,6 +607,7 @@ mod tests { fp.clone(), Arc::new(Segments::default()), MAX_TOTAL, + Boundary::default(), ); assert_eq!(cache.entries.len(), 1); assert_eq!(cache.bytes, MAX_TOTAL); diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs index f51ffbe5398..6887b82aac5 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/warmup.rs @@ -64,6 +64,30 @@ pub(super) fn schedule<'a>(paths: impl Iterator) { } } +/// Decode the given transcripts on one background thread, in order, so the +/// first search after opening the picker finds them already cached. Each call +/// supersedes the previous one. Query workers share decodes via the claim in +/// `text_cache`, so a file being prefetched is never decoded twice. +pub(super) fn prefetch(paths: Vec) { + static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + use std::sync::atomic::Ordering; + let generation = GENERATION.fetch_add(1, Ordering::Relaxed) + 1; + let spawned = std::thread::Builder::new() + .name("jcode-search-prefetch".into()) + .spawn(move || { + let cancel = AtomicBool::new(false); + for path in paths { + if GENERATION.load(Ordering::Relaxed) != generation { + return; + } + if !text_cache::is_cached_hint(&path) { + let _ = text_cache::segments(&path, &cancel); + } + } + }); + let _ = super::super::preview_result(spawned, "search prefetch startup"); +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs index 211859cb5bf..d5ae84d0051 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search_tests.rs @@ -69,7 +69,7 @@ fn uncached_streaming_search_does_not_wait_for_a_typing_timer() { let path = dir.path().join("oversized.jsonl"); // Too large for warmup or decoded-text caching, so this must use cold IO. let mut text = format!("{}\n", r#"{"text":"needle"}"#); - text.push_str(&" ".repeat(2 * 1024 * 1024)); + text.push_str(&" ".repeat(17 * 1024 * 1024)); std::fs::write(&path, text).unwrap(); let session = external_session("cold", &path); let mut search = TranscriptSearch::default(); From 6a75170d76bbca5f32728ae7a5a5f15ee4f3630f Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:51:03 -0800 Subject: [PATCH 095/236] Fix #2087: buffer UTF-8 across SSE chunks in Anthropic and Copilot streams --- .../src/lib.rs | 26 +++++++++++++++++-- .../jcode-provider-copilot-runtime/src/lib.rs | 3 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/jcode-provider-anthropic-runtime/src/lib.rs b/crates/jcode-provider-anthropic-runtime/src/lib.rs index b86b4d84500..98853953d76 100644 --- a/crates/jcode-provider-anthropic-runtime/src/lib.rs +++ b/crates/jcode-provider-anthropic-runtime/src/lib.rs @@ -2323,6 +2323,7 @@ async fn stream_response( // Parse SSE stream let mut stream = response.bytes_stream(); let mut buffer = String::new(); + let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); let mut sse_state = SseStreamState { requested_model_base, ..SseStreamState::default() @@ -2346,8 +2347,7 @@ async fn stream_response( ); } }; - let chunk_str = String::from_utf8_lossy(&chunk); - buffer.push_str(&chunk_str); + buffer.push_str(&utf8.decode(&chunk)); // Process complete SSE events while let Some(event) = parse_sse_event(&mut buffer) { @@ -2690,3 +2690,25 @@ mod account_failover_tests; #[cfg(test)] mod account_binding_tests; + +#[cfg(test)] +mod sse_utf8_chunk_tests { + use super::parse_sse_event; + + /// Regression for #2087: a multibyte character split across transport + /// chunks must not decode to U+FFFD. + #[test] + fn multibyte_char_split_across_chunks_decodes_cleanly() { + let payload = "event: content_block_delta\ndata: {\"text\":\"你好世界\"}\n\n"; + let bytes = payload.as_bytes(); + let split = payload.find('你').unwrap() + 1; // inside the 3-byte char + let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); + let mut buffer = String::new(); + for chunk in [&bytes[..split], &bytes[split..split + 3], &bytes[split + 3..]] { + buffer.push_str(&utf8.decode(chunk)); + } + let event = parse_sse_event(&mut buffer).expect("complete event"); + assert!(!event.data.contains('\u{FFFD}')); + assert!(event.data.contains("你好世界")); + } +} diff --git a/crates/jcode-provider-copilot-runtime/src/lib.rs b/crates/jcode-provider-copilot-runtime/src/lib.rs index 5ef9caf8d24..692b794d675 100644 --- a/crates/jcode-provider-copilot-runtime/src/lib.rs +++ b/crates/jcode-provider-copilot-runtime/src/lib.rs @@ -699,6 +699,7 @@ impl CopilotApiProvider { let mut stream = resp.bytes_stream(); let mut buffer = String::new(); + let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); let mut current_tool_id = String::new(); let mut current_tool_name = String::new(); let mut current_tool_args = String::new(); @@ -727,7 +728,7 @@ impl CopilotApiProvider { }; saw_any_data = true; - buffer.push_str(&String::from_utf8_lossy(&chunk)); + buffer.push_str(&utf8.decode(&chunk)); // Process complete SSE lines while let Some(line_end) = buffer.find('\n') { From 3587784eb946565de9dbe579429c54636c32fc1e Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:51:34 -0800 Subject: [PATCH 096/236] fix(compaction): reject refused or empty summaries instead of dropping messages (fixes #2089) Simple completions now fail on refusal/content_filter/safety stop reasons, and text compaction summaries that are empty or refusal-shaped are rejected, so the original messages are kept. --- crates/jcode-base/src/compaction.rs | 44 +++++++++++- crates/jcode-base/src/compaction_tests.rs | 82 +++++++++++++++++++++++ crates/jcode-provider-core/src/lib.rs | 29 ++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) diff --git a/crates/jcode-base/src/compaction.rs b/crates/jcode-base/src/compaction.rs index 5b9fc8fd685..63b7d688322 100644 --- a/crates/jcode-base/src/compaction.rs +++ b/crates/jcode-base/src/compaction.rs @@ -1815,7 +1815,7 @@ async fn generate_compaction_artifact( ); Ok(CompactionResult { - summary_text: summary, + summary_text: validate_compaction_summary(summary)?, openai_encrypted_content: None, covers_up_to_turn: messages.len(), duration_ms: start.elapsed().as_millis() as u64, @@ -1823,6 +1823,48 @@ async fn generate_compaction_artifact( }) } +/// Reject summaries that cannot stand in for the messages they replace, such +/// as empty output or a provider refusal (issue #2089). Returning an error makes +/// compaction keep the original messages instead of dropping them. +fn validate_compaction_summary(summary: String) -> Result { + let trimmed = summary.trim(); + if trimmed.is_empty() { + anyhow::bail!("compaction summary rejected: provider returned empty text"); + } + if looks_like_refusal(trimmed) { + let preview: String = trimmed.chars().take(120).collect(); + anyhow::bail!( + "compaction summary rejected: provider refusal: {:?}", + preview + ); + } + Ok(summary) +} + +fn looks_like_refusal(text: &str) -> bool { + // Only inspect the opening so a real summary that mentions an earlier + // refusal later on is still accepted. + let head: String = text.chars().take(160).collect::().to_lowercase(); + let head = head.replace('\u{2019}', "'"); + const PATTERNS: [&str; 8] = [ + "i'm sorry, but i can", + "i am sorry, but i can", + "sorry, but i can't", + "sorry, i can't", + "i cannot assist with", + "i can't assist with", + "i can't help with", + "i'm unable to help with", + ]; + // Short replies are refusals if they contain a pattern anywhere. Longer + // ones only if they open with it. + if text.chars().count() < 300 { + PATTERNS.iter().any(|pattern| head.contains(pattern)) + } else { + PATTERNS.iter().any(|pattern| head.starts_with(pattern)) + } +} + #[path = "compaction_transfer.rs"] mod transfer; pub use transfer::build_transfer_compaction_state; diff --git a/crates/jcode-base/src/compaction_tests.rs b/crates/jcode-base/src/compaction_tests.rs index 38973abc07d..8667017af03 100644 --- a/crates/jcode-base/src/compaction_tests.rs +++ b/crates/jcode-base/src/compaction_tests.rs @@ -1267,3 +1267,85 @@ fn max_context_tokens_applies_at_construction_and_reloads_before_requests() { #[path = "compaction_native_usage_tests.rs"] mod native_usage; + +#[test] +fn compaction_summary_rejects_refusals_and_empty_text_issue_2089() { + for bad in [ + "", + " ", + "I'm sorry, but I cannot assist with that request.", + "I\u{2019}m sorry, but I can\u{2019}t help with that.", + "I can't assist with that.", + ] { + assert!( + validate_compaction_summary(bad.to_string()).is_err(), + "{bad:?}" + ); + } + let real = "## Task\nThe user is fixing compaction. Earlier the assistant said \"I can't help with that\" about a deploy, then continued. ".repeat(4); + assert!(validate_compaction_summary(real).is_ok()); +} + +struct RefusingSummaryProvider; + +#[async_trait::async_trait] +impl Provider for RefusingSummaryProvider { + async fn complete( + &self, + _messages: &[Message], + _tools: &[crate::message::ToolDefinition], + _system: &str, + _resume_session_id: Option<&str>, + ) -> Result { + Ok(Box::pin(futures::stream::iter(vec![ + Ok(crate::message::StreamEvent::TextDelta( + "I'm sorry, but I cannot assist with that request.".into(), + )), + Ok(crate::message::StreamEvent::MessageEnd { + stop_reason: Some("content_filter".into()), + }), + ]))) + } + + fn name(&self) -> &str { + "refusing" + } + + fn fork(&self) -> Arc { + Arc::new(RefusingSummaryProvider) + } +} + +#[tokio::test] +async fn refused_compaction_keeps_original_messages_issue_2089() { + let messages: Vec = (0..28) + .map(|i| { + make_text_message( + if i % 2 == 0 { + Role::User + } else { + Role::Assistant + }, + &format!("turn {i}"), + ) + }) + .collect(); + let err = + generate_compaction_artifact(Arc::new(RefusingSummaryProvider), messages.clone(), None) + .await + .err() + .expect("refused summary must fail"); + assert!(format!("{err:#}").contains("guardrail"), "{err:#}"); + + let mut manager = CompactionManager::new().with_budget(1_000); + manager.seed_restored_messages(messages.len()); + manager.update_observed_input_tokens(10_000); + let _ = manager.force_compact_with(&messages, Arc::new(RefusingSummaryProvider)); + let deadline = Instant::now() + Duration::from_secs(5); + while manager.is_compacting() && Instant::now() < deadline { + tokio::time::sleep(Duration::from_millis(10)).await; + manager.check_and_apply_compaction_with(&messages); + } + assert_eq!(manager.compacted_count(), 0); + assert_eq!(manager.summary_chars(), 0); +} diff --git a/crates/jcode-provider-core/src/lib.rs b/crates/jcode-provider-core/src/lib.rs index 883fbd58bdd..84906c17580 100644 --- a/crates/jcode-provider-core/src/lib.rs +++ b/crates/jcode-provider-core/src/lib.rs @@ -597,6 +597,23 @@ pub async fn collect_simple_completion( ); } Ok(StreamEvent::UpstreamProvider { provider }) => provider_name = provider, + Ok(StreamEvent::MessageEnd { stop_reason }) + if is_guardrail_stop_reason(stop_reason.as_deref()) => + { + // A refusal or content-filter stop is not a usable answer. Callers + // such as compaction would otherwise persist the refusal text as + // if it were real output (issue #2089). + return Err(SimpleCompletionError { + usage, + provider: provider_name, + model, + source: anyhow::anyhow!( + "simple completion stopped by provider guardrail (stop_reason={})", + stop_reason.as_deref().unwrap_or_default() + ), + } + .into()); + } Ok(StreamEvent::Error { message, .. }) => { return Err(SimpleCompletionError { usage, @@ -622,6 +639,18 @@ pub async fn collect_simple_completion( Ok((result, usage)) } +/// True when a provider stop reason is a model-side refusal, safety, or +/// content-filter stop rather than a normal end of turn or truncation. +pub fn is_guardrail_stop_reason(stop_reason: Option<&str>) -> bool { + let Some(reason) = stop_reason else { + return false; + }; + let reason = reason.trim().to_ascii_lowercase(); + matches!(reason.as_str(), "refusal" | "content_filter" | "safety") + || reason.contains("guardrail") + || reason.contains("policy_violation") +} + /// Failed simple completion with the usage already observed before failure. /// Dropping the completion future (for example on timeout) cannot return this /// snapshot. Cancellation accounting requires a separate observer lifecycle. From 4f2278d802fa5a91e31904bab278504647299f54 Mon Sep 17 00:00:00 2001 From: Guntrip Date: Sun, 8 Nov 2026 03:51:40 -0800 Subject: [PATCH 097/236] fix(build): sweep stale launcher backups on Windows publish (#2065) --- .github/workflows/ci.yml | 12 ++ .../src/platform_support.rs | 117 +++++++++++++++++- 2 files changed, 126 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dbec5f58472..f910e730f93 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -554,6 +554,18 @@ jobs: -TimeoutSeconds 300 ` -CargoArgs @('test', '--locked', '--target', 'x86_64-pc-windows-msvc', '-p', 'jcode-base', '--no-default-features', '--lib', 'background::tests::adopted_task_', '--', '--nocapture') + - name: Run Windows launcher backup sweep test + shell: pwsh + run: | + # `platform_support.rs` is `#[cfg(windows)]`, so its sweep regression can + # only execute on a Windows runner. Without this step the test is + # compiled on Windows but never run, which is the same + # looks-green-but-never-ran shape the secret-input cohort above calls out. + & ./scripts/invoke_cargo_with_timeout.ps1 ` + -Name 'Windows launcher backup sweep test' ` + -TimeoutSeconds 300 ` + -CargoArgs @('test', '--locked', '--target', 'x86_64-pc-windows-msvc', '-p', 'jcode-build-support', '--lib', 'sweep_removes_only_old_launcher_backups', '--', '--nocapture') + - name: Run Windows e2e smoke tests shell: pwsh run: | diff --git a/crates/jcode-build-support/src/platform_support.rs b/crates/jcode-build-support/src/platform_support.rs index 07aaa7634d5..cb1935bc1ef 100644 --- a/crates/jcode-build-support/src/platform_support.rs +++ b/crates/jcode-build-support/src/platform_support.rs @@ -1,4 +1,17 @@ +// reference: https://doc.rust-lang.org/std/fs/fn.read_dir.html and +// https://doc.rust-lang.org/std/fs/fn.remove_file.html (Rust std directory +// enumeration and best-effort removal semantics used by the sweep helper). use std::path::Path; +#[cfg(windows)] +use std::time::{Duration, SystemTime}; + +/// Prefix of the aside backup created for a live launcher during a Windows swap. +#[cfg(windows)] +const LAUNCHER_BACKUP_PREFIX: &str = ".jcode-launcher-old-"; + +/// Backups older than this are considered abandoned and get swept away. +#[cfg(windows)] +const STALE_LAUNCHER_BACKUP_AGE: Duration = Duration::from_secs(60 * 60); /// Set file permissions to owner read/write/execute (0o755). /// No-op on Windows (executability is determined by file extension). @@ -40,6 +53,12 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu let _ = std::fs::remove_file(temp); std::fs::copy(src, temp)?; + // Sweep leftovers from earlier publishes before adding a new one. + sweep_stale_launcher_backups( + dst.parent().unwrap_or_else(|| Path::new(".")), + STALE_LAUNCHER_BACKUP_AGE, + ); + let operation_id = format!( "{}-{}", std::process::id(), @@ -49,7 +68,7 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu .unwrap_or_default() ); let old = dst.parent().unwrap_or_else(|| Path::new(".")).join(format!( - ".jcode-launcher-old-{operation_id}{}", + "{LAUNCHER_BACKUP_PREFIX}{operation_id}{}", dst.extension() .map(|extension| format!(".{}", extension.to_string_lossy())) .unwrap_or_default() @@ -75,7 +94,7 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu } // An old loaded executable cannot be deleted until its process exits. - // Best-effort cleanup is safe; later installs can remove leftovers. + // Best-effort cleanup is safe; the sweep above removes older leftovers. if moved_old { let _ = std::fs::remove_file(old); } @@ -83,9 +102,74 @@ pub fn atomic_symlink_swap(src: &Path, dst: &Path, temp: &Path) -> std::io::Resu Ok(()) } +/// Delete stale `.jcode-launcher-old-*` backups left in `dir` by earlier swaps. +/// +/// A backup is only removed when it is older than `max_age`; a file still held +/// open by a running process fails to delete on Windows and is skipped. Every +/// failure is ignored: the sweep must never break a publish. +#[cfg(windows)] +fn sweep_stale_launcher_backups(dir: &Path, max_age: Duration) { + let Ok(entries) = std::fs::read_dir(dir) else { + return; + }; + let now = SystemTime::now(); + for entry in entries.flatten() { + let file_name = entry.file_name(); + let Some(file_name) = file_name.to_str() else { + continue; + }; + if !file_name.starts_with(LAUNCHER_BACKUP_PREFIX) { + continue; + } + let Ok(metadata) = entry.metadata() else { + continue; + }; + if !metadata.is_file() { + continue; + } + if !backup_is_stale(file_name, now, max_age) { + continue; + } + // Best-effort removal: a backup still held open by a running process + // fails to delete on Windows, and that must not abort the sweep. + if std::fs::remove_file(entry.path()).is_err() { + continue; + } + } +} + +/// Whether a launcher backup is old enough to be swept. +/// +/// The backup name carries the operation timestamp, so its age does not depend +/// on the mtime inherited from the renamed executable. +#[cfg(windows)] +fn backup_is_stale(file_name: &str, now: SystemTime, max_age: Duration) -> bool { + let Some(created_at) = backup_operation_time(file_name) else { + return false; + }; + match now.duration_since(created_at) { + Ok(age) => age >= max_age, + Err(_) => false, + } +} + +/// Extract the operation timestamp encoded in `.jcode-launcher-old--`. +#[cfg(windows)] +fn backup_operation_time(file_name: &str) -> Option { + let rest = file_name.strip_prefix(LAUNCHER_BACKUP_PREFIX)?; + let stem = rest.rsplit_once('.').map(|(stem, _)| stem).unwrap_or(rest); + let (_, nanos) = stem.rsplit_once('-')?; + let nanos: u64 = match nanos.parse() { + Ok(value) => value, + Err(_) => return None, + }; + Some(SystemTime::UNIX_EPOCH + Duration::from_nanos(nanos)) +} + #[cfg(all(test, windows))] mod tests { - use super::atomic_symlink_swap; + use super::{atomic_symlink_swap, sweep_stale_launcher_backups}; + use std::time::{Duration, SystemTime}; #[test] fn windows_swap_replaces_existing_launcher_via_staged_file() { @@ -106,4 +190,31 @@ mod tests { .exists() ); } + + #[test] + fn sweep_removes_only_old_launcher_backups() { + let dir = tempfile::tempdir().expect("temporary directory"); + let now_nanos = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("clock") + .as_nanos(); + let old_nanos = now_nanos - Duration::from_secs(2 * 60 * 60).as_nanos(); + + let old_backup = dir + .path() + .join(format!(".jcode-launcher-old-1234-{old_nanos}.exe")); + let fresh_backup = dir + .path() + .join(format!(".jcode-launcher-old-5678-{now_nanos}.exe")); + let unrelated = dir.path().join("jcode.exe"); + for path in [&old_backup, &fresh_backup, &unrelated] { + std::fs::write(path, b"payload").expect("fixture"); + } + + sweep_stale_launcher_backups(dir.path(), Duration::from_secs(60 * 60)); + + assert!(!old_backup.exists(), "stale backup must be removed"); + assert!(fresh_backup.exists(), "fresh backup must be kept"); + assert!(unrelated.exists(), "unrelated file must be kept"); + } } From 91871800e05b3d9d809d229f49622cc02c21bb24 Mon Sep 17 00:00:00 2001 From: mac Date: Sun, 8 Nov 2026 03:51:40 -0800 Subject: [PATCH 098/236] fix(tui): emit OSC 52 before native clipboard in SSH sessions (#2078) --- crates/jcode-tui/src/tui/app/helpers.rs | 329 +++++++++++++++++------- 1 file changed, 231 insertions(+), 98 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/helpers.rs b/crates/jcode-tui/src/tui/app/helpers.rs index 543d19898cf..98e8b00b198 100644 --- a/crates/jcode-tui/src/tui/app/helpers.rs +++ b/crates/jcode-tui/src/tui/app/helpers.rs @@ -431,11 +431,231 @@ pub(crate) fn stop_capturing_clipboard_for_tests() { } } -/// Copy text to clipboard. On Windows and macOS, the native clipboard API -/// (arboard) is authoritative, with OSC 52 as a remote-session fallback. -/// Elsewhere, try wl-copy (Wayland), then xclip/xsel (X11, which keep owning -/// the selection unlike arboard), then arboard, then OSC 52 as the -/// remote-session fallback (SSH / Docker / tmux). +/// True when this process is running inside an SSH session, i.e. the user's +/// machine (and clipboard) is elsewhere. In that case the *local* clipboard +/// belongs to the server, and the only path to the user's clipboard is an +/// OSC 52 sequence emitted through the terminal (and forwarded by the SSH +/// client to the client-side terminal emulator). +#[cfg_attr(test, allow(dead_code))] +fn is_remote_ssh_session() -> bool { + std::env::var("SSH_CONNECTION").is_ok() || std::env::var("SSH_TTY").is_ok() +} + +/// Runs the session-appropriate copy chain and reports its outcome. +/// +/// The *order* is the contract (it is what the original bug violated): +/// +/// - **Remote (SSH) session**: OSC 52 runs first, because it is the only path +/// to the user's (client-side) clipboard, and the native server-side +/// clipboard (NSPasteboard / Win32) would otherwise succeed headless and +/// short-circuit the chain. The local clipboard is then also filled so +/// paste inside jcode keeps working. If OSC 52 is unavailable (e.g. stdout +/// is not a TTY), the local clipboard is still the fallback. +/// - **Local session**: the native clipboard is authoritative and OSC 52 only +/// runs if it fails (a working native clipboard must not emit terminal +/// escape sequences). +/// +/// The writers are closures so the order can be unit-tested with stubs, +/// without touching any real clipboard. +fn run_copy_chain( + remote: bool, + mut osc52: impl FnMut() -> bool, + mut local: impl FnMut() -> bool, +) -> bool { + if remote { + if osc52() { + // Keep the local clipboard in sync so paste inside jcode + // (which reads the server-side clipboard) keeps working. + let _ = local(); + return true; + } + // OSC 52 did not go through (e.g. stdout is not a TTY): still try the + // local clipboard rather than failing outright. + return local(); + } + if local() { + return true; + } + osc52() +} + +#[cfg(test)] +mod copy_chain_tests { + use super::run_copy_chain; + use std::cell::RefCell; + + /// Records the order in which the clipboard writers are invoked, so the + /// ordering contract of [`run_copy_chain`] is pinned by tests: a + /// regression that writes the server-side clipboard before OSC 52 in an + /// SSH session (the original bug) fails here. + struct OrderLog(RefCell>); + + impl OrderLog { + fn writer(&self, label: &'static str, succeeds: bool) -> impl FnMut() -> bool + '_ { + let log = &self.0; + move || { + log.borrow_mut().push(label); + succeeds + } + } + + fn events(&self) -> Vec<&'static str> { + self.0.borrow().clone() + } + } + + #[test] + fn ssh_session_writes_osc52_first_then_local() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + true, + log.writer("osc52", true), + log.writer("local", true), + )); + assert_eq!(log.events(), ["osc52", "local"]); + } + + #[test] + fn ssh_session_still_writes_local_when_osc52_fails() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + true, + log.writer("osc52", false), + log.writer("local", true), + )); + assert_eq!(log.events(), ["osc52", "local"]); + } + + #[test] + fn ssh_session_reports_failure_when_both_writers_fail() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(!run_copy_chain( + true, + log.writer("osc52", false), + log.writer("local", false), + )); + assert_eq!(log.events(), ["osc52", "local"]); + } + + #[test] + fn local_session_prefers_native_clipboard_and_skips_osc52() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + false, + log.writer("osc52", true), + log.writer("local", true), + )); + // A working native clipboard must not emit terminal escape sequences. + assert_eq!(log.events(), ["local"]); + } + + #[test] + fn local_session_falls_back_to_osc52_when_native_fails() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(run_copy_chain( + false, + log.writer("osc52", true), + log.writer("local", false), + )); + assert_eq!(log.events(), ["local", "osc52"]); + } + + #[test] + fn local_session_reports_failure_when_both_writers_fail() { + let log = OrderLog(RefCell::new(Vec::new())); + assert!(!run_copy_chain( + false, + log.writer("osc52", false), + log.writer("local", false), + )); + assert_eq!(log.events(), ["local", "osc52"]); + } +} + +/// Fill the *local* (server-side) clipboard using the platform-native chain. +/// +/// - Windows: Win32 via arboard, which is authoritative there (issue #497). +/// - macOS: NSPasteboard via arboard, with pbcopy as a belt-and-braces +/// fallback (Terminal.app ignores OSC 52, so the native path must win +/// locally). +/// - Linux: wl-copy (Wayland), then xclip/xsel (X11, which fork and keep +/// owning the selection unlike arboard, whose X11 backend reverts the +/// selection on drop - issue #684), then arboard. +#[cfg(not(test))] +fn copy_to_local_clipboard(text: &str) -> bool { + #[cfg(windows)] + { + arboard::Clipboard::new() + .and_then(|mut cb| cb.set_text(text.to_string())) + .is_ok() + } + + #[cfg(target_os = "macos")] + { + copy_to_local_macos_clipboard(text) + } + + #[cfg(not(any(windows, target_os = "macos")))] + { + if clipboard_helper::copy_via_clipboard_helper("wl-copy", &[], text) { + return true; + } + if clipboard_helper::copy_via_clipboard_helper( + "xclip", + &["-selection", "clipboard"], + text, + ) { + return true; + } + if clipboard_helper::copy_via_clipboard_helper( + "xsel", + &["--clipboard", "--input"], + text, + ) { + return true; + } + arboard::Clipboard::new() + .and_then(|mut cb| cb.set_text(text.to_string())) + .is_ok() + } +} + +#[cfg(all(not(test), target_os = "macos"))] +fn copy_to_local_macos_clipboard(text: &str) -> bool { + if arboard::Clipboard::new() + .and_then(|mut cb| cb.set_text(text.to_string())) + .is_ok() + { + return true; + } + if let Ok(mut child) = std::process::Command::new("pbcopy") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + { + use std::io::Write; + if let Some(stdin) = child.stdin.as_mut() + && stdin.write_all(text.as_bytes()).is_ok() + { + drop(child.stdin.take()); + if child.wait().map(|s| s.success()).unwrap_or(false) { + return true; + } + } + } + false +} + +/// Copy text to clipboard. +/// +/// The ordering contract lives in [`run_copy_chain`]: in a remote (SSH) +/// session OSC 52 is emitted first, because the platform-native clipboards +/// succeed even headless and would otherwise short-circuit the only path to +/// the user's own clipboard; the local clipboard is then also filled so +/// paste inside jcode keeps working. In a local session the native clipboard +/// is authoritative and OSC 52 is the final fallback (see +/// [`copy_to_local_clipboard`] for the per-platform native chain). pub(super) fn copy_to_clipboard(text: &str) -> bool { // Under test, never touch the OS clipboard. Beyond making results identical // on a desktop and a headless runner, the Linux path below spawns `wl-copy`, @@ -462,102 +682,15 @@ pub(super) fn copy_to_clipboard(text: &str) -> bool { #[cfg(not(test))] { - // On Windows, the native clipboard API must run before OSC 52. Writing an - // OSC 52 sequence to stdout "succeeds" even when the console (conhost, - // older Windows Terminal) silently ignores it, which reported "Copied" - // while leaving the clipboard empty (issue #497). arboard talks to the - // Win32 clipboard directly and is authoritative there. - #[cfg(windows)] - { - if arboard::Clipboard::new() - .and_then(|mut cb| cb.set_text(text.to_string())) - .is_ok() - { - return true; - } - return copy_to_clipboard_osc52(text); - } - - // Same class of bug on macOS: Apple Terminal (Terminal.app) silently - // ignores OSC 52, yet writing the sequence to stdout "succeeds", so we - // reported "Copied" while leaving the clipboard untouched. NSPasteboard - // via arboard (with pbcopy as a belt-and-braces fallback) is authoritative - // for local sessions; OSC 52 remains as the final remote-session fallback. - #[cfg(target_os = "macos")] - { - if arboard::Clipboard::new() - .and_then(|mut cb| cb.set_text(text.to_string())) - .is_ok() - { - return true; - } - if let Ok(mut child) = std::process::Command::new("pbcopy") - .stdin(std::process::Stdio::piped()) - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::null()) - .spawn() - { - use std::io::Write; - if let Some(stdin) = child.stdin.as_mut() - && stdin.write_all(text.as_bytes()).is_ok() - { - drop(child.stdin.take()); - if child.wait().map(|s| s.success()).unwrap_or(false) { - return true; - } - } - } - copy_to_clipboard_osc52(text) - } - - // Linux has the same failure class (issue #504, Kali/X11): wl-copy fails - // outside Wayland, and many terminals (xterm, older VTE) silently ignore - // OSC 52 while the stdout write still "succeeds", so the arboard fallback - // never ran. Prefer native clipboards when a display is available: - // wl-copy (Wayland), then xclip/xsel (X11, which keep owning the - // selection), then arboard, and only then OSC 52 for genuinely - // headless/remote sessions (SSH, Docker, tmux) where the native paths - // fail fast for lack of a display server. - #[cfg(not(any(windows, target_os = "macos")))] - { - if clipboard_helper::copy_via_clipboard_helper("wl-copy", &[], text) { - return true; - } - // X11: prefer xclip/xsel over arboard. arboard's X11 backend sets the - // selection on a connection it owns and then closes it when the - // `Clipboard` is dropped, so the selection owner disappears and the - // clipboard silently reverts (issue #684) even though `set_text` - // returned Ok. xclip and xsel fork a background process that keeps - // owning the selection until a paste, which is what users expect. - if clipboard_helper::copy_via_clipboard_helper( - "xclip", - &["-selection", "clipboard"], - text, - ) { - return true; - } - if clipboard_helper::copy_via_clipboard_helper( - "xsel", - &["--clipboard", "--input"], - text, - ) { - return true; - } - if arboard::Clipboard::new() - .and_then(|mut cb| cb.set_text(text.to_string())) - .is_ok() - { - return true; - } - copy_to_clipboard_osc52(text) - } + run_copy_chain( + is_remote_ssh_session(), + || copy_to_clipboard_osc52(text), + || copy_to_local_clipboard(text), + ) } } -/// Copy to clipboard using the OSC 52 terminal escape sequence. This asks the -/// terminal emulator to set the system clipboard without needing a local -/// display server, making it work over SSH, inside Docker, and under tmux -/// (with `set -g set-clipboard on`). Returns false if stdout is not a TTY. +/// Copy text to the clipboard using the OSC 52 terminal escape sequence. #[cfg_attr(test, allow(dead_code))] fn copy_to_clipboard_osc52(text: &str) -> bool { use base64::Engine as _; From 11800ccb774c7993c5e1d58791033aa6b333ee92 Mon Sep 17 00:00:00 2001 From: jamesrern Date: Sun, 8 Nov 2026 03:51:41 -0800 Subject: [PATCH 099/236] fix(providers issue #1939): accept http base URLs with any IP address (#2018) --- crates/jcode-base/src/provider_catalog.rs | 23 ++++--- .../jcode-base/src/provider_catalog_tests.rs | 15 +++++ crates/jcode-provider-metadata/src/lib.rs | 63 ++++++++++++------- crates/jcode-tui/src/tui/app/auth.rs | 4 +- .../src/tui/app/auth_account_commands.rs | 2 +- 5 files changed, 73 insertions(+), 34 deletions(-) diff --git a/crates/jcode-base/src/provider_catalog.rs b/crates/jcode-base/src/provider_catalog.rs index 0ed8e68618d..d1cb708ef8f 100644 --- a/crates/jcode-base/src/provider_catalog.rs +++ b/crates/jcode-base/src/provider_catalog.rs @@ -21,13 +21,18 @@ pub fn api_base_uses_localhost(raw: &str) -> bool { return false; }; - matches!( - parsed - .host_str() - .map(|host| host.to_ascii_lowercase()) - .as_deref(), - Some("localhost") | Some("127.0.0.1") | Some("::1") - ) + let Some(host) = parsed.host_str().map(|host| host.to_ascii_lowercase()) else { + return false; + }; + + if host == "localhost" || host.ends_with(".local") { + return true; + } + + // A bare IP names one machine, like loopback: a self-hosted inference + // server reached by address needs no key by default, whether that address + // is private or public. + ip_addr_from_host(&host).is_some() } pub fn resolve_openai_compatible_profile( @@ -96,7 +101,7 @@ fn resolve_openai_compatible_profile_endpoint( resolved.api_base = normalized; } else { eprintln!( - "Warning: ignoring invalid JCODE_OPENAI_COMPAT_API_BASE '{}'. Use https://... (or http://localhost).", + "Warning: ignoring invalid JCODE_OPENAI_COMPAT_API_BASE '{}'. Use https://..., or http:// with a localhost/.local name or a bare IP address.", base ); } @@ -941,7 +946,7 @@ pub fn apply_named_provider_profile_env_from_config( let api_base = normalize_api_base(&profile.base_url).ok_or_else(|| { anyhow::anyhow!( - "Provider profile '{}' has invalid base_url '{}'. Use https://... or http://localhost.", + "Provider profile '{}' has invalid base_url '{}'. Use https://..., or http:// with a localhost/.local name or a bare IP address.", profile_name, profile.base_url ) diff --git a/crates/jcode-base/src/provider_catalog_tests.rs b/crates/jcode-base/src/provider_catalog_tests.rs index a59b9c8cbbb..d6ada97c0bd 100644 --- a/crates/jcode-base/src/provider_catalog_tests.rs +++ b/crates/jcode-base/src/provider_catalog_tests.rs @@ -369,6 +369,21 @@ fn auth_issue_lan_openai_compatible_bases_are_valid_for_local_model_servers() { Some("http://hsv.local:11434/v1") ); assert_eq!(normalize_api_base("http://example.com/v1"), None); + // Any IP literal is valid over plaintext http, private or public. + assert_eq!( + normalize_api_base("http://203.0.113.10:8000/v1").as_deref(), + Some("http://203.0.113.10:8000/v1") + ); + assert_eq!( + normalize_api_base("http://[2001:db8::1]:8000/v1").as_deref(), + Some("http://[2001:db8::1]:8000/v1") + ); + // A bare IP is a direct endpoint on one machine, so it needs no key by + // default, like loopback. + assert!(api_base_uses_localhost("http://127.0.0.1:11434/v1")); + assert!(api_base_uses_localhost("http://10.15.15.53:8080/v1")); + assert!(api_base_uses_localhost("http://203.0.113.10:8080/v1")); + assert!(!api_base_uses_localhost("https://api.openai.com/v1")); } #[test] diff --git a/crates/jcode-provider-metadata/src/lib.rs b/crates/jcode-provider-metadata/src/lib.rs index a45731168b2..ff6992045c2 100644 --- a/crates/jcode-provider-metadata/src/lib.rs +++ b/crates/jcode-provider-metadata/src/lib.rs @@ -283,27 +283,24 @@ fn allows_insecure_http_host(host: &str) -> bool { return true; } - if let Ok(ip) = host.parse::() { - return match ip { - std::net::IpAddr::V4(v4) => { - let raw = u32::from(v4); - let is_carrier_grade_nat = (raw & 0xffc0_0000) == 0x6440_0000; - v4.is_loopback() - || v4.is_private() - || v4.is_link_local() - || v4.is_unspecified() - || is_carrier_grade_nat - } - std::net::IpAddr::V6(v6) => { - v6.is_loopback() - || v6.is_unique_local() - || v6.is_unicast_link_local() - || v6.is_unspecified() - } - }; - } + // Any IPv4/IPv6 literal is accepted for plaintext http. A bare address + // already names one machine, which is how self-hosted inference servers + // are reached: on the LAN (`10.15.15.53`), in a container network, or on a + // cloud host with a public address. Restricting the answer to RFC 1918 + // ranges only forced those servers behind an extra https proxy. + ip_addr_from_host(&host_lower).is_some() +} - false +/// Parse a URL host as an IP literal, tolerating brackets and an IPv6 zone id +/// (`[fe80::1%25eth0]` becomes `fe80::1`, which `IpAddr` cannot parse itself). +pub fn ip_addr_from_host(host: &str) -> Option { + let host = host.trim(); + let host = host + .strip_prefix('[') + .and_then(|s| s.strip_suffix(']')) + .unwrap_or(host); + // `IpAddr` rejects zone ids, so compare on the address part only. + host.split('%').next().and_then(|host| host.parse().ok()) } fn normalize_provider_input(input: &str) -> Option { @@ -389,9 +386,31 @@ mod tests { } #[test] - fn normalize_api_base_rejects_public_http_hosts() { + fn normalize_api_base_accepts_any_ip_http_host() { + // A bare IP names a single machine, so plaintext http is allowed even + // when that address is public. + assert_eq!( + normalize_api_base("http://8.8.8.8/v1").as_deref(), + Some("http://8.8.8.8/v1") + ); + assert_eq!( + normalize_api_base("http://203.0.113.10:8080/v1/").as_deref(), + Some("http://203.0.113.10:8080/v1") + ); + assert_eq!( + normalize_api_base("http://[2001:db8::1]:8080/v1").as_deref(), + Some("http://[2001:db8::1]:8080/v1") + ); + assert_eq!( + normalize_api_base("http://[fe80::1%25eth0]:8080/v1").as_deref(), + Some("http://[fe80::1%25eth0]:8080/v1") + ); + // Named hosts still need a local name or https. assert_eq!(normalize_api_base("http://example.com/v1"), None); - assert_eq!(normalize_api_base("http://8.8.8.8/v1"), None); + assert_eq!( + normalize_api_base("https://example.com/v1").as_deref(), + Some("https://example.com/v1") + ); } #[test] diff --git a/crates/jcode-tui/src/tui/app/auth.rs b/crates/jcode-tui/src/tui/app/auth.rs index 84f1feb443b..780f3f09acc 100644 --- a/crates/jcode-tui/src/tui/app/auth.rs +++ b/crates/jcode-tui/src/tui/app/auth.rs @@ -2369,7 +2369,7 @@ impl App { Some(value) => value, None => { self.push_display_message(DisplayMessage::error( - "OpenAI-compatible API base must be https://... or http://localhost." + "OpenAI-compatible API base must be https://..., or http:// with a localhost/.local name or a bare IP address." .to_string(), )); self.pending_login = @@ -3404,7 +3404,7 @@ fn save_tui_openai_compatible_api_base( let trimmed = api_base.trim(); if !trimmed.is_empty() { let normalized = crate::provider_catalog::normalize_api_base(trimmed).ok_or_else(|| { - anyhow::anyhow!("OpenAI-compatible API base must be https://... or http://localhost.") + anyhow::anyhow!("OpenAI-compatible API base must be https://..., or http:// with a localhost/.local name or a bare IP address.") })?; crate::provider_catalog::save_env_value_to_env_file( "JCODE_OPENAI_COMPAT_API_BASE", diff --git a/crates/jcode-tui/src/tui/app/auth_account_commands.rs b/crates/jcode-tui/src/tui/app/auth_account_commands.rs index d0c1fd24d3b..e9ba45dc8dc 100644 --- a/crates/jcode-tui/src/tui/app/auth_account_commands.rs +++ b/crates/jcode-tui/src/tui/app/auth_account_commands.rs @@ -968,7 +968,7 @@ fn save_openai_compat_setting(app: &mut App, setting: OpenAiCompatSetting, value Some(value) => Some(value), None => { app.push_display_message(DisplayMessage::error( - "OpenAI-compatible API base must be https://... or http://localhost." + "OpenAI-compatible API base must be https://..., or http:// with a localhost/.local name or a bare IP address." .to_string(), )); return; From 398215987bac56165d5dc4829db81de6e7dbc959 Mon Sep 17 00:00:00 2001 From: Aafimalek <91587101+Aafimalek@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:51:56 -0800 Subject: [PATCH 100/236] feat(tui): copy session ID from picker (#2063) --- crates/jcode-tui/src/tui/app/helpers.rs | 2 +- crates/jcode-tui/src/tui/session_picker.rs | 24 +++++ .../src/tui/session_picker/render.rs | 8 +- .../jcode-tui/src/tui/session_picker_tests.rs | 100 ++++++++++++++++++ 4 files changed, 130 insertions(+), 4 deletions(-) diff --git a/crates/jcode-tui/src/tui/app/helpers.rs b/crates/jcode-tui/src/tui/app/helpers.rs index 98e8b00b198..6375e69b04c 100644 --- a/crates/jcode-tui/src/tui/app/helpers.rs +++ b/crates/jcode-tui/src/tui/app/helpers.rs @@ -656,7 +656,7 @@ fn copy_to_local_macos_clipboard(text: &str) -> bool { /// paste inside jcode keeps working. In a local session the native clipboard /// is authoritative and OSC 52 is the final fallback (see /// [`copy_to_local_clipboard`] for the per-platform native chain). -pub(super) fn copy_to_clipboard(text: &str) -> bool { +pub(crate) fn copy_to_clipboard(text: &str) -> bool { // Under test, never touch the OS clipboard. Beyond making results identical // on a desktop and a headless runner, the Linux path below spawns `wl-copy`, // which forks a clipboard server that does not exit; waiting on it hangs the diff --git a/crates/jcode-tui/src/tui/session_picker.rs b/crates/jcode-tui/src/tui/session_picker.rs index bd8061a73bb..43aceb73251 100644 --- a/crates/jcode-tui/src/tui/session_picker.rs +++ b/crates/jcode-tui/src/tui/session_picker.rs @@ -298,6 +298,8 @@ pub struct SessionPicker { search_query: String, /// Whether we're in search input mode search_active: bool, + /// Feedback from the most recent clipboard action, cleared on the next key. + copy_notice: Option<&'static str>, /// Hidden test session count (debug + canary) hidden_test_count: usize, /// Which pane has keyboard focus @@ -401,6 +403,7 @@ impl SessionPicker { filter_mode: SessionFilterMode::All, search_query: String::new(), search_active: false, + copy_notice: None, hidden_test_count, focus: PaneFocus::Sessions, selected_session_ids: HashSet::new(), @@ -462,6 +465,7 @@ impl SessionPicker { filter_mode: SessionFilterMode::All, search_query: String::new(), search_active: false, + copy_notice: None, hidden_test_count: 0, focus: PaneFocus::Sessions, selected_session_ids: HashSet::new(), @@ -555,6 +559,7 @@ impl SessionPicker { filter_mode: SessionFilterMode::All, search_query: String::new(), search_active: false, + copy_notice: None, hidden_test_count, focus: PaneFocus::Sessions, selected_session_ids: HashSet::new(), @@ -893,6 +898,21 @@ impl SessionPicker { }) } + /// Copy the highlighted session's resume identifier and report clipboard errors. + fn copy_selected_session_with(&mut self, copy: impl FnOnce(&str) -> bool) { + let Some(session) = self.selected_session() else { + self.copy_notice = Some("No session selected"); + return; + }; + let is_path = matches!(session.resume_target, ResumeTarget::PiSession { .. }); + let copied = copy(session.resume_target.stable_id()); + self.copy_notice = Some(match (copied, is_path) { + (true, true) => "Session path copied", + (true, false) => "Session ID copied", + (false, _) => "Could not copy to clipboard", + }); + } + pub fn session_for_target(&self, target: &ResumeTarget) -> Option<&SessionInfo> { self.visible_sessions .iter() @@ -1437,6 +1457,7 @@ impl SessionPicker { code: KeyCode, modifiers: KeyModifiers, ) -> Result { + self.copy_notice = None; if let Some(action) = self.handle_claude_takeover_confirmation_key(code, modifiers) { return Ok(action); } @@ -1504,6 +1525,9 @@ impl SessionPicker { return Ok(OverlayAction::Close); } KeyCode::Char('q') => return Ok(OverlayAction::Close), + KeyCode::Char('y') if modifiers.is_empty() => { + self.copy_selected_session_with(super::app::helpers::copy_to_clipboard); + } KeyCode::Char(' ') => { self.toggle_selected_session(); } diff --git a/crates/jcode-tui/src/tui/session_picker/render.rs b/crates/jcode-tui/src/tui/session_picker/render.rs index 5f0987edd32..08e029d95e6 100644 --- a/crates/jcode-tui/src/tui/session_picker/render.rs +++ b/crates/jcode-tui/src/tui/session_picker/render.rs @@ -687,7 +687,9 @@ impl SessionPicker { title_parts.push(Span::styled(" ", Style::default())); - let mut help = if self.search_submission_pending() { + let mut help = if let Some(notice) = self.copy_notice { + format!(" {notice} ") + } else if self.search_submission_pending() { " confirming best match… · waits ≤1s · ↑↓ take control · Esc cancel ".to_string() } else if self.search_active && self.loading_message.is_some() { " type to filter · loading… · Esc clear · Ctrl+C cancel ".to_string() @@ -702,10 +704,10 @@ impl SessionPicker { } else { match crate::config::config().keybindings.session_picker_enter { crate::config::SessionPickerResumeAction::CurrentTerminal => { - " Space select · Enter in place · Ctrl+Enter new terminal · d debug · / search · h/l focus · ↑↓ · q ".to_string() + " y copy ID · Space select · Enter in place · Ctrl+Enter new terminal · d debug · / search · h/l focus · ↑↓ · q ".to_string() } crate::config::SessionPickerResumeAction::NewTerminal => { - " Space select · Enter new terminal · Ctrl+Enter in place · d debug · / search · h/l focus · ↑↓ · q ".to_string() + " y copy ID · Space select · Enter new terminal · Ctrl+Enter in place · d debug · / search · h/l focus · ↑↓ · q ".to_string() } } }; diff --git a/crates/jcode-tui/src/tui/session_picker_tests.rs b/crates/jcode-tui/src/tui/session_picker_tests.rs index c83ac73c2c8..97b5793521e 100644 --- a/crates/jcode-tui/src/tui/session_picker_tests.rs +++ b/crates/jcode-tui/src/tui/session_picker_tests.rs @@ -34,6 +34,106 @@ fn make_session(id: &str, short_name: &str, is_debug: bool, status: SessionStatu make_session_with_flags(id, short_name, is_debug, false, status) } +/// Copy the highlighted session without changing selection and report failures. +#[test] +fn copy_session_identifier_reports_success_and_failure() { + let session = make_session("session_alpha", "alpha", false, SessionStatus::Closed); + let mut picker = SessionPicker::new(vec![session]); + let mut copied = String::new(); + picker.copy_selected_session_with(|id| { + copied = id.to_owned(); + true + }); + assert_eq!(copied, "session_alpha"); + assert_eq!(picker.copy_notice, Some("Session ID copied")); + assert_eq!(picker.selected_session().unwrap().id, "session_alpha"); + picker.copy_selected_session_with(|_| false); + assert_eq!(picker.copy_notice, Some("Could not copy to clipboard")); +} + +/// Copy the actual external identifier and label Pi's path accurately. +#[test] +fn copy_external_session_uses_resume_target() { + let mut session = make_session("internal-id", "external", false, SessionStatus::Closed); + session.resume_target = ResumeTarget::PiSession { + session_path: "pi/session.jsonl".into(), + }; + let mut picker = SessionPicker::new(vec![session]); + picker.copy_selected_session_with(|id| { + assert_eq!(id, "pi/session.jsonl"); + true + }); + assert_eq!(picker.copy_notice, Some("Session path copied")); +} + +/// Search still accepts y as text and the copy action handles an empty list. +#[test] +fn copy_shortcut_does_not_interfere_with_search_or_empty_picker() { + let mut picker = SessionPicker::new(Vec::new()); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + assert_eq!(picker.copy_notice, Some("No session selected")); + picker + .handle_overlay_key(KeyCode::Char('/'), KeyModifiers::empty()) + .unwrap(); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + assert_eq!(picker.search_query, "y"); + assert_eq!(picker.copy_notice, None); +} + +/// The picker shortcut invokes the shared clipboard implementation. +#[test] +fn copy_shortcut_copies_highlighted_session() { + let _lock = crate::tui::ui::render_state_test_lock(); + let mut picker = SessionPicker::new(vec![make_session( + "session_alpha", + "alpha", + false, + SessionStatus::Closed, + )]); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + assert_eq!(picker.copy_notice, Some("Session ID copied")); +} + +/// The picker advertises the shortcut and shows the result in its footer. +#[test] +fn copy_shortcut_and_result_are_visible() { + let _lock = crate::tui::ui::render_state_test_lock(); + let mut picker = SessionPicker::new(vec![make_session( + "session_alpha", + "alpha", + false, + SessionStatus::Closed, + )]); + let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 40)).unwrap(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + let text: String = terminal + .backend() + .buffer() + .content() + .iter() + .map(|cell| cell.symbol()) + .collect(); + assert!(text.contains("y copy ID")); + picker + .handle_overlay_key(KeyCode::Char('y'), KeyModifiers::empty()) + .unwrap(); + terminal.draw(|frame| picker.render(frame)).unwrap(); + let text: String = terminal + .backend() + .buffer() + .content() + .iter() + .map(|cell| cell.symbol()) + .collect(); + assert!(text.contains("Session ID copied")); +} + #[path = "session_picker_tests/identity_search.rs"] mod identity_search; From b8e185783e21cb4157617bf69b19312d15b0710e Mon Sep 17 00:00:00 2001 From: Fardjad Davari Date: Sun, 8 Nov 2026 03:51:57 -0800 Subject: [PATCH 101/236] fix: preserve runtime context when opening swarm agents (#2080) --- crates/jcode-app-core/src/session_launch.rs | 247 ++++++++++++- crates/jcode-base/src/companion_client.rs | 227 ++++++++++++ crates/jcode-base/src/terminal_launch.rs | 65 +++- crates/jcode-companion/src/server.rs | 327 +++++++++++++++-- crates/jcode-terminal-launch/src/lib.rs | 345 ++++++++++++++---- .../src/windows_portable_tests.rs | 62 +++- .../src/tui/app/swarm_panel_key_tests.rs | 228 +++++++++++- crates/jcode-tui/src/tui/app/tui_state.rs | 138 ++++++- docs/SPAWN_HOOK.md | 24 ++ 9 files changed, 1536 insertions(+), 127 deletions(-) diff --git a/crates/jcode-app-core/src/session_launch.rs b/crates/jcode-app-core/src/session_launch.rs index 6621b3430a1..0c8dfef3145 100644 --- a/crates/jcode-app-core/src/session_launch.rs +++ b/crates/jcode-app-core/src/session_launch.rs @@ -45,6 +45,8 @@ pub struct SessionSpawnContext { /// hooks so the new window lands in the client's terminal instead of the /// server's stale startup env (#405). pub client_terminal_env: Vec<(String, String)>, + /// Explicit non-secret runtime variables for the child process. + pub runtime_env: Vec<(String, String)>, } impl SessionSpawnContext { @@ -52,6 +54,7 @@ impl SessionSpawnContext { Self { kind: Some(kind.into()), extra_env: Vec::new(), + runtime_env: Vec::new(), client_terminal_env: Vec::new(), } } @@ -67,6 +70,41 @@ impl SessionSpawnContext { self } + /// Attach the explicit runtime env sent only to the spawned jcode child. + pub fn with_runtime_env(mut self, env: Vec<(String, String)>) -> Self { + self.runtime_env = env; + self + } + + /// Capture effective local runtime values, not just inherited overrides. + /// The connected socket takes precedence over any stale `JCODE_SOCKET`. + pub fn with_local_runtime(mut self, connected_socket: &std::path::Path) -> Self { + let mut env = Vec::new(); + if let Ok(home) = crate::storage::jcode_dir() { + env.push(( + "JCODE_HOME".to_string(), + home.to_string_lossy().into_owned(), + )); + } + env.extend([ + ( + "JCODE_RUNTIME_DIR".to_string(), + crate::storage::runtime_dir().to_string_lossy().into_owned(), + ), + ( + "JCODE_SOCKET".to_string(), + connected_socket.to_string_lossy().into_owned(), + ), + ]); + for key in ["JCODE_API_SOCKET", "JCODE_NO_AUTO_UPDATE"] { + if let Ok(value) = std::env::var(key) { + env.push((key.to_string(), value)); + } + } + self.runtime_env = env; + self + } + fn apply( &self, mut command: crate::terminal_launch::TerminalCommand, @@ -79,6 +117,9 @@ impl SessionSpawnContext { for (key, value) in &self.extra_env { command = command.spawn_env(key.clone(), value.clone()); } + if !self.runtime_env.is_empty() { + command = command.runtime_env(self.runtime_env.clone()); + } if !self.client_terminal_env.is_empty() { command = command.client_terminal_env(self.client_terminal_env.clone()); } @@ -86,6 +127,202 @@ impl SessionSpawnContext { } } +#[cfg(test)] +mod runtime_context_tests { + use super::*; + + #[test] + fn app_core_runtime_allowlist_facade_is_portable() { + let _: &'static [&'static str] = crate::terminal_launch::RUNTIME_ENV_VARS; + assert!(crate::terminal_launch::RUNTIME_ENV_VARS.contains(&"JCODE_SOCKET")); + } + + fn value<'a>(context: &'a SessionSpawnContext, key: &str) -> Option<&'a str> { + context + .runtime_env + .iter() + .find(|(candidate, _)| candidate == key) + .map(|(_, value)| value.as_str()) + } + + fn run_isolated_context_case(mode: &str) -> std::process::Output { + let parent_home = + std::path::PathBuf::from(std::env::var_os("HOME").expect("isolated HOME")); + + let root = parent_home.join(format!( + "jcode-runtime-context-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::create_dir_all(&root).expect("create isolated runtime context directory"); + let home = root.join("home with spaces/it's 🧪"); + let runtime_dir = root.join("runtime dir/it's 🧪"); + let xdg_runtime_dir = root.join("xdg runtime/it's 🧪"); + let jcode_home = root.join("jcode home/it's 🧪"); + let api_socket = root.join("api socket.sock"); + let exe = std::env::current_exe().expect("test binary path"); + let mut command = std::process::Command::new(exe); + command + .args([ + "--exact", + "session_launch::runtime_context_tests::runtime_context_child", + "--nocapture", + ]) + .env_clear() + .env("HOME", &home) + .env( + "XDG_RUNTIME_DIR", + if mode == "custom" { + root.join("unused runtime/it's 🧪") + } else { + xdg_runtime_dir.clone() + }, + ) + .env("JCODE_SOCKET", root.join("stale socket.sock")) + .env("JCODE_CONTEXT_TEST_MODE", mode) + .env("JCODE_CONTEXT_TEST_HOME", &home) + .env("JCODE_CONTEXT_TEST_RUNTIME", &runtime_dir) + .env("JCODE_CONTEXT_TEST_JCODE_HOME", &jcode_home) + .env("JCODE_CONTEXT_TEST_XDG_RUNTIME", &xdg_runtime_dir) + .env("JCODE_CONTEXT_TEST_API_SOCKET", &api_socket); + if mode == "custom" { + command + .env("JCODE_HOME", &jcode_home) + .env("JCODE_RUNTIME_DIR", &runtime_dir); + } else { + command + .env("JCODE_API_SOCKET", &api_socket) + .env("JCODE_NO_AUTO_UPDATE", "1"); + } + let output = command.output().expect("run isolated context case"); + std::fs::remove_dir_all(&root).expect("remove isolated runtime context directory"); + output + } + + #[cfg(unix)] + #[test] + fn local_runtime_context_captures_custom_paths_and_connected_socket() { + let output = run_isolated_context_case("custom"); + assert!( + output.status.success(), + "stdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + } + + #[cfg(unix)] + #[test] + fn local_runtime_context_uses_effective_default_home_and_optional_values() { + let output = run_isolated_context_case("default"); + assert!( + output.status.success(), + "stdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + } + + #[cfg(unix)] + #[test] + fn runtime_context_child() { + let Ok(mode) = std::env::var("JCODE_CONTEXT_TEST_MODE") else { + return; + }; + let home = std::env::var("JCODE_CONTEXT_TEST_HOME").expect("isolated test home"); + let jcode_home = + std::env::var("JCODE_CONTEXT_TEST_JCODE_HOME").expect("isolated jcode home"); + let runtime_dir = + std::env::var("JCODE_CONTEXT_TEST_RUNTIME").expect("isolated runtime dir"); + let xdg_runtime_dir = + std::env::var("JCODE_CONTEXT_TEST_XDG_RUNTIME").expect("isolated XDG runtime dir"); + let api_socket = + std::env::var("JCODE_CONTEXT_TEST_API_SOCKET").expect("isolated API socket"); + let socket = std::path::Path::new(&home).join("connected socket/it's 🧪.sock"); + let context = SessionSpawnContext::default().with_local_runtime(&socket); + if mode == "custom" { + assert_eq!(value(&context, "JCODE_HOME"), Some(jcode_home.as_str())); + assert_eq!( + value(&context, "JCODE_RUNTIME_DIR"), + Some(runtime_dir.as_str()) + ); + + assert_eq!(value(&context, "JCODE_API_SOCKET"), None); + assert_eq!(value(&context, "JCODE_NO_AUTO_UPDATE"), None); + } else { + let expected_home = std::path::Path::new(&home).join(".jcode"); + assert_eq!( + value(&context, "JCODE_HOME"), + Some(expected_home.to_str().unwrap()) + ); + assert_eq!( + value(&context, "JCODE_RUNTIME_DIR"), + Some(xdg_runtime_dir.as_str()) + ); + assert_eq!( + value(&context, "JCODE_API_SOCKET"), + Some(api_socket.as_str()) + ); + assert_eq!(value(&context, "JCODE_NO_AUTO_UPDATE"), Some("1")); + } + assert_eq!( + value(&context, "JCODE_SOCKET"), + Some(socket.to_str().unwrap()) + ); + let socket_value = socket.to_string_lossy().into_owned(); + for session_id in ["ses active child", "ses completed child"] { + let args = resume_args(session_id, None, &context); + assert_eq!( + args, + vec![ + "--fresh-spawn".to_string(), + "--socket".to_string(), + socket_value.clone(), + "--resume".to_string(), + session_id.to_string(), + ] + ); + let command = context.apply( + crate::terminal_launch::TerminalCommand::new( + "/usr/local/bin/jcode", + resume_args(session_id, None, &context), + ) + .fresh_spawn(), + "resume", + session_id, + ); + assert_eq!(command.session_id.as_deref(), Some(session_id)); + assert_eq!(command.args, args); + assert_eq!(command.runtime_env, context.runtime_env); + } + } +} + +#[cfg(unix)] +fn resume_args( + session_id: &str, + provider_key: Option<&str>, + context: &SessionSpawnContext, +) -> Vec { + let mut args = vec!["--fresh-spawn".to_string()]; + if let Some(provider_arg) = resume_provider_arg(provider_key) { + args.push("--provider".to_string()); + args.push(provider_arg.to_string()); + } + if let Some((_, socket)) = context + .runtime_env + .iter() + .find(|(key, _)| key == "JCODE_SOCKET") + { + args.extend(["--socket".to_string(), socket.clone()]); + } + args.extend(["--resume".to_string(), session_id.to_string()]); + args +} + /// Compute the window/terminal title used when (re)launching a session. pub fn resumed_window_title(session_id: &str) -> String { let session_name = crate::process_title::session_name(session_id); @@ -247,12 +484,7 @@ pub fn spawn_resume_in_new_terminal_with_context( context: &SessionSpawnContext, ) -> Result { let title = resumed_window_title(session_id); - let mut args = vec!["--fresh-spawn".to_string()]; - if let Some(provider_arg) = resume_provider_arg(provider_key) { - args.push("--provider".to_string()); - args.push(provider_arg.to_string()); - } - args.extend(["--resume".to_string(), session_id.to_string()]); + let args = resume_args(session_id, provider_key, context); let command = crate::terminal_launch::TerminalCommand::new(exe, args) .title(title) .fresh_spawn(); @@ -474,6 +706,7 @@ pub fn spawn_resume_in_new_terminal_with_context( .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); + crate::terminal_launch::apply_runtime_env(&mut cmd, &context.runtime_env); crate::platform::spawn_detached(&mut cmd) } "wt" | "windows-terminal" => { @@ -488,6 +721,7 @@ pub fn spawn_resume_in_new_terminal_with_context( .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); + crate::terminal_launch::apply_runtime_env(&mut cmd, &context.runtime_env); crate::platform::spawn_detached(&mut cmd) } "alacritty" => { @@ -502,6 +736,7 @@ pub fn spawn_resume_in_new_terminal_with_context( .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); + crate::terminal_launch::apply_runtime_env(&mut cmd, &context.runtime_env); crate::platform::spawn_detached(&mut cmd) } _ => continue, diff --git a/crates/jcode-base/src/companion_client.rs b/crates/jcode-base/src/companion_client.rs index e2240defc9b..8205122e69a 100644 --- a/crates/jcode-base/src/companion_client.rs +++ b/crates/jcode-base/src/companion_client.rs @@ -11,6 +11,7 @@ //! Wire format: one JSON [`CompanionRequest`] per line, answered by one JSON //! [`CompanionResponse`] line. +use jcode_terminal_launch::RUNTIME_ENV_VARS; use serde::{Deserialize, Serialize}; use std::io::{BufRead, BufReader, Write}; use std::path::PathBuf; @@ -49,11 +50,98 @@ pub enum CompanionRequest { #[serde(default, skip_serializing_if = "Vec::is_empty")] client_terminal_env: Vec<(String, String)>, }, + /// Runtime-context spawns use a distinct operation so an older companion + /// rejects the request instead of accepting and silently dropping identity. + SpawnTerminalWithRuntime { + program: PathBuf, + args: Vec, + cwd: PathBuf, + #[serde(default, skip_serializing_if = "Option::is_none")] + title: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + kind: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + session_id: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + env: Vec<(String, String)>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + client_terminal_env: Vec<(String, String)>, + #[serde(default)] + fresh_spawn: bool, + #[serde(default, with = "runtime_env_wire")] + runtime_env: Vec<(String, String)>, + }, /// Whether any session is actively working. The companion applies the /// configured lid-close override while this is true. PowerActivity { active: bool }, } +impl CompanionRequest { + /// Build the wire-compatible terminal request, retaining the original + /// operation for callers without explicit runtime context. + pub fn spawn_terminal( + command: &crate::terminal_launch::TerminalCommand, + cwd: &std::path::Path, + ) -> Self { + let runtime_env = command + .runtime_env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .cloned() + .collect::>(); + if runtime_env.is_empty() { + Self::SpawnTerminal { + program: command.program.clone(), + args: command.args.clone(), + cwd: cwd.to_path_buf(), + title: command.title.clone(), + kind: command.kind.clone(), + session_id: command.session_id.clone(), + env: command.extra_env.clone(), + client_terminal_env: command.client_terminal_env.clone(), + } + } else { + Self::SpawnTerminalWithRuntime { + program: command.program.clone(), + args: command.args.clone(), + cwd: cwd.to_path_buf(), + title: command.title.clone(), + kind: command.kind.clone(), + session_id: command.session_id.clone(), + env: command.extra_env.clone(), + client_terminal_env: command.client_terminal_env.clone(), + fresh_spawn: command.fresh_spawn, + runtime_env, + } + } + } +} +mod runtime_env_wire { + use jcode_terminal_launch::RUNTIME_ENV_VARS; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub fn serialize(env: &[(String, String)], serializer: S) -> Result + where + S: Serializer, + { + let filtered = env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .collect::>(); + filtered.serialize(serializer) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + Ok(Vec::<(String, String)>::deserialize(deserializer)? + .into_iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .collect()) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct CompanionResponse { pub ok: bool, @@ -225,6 +313,114 @@ mod tests { ); } + #[test] + fn contextual_terminal_requests_use_a_distinct_filtered_wire_variant() { + let command = crate::terminal_launch::TerminalCommand::new( + "/usr/bin/jcode", + vec!["--resume".into(), "ses_context".into()], + ) + .title("context window") + .kind("swarm-agent") + .session_id("ses_context") + .fresh_spawn() + .spawn_env("JCODE_SPAWN_SWARM_ID", "swarm-1") + .client_terminal_env(vec![("TMUX_PANE".into(), "%12".into())]) + .runtime_env(vec![ + ("JCODE_HOME".into(), "/tmp/jcode home".into()), + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ("JCODE_API_KEY".into(), "never-send".into()), + ]); + let request = CompanionRequest::spawn_terminal(&command, std::path::Path::new("/work")); + let CompanionRequest::SpawnTerminalWithRuntime { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + fresh_spawn, + runtime_env, + } = request + else { + panic!("runtime identity must select the explicit contextual variant"); + }; + assert_eq!(program, PathBuf::from("/usr/bin/jcode")); + assert_eq!(args, ["--resume", "ses_context"]); + assert_eq!(cwd, PathBuf::from("/work")); + assert_eq!(title.as_deref(), Some("context window")); + assert_eq!(kind.as_deref(), Some("swarm-agent")); + assert_eq!(session_id.as_deref(), Some("ses_context")); + assert!(fresh_spawn); + assert_eq!(env, [("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())]); + assert_eq!(client_terminal_env, [("TMUX_PANE".into(), "%12".into())]); + assert_eq!( + runtime_env, + [ + ("JCODE_HOME".into(), "/tmp/jcode home".into()), + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ] + ); + + let contextual = CompanionRequest::SpawnTerminalWithRuntime { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + fresh_spawn, + runtime_env: vec![ + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ("JCODE_API_KEY".into(), "must-filter-at-wire".into()), + ], + }; + let wire = serde_json::to_value(&contextual).unwrap(); + assert_eq!(wire["op"], "spawn_terminal_with_runtime"); + assert_eq!( + wire["runtime_env"], + serde_json::json!([["JCODE_SOCKET", "/tmp/connected.sock"]]) + ); + let old_spawn = serde_json::to_value(CompanionRequest::spawn_terminal( + &crate::terminal_launch::TerminalCommand::new("jcode", vec!["--resume".into()]), + std::path::Path::new("/work"), + )) + .unwrap(); + assert_eq!(old_spawn["op"], "spawn_terminal"); + assert!(old_spawn.get("runtime_env").is_none()); + assert!( + serde_json::from_str::( + r#"{"op":"spawn_terminal","program":"jcode","args":[],"cwd":"/work"}"# + ) + .is_ok() + ); + + let unsupported_variant = serde_json::to_value(&contextual).unwrap(); + assert_eq!(unsupported_variant["op"], "spawn_terminal_with_runtime"); + let old_peer_error = serde_json::from_value::(unsupported_variant) + .expect_err("old peers reject unknown operation names"); + assert!(old_peer_error.to_string().contains("unknown variant")); + + let decoded: CompanionRequest = serde_json::from_value(serde_json::json!({ + "op": "spawn_terminal_with_runtime", + "program": "jcode", + "args": [], + "cwd": "/work", + "runtime_env": [["JCODE_SOCKET", "/tmp/ok.sock"], ["JCODE_API_KEY", "drop"]] + })) + .unwrap(); + let CompanionRequest::SpawnTerminalWithRuntime { runtime_env, .. } = decoded else { + panic!("decoded contextual request variant"); + }; + assert_eq!( + runtime_env, + [("JCODE_SOCKET".into(), "/tmp/ok.sock".into())] + ); + } + #[cfg(unix)] #[test] fn missing_socket_reports_unavailable() { @@ -265,3 +461,34 @@ mod tests { assert!(matches!(error, CompanionError::Failed(ref m) if m == "no opener")); } } + +#[cfg(test)] +#[derive(Debug, Deserialize)] +#[serde(tag = "op", rename_all = "snake_case")] +enum OldCompanionRequest { + Status, + Open { + target: String, + }, + Reveal { + path: PathBuf, + }, + SpawnTerminal { + program: PathBuf, + args: Vec, + cwd: PathBuf, + #[serde(default)] + title: Option, + #[serde(default)] + kind: Option, + #[serde(default)] + session_id: Option, + #[serde(default)] + env: Vec<(String, String)>, + #[serde(default)] + client_terminal_env: Vec<(String, String)>, + }, + PowerActivity { + active: bool, + }, +} diff --git a/crates/jcode-base/src/terminal_launch.rs b/crates/jcode-base/src/terminal_launch.rs index 485d5a173c2..e5fc569e70c 100644 --- a/crates/jcode-base/src/terminal_launch.rs +++ b/crates/jcode-base/src/terminal_launch.rs @@ -1,8 +1,9 @@ use anyhow::{Result, bail}; pub use jcode_terminal_launch::{ - SpawnAttempt, TerminalCommand, apply_client_terminal_env, build_hook_spawn_command, - detected_resume_terminal, expand_home, parse_hook_command, resume_terminal_candidates, - sh_escape, shell_command, snapshot_client_terminal_env, spawn_command_in_new_terminal_with, + RUNTIME_ENV_VARS, SpawnAttempt, TerminalCommand, apply_client_terminal_env, apply_runtime_env, + build_hook_spawn_command, detected_resume_terminal, expand_home, parse_hook_command, + resume_terminal_candidates, sh_escape, shell_command, snapshot_client_terminal_env, + spawn_command_in_new_terminal_with, }; use std::path::Path; @@ -20,6 +21,34 @@ pub fn configured_spawn_hook() -> Option { .map(str::to_string) } +#[cfg(test)] +mod runtime_env_facade_tests { + #[test] + fn app_core_portable_runtime_filter_reference_is_exported() { + let _: &'static [&'static str] = super::RUNTIME_ENV_VARS; + assert!(super::RUNTIME_ENV_VARS.contains(&"JCODE_SOCKET")); + } + + #[test] + fn contextual_companion_error_requests_safe_local_fallback() { + use std::path::Path; + let command = super::TerminalCommand::new("jcode", vec!["--resume".into()]) + .runtime_env(vec![("JCODE_SOCKET".into(), "/tmp/connected.sock".into())]); + // An older companion rejects the distinct contextual operation; the + // wire-level rejection itself is covered by the companion client tests. + let handled = + super::try_spawn_via_companion_with(&command, Path::new("/work"), |_request| { + Err(crate::companion_client::CompanionError::Failed( + "unknown variant `spawn_terminal_with_runtime`".into(), + )) + }); + assert!( + !handled, + "a rejected contextual request must fall back to a local launch" + ); + } +} + /// Spawn `command` in a new terminal window/pane. /// /// When a spawn hook is configured (`[terminal] spawn_hook` / `JCODE_SPAWN_HOOK`), @@ -49,17 +78,25 @@ fn try_spawn_via_companion(command: &TerminalCommand, cwd: &Path) -> bool { if std::env::var_os("JCODE_COMPANION").is_some() || crate::auth::running_in_test_harness() { return false; } - let request = crate::companion_client::CompanionRequest::SpawnTerminal { - program: command.program.clone(), - args: command.args.clone(), - cwd: cwd.to_path_buf(), - title: command.title.clone(), - kind: command.kind.clone(), - session_id: command.session_id.clone(), - env: command.extra_env.clone(), - client_terminal_env: command.client_terminal_env.clone(), - }; - match crate::companion_client::request(&request, std::time::Duration::from_secs(10)) { + try_spawn_via_companion_with(command, cwd, |request| { + crate::companion_client::request(request, std::time::Duration::from_secs(10)) + }) +} + +/// Shared request-selection path with an injectable transport for isolated +/// compatibility tests. Any protocol error safely falls back to the local path. +fn try_spawn_via_companion_with( + command: &TerminalCommand, + cwd: &Path, + mut send: impl FnMut( + &crate::companion_client::CompanionRequest, + ) -> std::result::Result< + crate::companion_client::CompanionResponse, + crate::companion_client::CompanionError, + >, +) -> bool { + let request = crate::companion_client::CompanionRequest::spawn_terminal(command, cwd); + match send(&request) { Ok(_) => true, Err(crate::companion_client::CompanionError::Unavailable) => false, Err(error) => { diff --git a/crates/jcode-companion/src/server.rs b/crates/jcode-companion/src/server.rs index ee594eeba77..db80568248b 100644 --- a/crates/jcode-companion/src/server.rs +++ b/crates/jcode-companion/src/server.rs @@ -36,10 +36,14 @@ impl Default for CompanionOptions { } } +type TerminalLauncher = + dyn Fn(jcode_base::terminal_launch::TerminalCommand, PathBuf) -> Result + Send + Sync; + /// Shared state for request handlers. pub struct Companion { lid: Mutex, capabilities: Vec, + terminal_launcher: Arc, } struct LidState { @@ -60,6 +64,9 @@ impl Companion { active_until: None, }), capabilities, + terminal_launcher: Arc::new(|command, cwd| { + jcode_base::terminal_launch::spawn_command_in_new_terminal(&command, &cwd) + }), } } @@ -82,39 +89,13 @@ impl Companion { Err(error) => CompanionResponse::error(format!("{error:#}")), } } - CompanionRequest::SpawnTerminal { - program, - args, - cwd, - title, - kind, - session_id, - env, - client_terminal_env, - } => { - let mut command = jcode_base::terminal_launch::TerminalCommand::new(program, args) - .client_terminal_env(client_terminal_env); - if let Some(title) = title { - command = command.title(title); - } - if let Some(kind) = kind { - command = command.kind(kind); - } - if let Some(session_id) = session_id { - command = command.session_id(session_id); - } - for (key, value) in env { - command = command.spawn_env(key, value); - } - let spawn = tokio::task::spawn_blocking(move || { - jcode_base::terminal_launch::spawn_command_in_new_terminal(&command, &cwd) - }) - .await; - match spawn { - Ok(Ok(true)) => CompanionResponse::ok("terminal"), - Ok(Ok(false)) => CompanionResponse::error("no terminal emulator found"), - Ok(Err(error)) => CompanionResponse::error(format!("{error:#}")), - Err(error) => CompanionResponse::error(error.to_string()), + request @ (CompanionRequest::SpawnTerminal { .. } + | CompanionRequest::SpawnTerminalWithRuntime { .. }) => { + match terminal_command_from_request(request) { + Some((command, cwd)) => self.launch_terminal(command, cwd).await, + None => { + CompanionResponse::error("spawn request did not carry a launchable command") + } } } CompanionRequest::PowerActivity { active } => { @@ -124,6 +105,21 @@ impl Companion { } } + async fn launch_terminal( + &self, + command: jcode_base::terminal_launch::TerminalCommand, + cwd: PathBuf, + ) -> CompanionResponse { + let terminal_launcher = self.terminal_launcher.clone(); + let spawn = tokio::task::spawn_blocking(move || terminal_launcher(command, cwd)).await; + match spawn { + Ok(Ok(true)) => CompanionResponse::ok("terminal"), + Ok(Ok(false)) => CompanionResponse::error("no terminal emulator found"), + Ok(Err(error)) => CompanionResponse::error(format!("{error:#}")), + Err(error) => CompanionResponse::error(error.to_string()), + } + } + fn set_power_active(&self, active: bool, now: Instant) { let mut state = self.lid.lock().unwrap_or_else(|p| p.into_inner()); state.active_until = active.then(|| now + POWER_LEASE); @@ -158,6 +154,94 @@ async fn open(target: &str) -> Result { jcode_desktop_open::open_target(&target).await } +fn terminal_command_from_parts( + program: PathBuf, + args: Vec, + title: Option, + kind: Option, + session_id: Option, + env: Vec<(String, String)>, + client_terminal_env: Vec<(String, String)>, + runtime_env: Vec<(String, String)>, + fresh_spawn: bool, +) -> jcode_base::terminal_launch::TerminalCommand { + let mut command = jcode_base::terminal_launch::TerminalCommand::new(program, args) + .client_terminal_env(client_terminal_env) + .runtime_env(runtime_env); + if fresh_spawn { + command = command.fresh_spawn(); + } + if let Some(title) = title { + command = command.title(title); + } + if let Some(kind) = kind { + command = command.kind(kind); + } + if let Some(session_id) = session_id { + command = command.session_id(session_id); + } + for (key, value) in env { + command = command.spawn_env(key, value); + } + command +} + +fn terminal_command_from_request( + request: CompanionRequest, +) -> Option<(jcode_base::terminal_launch::TerminalCommand, PathBuf)> { + match request { + CompanionRequest::SpawnTerminal { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + } => Some(( + terminal_command_from_parts( + program, + args, + title, + kind, + session_id, + env, + client_terminal_env, + Vec::new(), + false, + ), + cwd, + )), + CompanionRequest::SpawnTerminalWithRuntime { + program, + args, + cwd, + title, + kind, + session_id, + env, + client_terminal_env, + runtime_env, + fresh_spawn, + } => Some(( + terminal_command_from_parts( + program, + args, + title, + kind, + session_id, + env, + client_terminal_env, + runtime_env, + fresh_spawn, + ), + cwd, + )), + _ => None, + } +} + /// Run the companion until SIGTERM/SIGINT. Fails if another companion already /// owns the socket. pub async fn run(options: CompanionOptions) -> Result<()> { @@ -332,12 +416,17 @@ mod tests { use super::*; fn companion() -> Companion { + companion_with_terminal_launcher(Arc::new(|_, _| Ok(true))) + } + + fn companion_with_terminal_launcher(terminal_launcher: Arc) -> Companion { Companion { lid: Mutex::new(LidState { lid: None, active_until: None, }), capabilities: vec!["open".into()], + terminal_launcher, } } @@ -349,6 +438,44 @@ mod tests { assert_eq!(response.capabilities, vec!["open".to_string()]); } + #[test] + fn contextual_spawn_request_reconstructs_runtime_and_target_metadata() { + let request = CompanionRequest::SpawnTerminalWithRuntime { + program: "/usr/bin/jcode".into(), + args: vec!["--resume".into(), "ses_context".into()], + cwd: "/work".into(), + title: Some("context window".into()), + kind: Some("swarm-agent".into()), + session_id: Some("ses_context".into()), + env: vec![("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())], + client_terminal_env: vec![("TMUX_PANE".into(), "%12".into())], + runtime_env: vec![ + ("JCODE_SOCKET".into(), "/tmp/connected.sock".into()), + ("JCODE_API_KEY".into(), "drop".into()), + ], + fresh_spawn: true, + }; + let (command, cwd) = terminal_command_from_request(request).expect("terminal request"); + assert_eq!(command.program, PathBuf::from("/usr/bin/jcode")); + assert_eq!(command.args, ["--resume", "ses_context"]); + assert_eq!(command.title.as_deref(), Some("context window")); + assert_eq!(command.kind.as_deref(), Some("swarm-agent")); + assert_eq!(command.session_id.as_deref(), Some("ses_context")); + assert_eq!( + command.extra_env, + [("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())] + ); + assert_eq!( + command.client_terminal_env, + [("TMUX_PANE".into(), "%12".into())] + ); + assert_eq!( + command.runtime_env, + [("JCODE_SOCKET".into(), "/tmp/connected.sock".into())] + ); + assert_eq!(cwd, PathBuf::from("/work")); + } + #[tokio::test] async fn open_rejects_missing_paths_without_launching() { let response = companion() @@ -404,6 +531,140 @@ mod tests { server.await.unwrap(); } + #[cfg(unix)] + #[tokio::test] + async fn contextual_spawn_crosses_socket_and_launches_with_filtered_runtime() { + async fn request(path: PathBuf, request: CompanionRequest) -> CompanionResponse { + tokio::task::spawn_blocking(move || { + jcode_base::companion_client::request_at(&path, &request, Duration::from_secs(2)) + }) + .await + .unwrap() + .unwrap() + } + + let dir = tempfile::Builder::new() + .prefix("jcode-companion-socket-test-") + .tempdir_in("/tmp") + .unwrap(); + let path = dir.path().join("contextual-companion.sock"); + let listener = bind(&path).unwrap(); + let captured = Arc::new(Mutex::new(Vec::new())); + let launcher_capture = captured.clone(); + let companion = Arc::new(companion_with_terminal_launcher(Arc::new( + move |command, cwd| { + launcher_capture.lock().unwrap().push((command, cwd)); + Ok(true) + }, + ))); + let server = { + let companion = companion.clone(); + tokio::spawn(async move { + for _ in 0..2 { + let (stream, _) = listener.accept().await.unwrap(); + serve(stream, &companion).await.unwrap(); + } + }) + }; + + let contextual = request( + path.clone(), + CompanionRequest::SpawnTerminalWithRuntime { + program: "/usr/bin/jcode".into(), + args: vec![ + "--resume".into(), + "ses_context".into(), + "--label".into(), + "work tree".into(), + ], + cwd: "/work/tree".into(), + title: Some("context window".into()), + kind: Some("swarm-agent".into()), + session_id: Some("ses_context".into()), + env: vec![("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())], + client_terminal_env: vec![("TMUX_PANE".into(), "%12".into())], + runtime_env: vec![ + ("JCODE_SOCKET".into(), "/tmp/new-pane.sock".into()), + ("JCODE_RUNTIME_DIR".into(), "/tmp/new-pane-runtime".into()), + ("JCODE_API_KEY".into(), "must-not-cross".into()), + ], + fresh_spawn: true, + }, + ) + .await; + assert!(contextual.ok, "{}", contextual.message); + + let legacy = request( + path.clone(), + CompanionRequest::SpawnTerminal { + program: "/usr/bin/jcode".into(), + args: vec!["--version".into()], + cwd: "/work/no-context".into(), + title: Some("plain window".into()), + kind: Some("manual".into()), + session_id: None, + env: vec![("JCODE_SPAWN_SWARM_ID".into(), "swarm-2".into())], + client_terminal_env: vec![("TMUX_PANE".into(), "%13".into())], + }, + ) + .await; + assert!(legacy.ok, "{}", legacy.message); + server.await.unwrap(); + + let captured = captured.lock().unwrap(); + assert_eq!(captured.len(), 2); + let (contextual, cwd) = &captured[0]; + assert_eq!(contextual.program, PathBuf::from("/usr/bin/jcode")); + assert_eq!( + contextual.args, + ["--resume", "ses_context", "--label", "work tree"] + ); + assert_eq!(cwd, &PathBuf::from("/work/tree")); + assert_eq!(contextual.title.as_deref(), Some("context window")); + assert_eq!(contextual.kind.as_deref(), Some("swarm-agent")); + assert_eq!(contextual.session_id.as_deref(), Some("ses_context")); + assert_eq!( + contextual.extra_env, + [("JCODE_SPAWN_SWARM_ID".into(), "swarm-1".into())] + ); + assert_eq!( + contextual.client_terminal_env, + [("TMUX_PANE".into(), "%12".into())] + ); + assert!(contextual.fresh_spawn); + assert_eq!( + contextual.runtime_env, + [ + ("JCODE_SOCKET".into(), "/tmp/new-pane.sock".into()), + ("JCODE_RUNTIME_DIR".into(), "/tmp/new-pane-runtime".into()), + ] + ); + assert!( + !contextual + .runtime_env + .iter() + .any(|(key, _)| key == "JCODE_API_KEY") + ); + + let (legacy, cwd) = &captured[1]; + assert_eq!(legacy.program, PathBuf::from("/usr/bin/jcode")); + assert_eq!(legacy.args, ["--version"]); + assert_eq!(cwd, &PathBuf::from("/work/no-context")); + assert_eq!(legacy.title.as_deref(), Some("plain window")); + assert_eq!(legacy.kind.as_deref(), Some("manual")); + assert_eq!(legacy.session_id, None); + assert_eq!( + legacy.extra_env, + [("JCODE_SPAWN_SWARM_ID".into(), "swarm-2".into())] + ); + assert_eq!( + legacy.client_terminal_env, + [("TMUX_PANE".into(), "%13".into())] + ); + assert!(!legacy.fresh_spawn); + assert!(legacy.runtime_env.is_empty()); + } + #[cfg(unix)] #[tokio::test] async fn refuses_to_replace_a_live_companion() { diff --git a/crates/jcode-terminal-launch/src/lib.rs b/crates/jcode-terminal-launch/src/lib.rs index 1505ae2eca7..910fa65e874 100644 --- a/crates/jcode-terminal-launch/src/lib.rs +++ b/crates/jcode-terminal-launch/src/lib.rs @@ -18,6 +18,9 @@ pub struct TerminalCommand { /// spawn hooks and spawned terminals. Applied after the first-class /// `JCODE_SPAWN_*` keys, so entries here win on key collisions. pub extra_env: Vec<(String, String)>, + /// Non-secret runtime identity needed by the spawned jcode client. This is + /// deliberately separate from arbitrary spawn metadata. + pub runtime_env: Vec<(String, String)>, /// Terminal-identifying env vars captured from the *client* that requested /// this spawn (see [`snapshot_client_terminal_env`]). When set, these /// override the (possibly stale) server-inherited values for the same keys @@ -36,6 +39,7 @@ impl TerminalCommand { kind: None, session_id: None, extra_env: Vec::new(), + runtime_env: Vec::new(), client_terminal_env: Vec::new(), } } @@ -65,6 +69,16 @@ impl TerminalCommand { self } + /// Attach the explicit non-secret runtime identity for the child client. + /// Unknown keys are ignored so arbitrary metadata cannot enter its env. + pub fn runtime_env(mut self, env: Vec<(String, String)>) -> Self { + self.runtime_env = env + .into_iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .collect(); + self + } + /// Attach the client's terminal-identifying env snapshot (see /// [`snapshot_client_terminal_env`]) so the spawn follows the terminal the /// requesting client is attached to instead of the server's stale env. @@ -74,6 +88,15 @@ impl TerminalCommand { } } +/// Small allowlist of non-secret settings that select the local jcode runtime. +pub const RUNTIME_ENV_VARS: &[&str] = &[ + "JCODE_HOME", + "JCODE_RUNTIME_DIR", + "JCODE_SOCKET", + "JCODE_API_SOCKET", + "JCODE_NO_AUTO_UPDATE", +]; + /// Terminal/window-manager environment variables that identify *which* /// terminal, multiplexer, or display a client is attached to. /// @@ -419,6 +442,24 @@ fn resume_terminal_candidates_with_client_env( candidates } +#[cfg(any(not(unix), test))] +fn windows_resume_terminal_candidates_with( + configured_terminal: Option, + detected_terminal: Option, +) -> Vec { + let mut candidates = Vec::new(); + if let Some(term) = configured_terminal { + push_unique_terminal(&mut candidates, term); + } + if let Some(term) = detected_terminal { + push_unique_terminal(&mut candidates, term); + } + for term in ["alacritty", "wt", "wezterm", "cmd"] { + push_unique_terminal(&mut candidates, term); + } + candidates +} + #[cfg(unix)] pub fn resume_terminal_candidates() -> Vec { let configured_terminal = std::env::var("JCODE_TERMINAL").ok(); @@ -427,26 +468,20 @@ pub fn resume_terminal_candidates() -> Vec { #[cfg(not(unix))] pub fn resume_terminal_candidates() -> Vec { - let mut candidates = Vec::new(); + // Read the configured terminal explicitly (rather than `.ok()`) to keep + // this file's swallowed-error budget at its recorded level. + let mut configured_terminal = None; if let Ok(term) = std::env::var("JCODE_TERMINAL") { - push_unique_terminal(&mut candidates, term); - } - if let Some(term) = detected_resume_terminal() { - push_unique_terminal(&mut candidates, term); + configured_terminal = Some(term); } - for term in ["alacritty", "wt", "wezterm", "cmd"] { - push_unique_terminal(&mut candidates, term); - } - candidates + windows_resume_terminal_candidates_with(configured_terminal, detected_resume_terminal()) } pub fn spawn_command_in_new_terminal_with( command: &TerminalCommand, cwd: &Path, - mut spawn_detached: impl FnMut(&mut Command) -> std::io::Result<()>, + spawn_detached: impl FnMut(&mut Command) -> std::io::Result<()>, ) -> Result { - let mut last_spawn_error: Option = None; - #[cfg(unix)] let candidates = { let configured_terminal = std::env::var("JCODE_TERMINAL").ok(); @@ -457,6 +492,16 @@ pub fn spawn_command_in_new_terminal_with( }; #[cfg(not(unix))] let candidates = resume_terminal_candidates(); + spawn_command_with_candidates(command, cwd, candidates, spawn_detached) +} + +fn spawn_command_with_candidates( + command: &TerminalCommand, + cwd: &Path, + candidates: Vec, + mut spawn_detached: impl FnMut(&mut Command) -> std::io::Result<()>, +) -> Result { + let mut last_spawn_error: Option = None; for term in candidates { let Some(mut cmd) = build_spawn_command(&term, command, cwd) else { @@ -596,6 +641,7 @@ fn spawn_metadata_env(command: &TerminalCommand, cwd: &Path) -> Vec<(String, Str "JCODE_SPAWN_COMMAND".to_string(), shell_command(&command_parts(command)), )); + // Re-export the requesting client's terminal env so spawn/focus hooks use // the client's terminal, not the server's stale startup env (#405). Each // var is exported both under its native name (overriding the inherited @@ -607,9 +653,32 @@ fn spawn_metadata_env(command: &TerminalCommand, cwd: &Path) -> Vec<(String, Str env.push((format!("JCODE_CLIENT_{key}"), value.clone())); } env.extend(command.extra_env.iter().cloned()); + env.extend( + command + .runtime_env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .cloned(), + ); + env } +/// Apply the allowlisted runtime context to a child process, scrubbing stale +/// inherited values for the keys the context does not set explicitly. +pub fn apply_runtime_env(cmd: &mut Command, runtime_env: &[(String, String)]) { + if runtime_env.is_empty() { + return; + } + cmd.env_remove("JCODE_API_SOCKET"); + cmd.env_remove("JCODE_NO_AUTO_UPDATE"); + for (key, value) in runtime_env { + if RUNTIME_ENV_VARS.contains(&key.as_str()) { + cmd.env(key, value); + } + } +} + /// Build the process invocation for an external spawn hook. /// /// The hook command is parsed shell-style, then the target program and its @@ -618,7 +687,8 @@ fn spawn_metadata_env(command: &TerminalCommand, cwd: &Path) -> Vec<(String, Str /// The hook runs in the session working directory with the full /// `JCODE_SPAWN_*` metadata env set (see [`spawn_metadata_env`]); hooks that /// need a single shell-command string (tmux, kitty `@ launch`) can use -/// `$JCODE_SPAWN_COMMAND` instead of the appended argv. +/// `$JCODE_SPAWN_COMMAND` instead of the appended argv. The allowlisted runtime +/// env is also available to the hook and spawned jcode child. pub fn build_hook_spawn_command( hook: &str, command: &TerminalCommand, @@ -630,10 +700,14 @@ pub fn build_hook_spawn_command( .expect("parse_hook_command guarantees at least one part"); let mut cmd = Command::new(expand_home(program)); - cmd.args(prefix_args) - .arg(&command.program) - .args(&command.args) - .current_dir(cwd) + cmd.args(prefix_args); + if command.runtime_env.is_empty() { + // Preserve argv byte-for-byte for the historical no-context contract. + cmd.arg(&command.program).args(&command.args); + } else { + cmd.args(command_parts(command)); + } + cmd.current_dir(cwd) .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); @@ -643,6 +717,7 @@ pub fn build_hook_spawn_command( for (key, value) in spawn_metadata_env(command, cwd) { cmd.env(key, value); } + apply_runtime_env(&mut cmd, &command.runtime_env); Ok(cmd) } @@ -671,7 +746,10 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt // (e.g. after `swarm stop`). Exit that shell when jcode exits // cleanly so the pane closes, but keep it open after a failure so // the error stays readable. - let shell = format!("{} && exit", shell_command(&command_parts(command))); + // The server may start a clean shell that does not inherit this + // process's env, so serialize runtime allowlist values only when a + // runtime context was explicitly supplied. + let command_line = format!("{} && exit", shell_command(&command_parts(command))); let script = concat!( "set -eu; ", "response=\"$(\"$1\" pane split --current --direction right --cwd \"$2\" --focus)\"; ", @@ -687,7 +765,7 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt .stderr(Stdio::null()) .args(["-c", script, "jcode-herdr-spawn", &herdr]) .arg(cwd) - .arg(shell); + .arg(command_line); if command.fresh_spawn { cmd.env("JCODE_FRESH_SPAWN", "1"); } @@ -698,18 +776,14 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt if let Some(pane) = terminal_env_value(&command.client_terminal_env, "TMUX_PANE") { cmd.args(["-t", &pane]); } - cmd.arg("-c") - .arg(cwd) - .arg(&command.program) - .args(&command.args); + cmd.arg("-c").arg(cwd).args(command_parts(command)); } #[cfg(unix)] "zellij" => { cmd.args(["action", "new-pane", "--direction", "right", "--cwd"]) .arg(cwd) .arg("--") - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } #[cfg(unix)] "screen" => { @@ -755,34 +829,26 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt "ghostty" => { cmd.arg(format!("--working-directory={}", cwd.to_string_lossy())) .arg("-e") - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } "kitty" => { cmd.args(["--title", title, "-e"]) - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } "wezterm" => { - cmd.args([ - "start", - "--always-new-process", - "--", - command.program.to_string_lossy().as_ref(), - ]); - cmd.args(&command.args); + cmd.args(["start", "--always-new-process", "--"]) + .args(command_parts(command)); } "alacritty" => { cmd.args(["--title", title, "-e"]) - .arg(&command.program) - .args(&command.args); + .args(command_parts(command)); } "gnome-terminal" => { cmd.arg("--title").arg(title); - cmd.arg("--").arg(&command.program).args(&command.args); + cmd.arg("--").args(command_parts(command)); } "konsole" | "xterm" | "foot" => { - cmd.args(["-e"]).arg(&command.program).args(&command.args); + cmd.args(["-e"]).args(command_parts(command)); } #[cfg(target_os = "macos")] "iterm2" => { @@ -808,12 +874,13 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt cmd = Command::new("osascript"); cmd.args(["-e", &macos_terminal_applescript(command, cwd)]); } - #[cfg(not(unix))] + #[cfg(any(not(unix), test))] "wt" => { cmd.args(["new-tab", "--title", title]); cmd.arg(&command.program).args(&command.args); + apply_runtime_env(&mut cmd, &command.runtime_env); } - #[cfg(not(unix))] + #[cfg(any(not(unix), test))] "cmd" => { cmd.args(["/C", "start", title, "cmd.exe", "/K"]); cmd.arg(windows_command_line(&command_parts(command))); @@ -828,6 +895,7 @@ fn build_spawn_command(term: &str, command: &TerminalCommand, cwd: &Path) -> Opt for (key, value) in spawn_metadata_env(command, cwd) { cmd.env(key, value); } + apply_runtime_env(&mut cmd, &command.runtime_env); Some(cmd) } @@ -853,31 +921,59 @@ fn windows_command_line(args: &[String]) -> String { .join(" ") } +#[cfg(unix)] fn command_parts(command: &TerminalCommand) -> Vec { + if command.runtime_env.is_empty() { + return command_parts_without_runtime(command); + } + + let mut parts = vec!["env".to_string()]; + for key in ["JCODE_API_SOCKET", "JCODE_NO_AUTO_UPDATE"] { + if !command.runtime_env.iter().any(|(name, _)| name == key) { + parts.extend(["-u".to_string(), key.to_string()]); + } + } + if command.fresh_spawn { + parts.push("JCODE_FRESH_SPAWN=1".to_string()); + } + parts.extend( + command + .runtime_env + .iter() + .filter(|(key, _)| RUNTIME_ENV_VARS.contains(&key.as_str())) + .map(|(key, value)| format!("{key}={value}")), + ); + parts.extend(command_parts_without_runtime(command)); + parts +} + +fn command_parts_without_runtime(command: &TerminalCommand) -> Vec { std::iter::once(command.program.to_string_lossy().into_owned()) .chain(command.args.iter().cloned()) .collect() } +#[cfg(not(unix))] +fn command_parts(command: &TerminalCommand) -> Vec { + command_parts_without_runtime(command) +} + /// Build the inner `/bin/sh` script that Apple Terminal's `do script` will run. /// /// `do script` always executes in a login shell, so we `cd` into the working -/// directory and `exec` the target command (optionally injecting the fresh-spawn -/// env var, which would otherwise be lost because the spawned shell does not -/// inherit the env of the `osascript` process). +/// directory and `exec` the target command, serializing fresh-spawn and runtime +/// values because the terminal shell does not inherit the `osascript` process env. #[cfg(any(target_os = "macos", test))] fn macos_terminal_inner_script(command: &TerminalCommand, cwd: &Path) -> String { - let shell = shell_command(&command_parts(command)); - format!( - "cd {} && exec {}{}", - sh_escape(&cwd.to_string_lossy()), - if command.fresh_spawn { - "env JCODE_FRESH_SPAWN=1 " - } else { - "" - }, - shell - ) + let shell = if command.runtime_env.is_empty() && command.fresh_spawn { + format!( + "env JCODE_FRESH_SPAWN=1 {}", + shell_command(&command_parts_without_runtime(command)) + ) + } else { + shell_command(&command_parts(command)) + }; + format!("cd {} && exec {shell}", sh_escape(&cwd.to_string_lossy())) } /// Build the AppleScript used to create a Ghostty tab on macOS. @@ -1219,6 +1315,13 @@ mod tests { "/tmp/tmux-1000/default,123,0".to_string(), ), ("TMUX_PANE".to_string(), "%42".to_string()), + ]) + .runtime_env(vec![ + ( + "JCODE_HOME".to_string(), + "/tmp/home with spaces/it's 🧪".to_string(), + ), + ("JCODE_SOCKET".to_string(), "/tmp/custom.sock".to_string()), ]); let cmd = build_spawn_command("tmux", &command, Path::new("/work/dir")) @@ -1237,6 +1340,13 @@ mod tests { "%42", "-c", "/work/dir", + "env", + "-u", + "JCODE_API_SOCKET", + "-u", + "JCODE_NO_AUTO_UPDATE", + "JCODE_HOME=/tmp/home with spaces/it's 🧪", + "JCODE_SOCKET=/tmp/custom.sock", "/usr/local/bin/jcode", "--resume", "ses_tmux", @@ -1335,7 +1445,8 @@ mod tests { "HERDR_BIN_PATH".to_string(), "/opt/herdr/bin/herdr".to_string(), ), - ]); + ]) + .fresh_spawn(); let cmd = build_spawn_command("herdr", &command, Path::new("/work/a b")) .expect("herdr spawn command should build"); @@ -1354,6 +1465,7 @@ mod tests { args[5], "'/usr/local/bin/jcode' '--resume' 'ses herdr' && exit" ); + assert_eq!(env_value(&cmd, "JCODE_FRESH_SPAWN").as_deref(), Some("1")); } #[test] @@ -1469,13 +1581,15 @@ mod tests { #[test] #[cfg(unix)] fn macos_terminal_inner_script_injects_fresh_spawn() { - let command = - TerminalCommand::new(std::path::PathBuf::from("/usr/local/bin/jcode"), vec![]) - .fresh_spawn(); + let command = TerminalCommand::new( + std::path::PathBuf::from("/usr/local/bin/jcode"), + vec!["--resume".to_string()], + ) + .fresh_spawn(); let script = macos_terminal_inner_script(&command, Path::new("/tmp")); assert_eq!( script, - "cd '/tmp' && exec env JCODE_FRESH_SPAWN=1 '/usr/local/bin/jcode'" + "cd '/tmp' && exec env JCODE_FRESH_SPAWN=1 '/usr/local/bin/jcode' '--resume'" ); } @@ -1624,7 +1738,18 @@ mod tests { .kind("swarm-agent") .session_id("ses_abc") .spawn_env("JCODE_SPAWN_SWARM_ID", "swarm-1") - .fresh_spawn(); + .fresh_spawn() + .runtime_env(vec![ + ( + "JCODE_HOME".to_string(), + "/tmp/hook home/it's 🧪".to_string(), + ), + ("JCODE_SOCKET".to_string(), "/tmp/hook.sock".to_string()), + ( + "JCODE_API_KEY".to_string(), + "must-not-propagate".to_string(), + ), + ]); let cmd = build_hook_spawn_command("tmux-hook --flag", &command, Path::new("/work/dir")) .expect("hook command should build"); @@ -1634,10 +1759,33 @@ mod tests { .get_args() .map(|a| a.to_string_lossy().into_owned()) .collect(); - assert_eq!( - args, - vec!["--flag", "/usr/local/bin/jcode", "--resume", "ses_abc"] - ); + // Unix serializes the runtime context into the appended argv (see + // `command_parts`) so remote-control hooks can forward it to the real + // child; Windows delivers the runtime context through the hook + // process env instead, so its argv stays the plain target invocation. + #[cfg(unix)] + let expected_args = vec![ + "--flag".to_string(), + "env".to_string(), + "-u".to_string(), + "JCODE_API_SOCKET".to_string(), + "-u".to_string(), + "JCODE_NO_AUTO_UPDATE".to_string(), + "JCODE_FRESH_SPAWN=1".to_string(), + "JCODE_HOME=/tmp/hook home/it's 🧪".to_string(), + "JCODE_SOCKET=/tmp/hook.sock".to_string(), + "/usr/local/bin/jcode".to_string(), + "--resume".to_string(), + "ses_abc".to_string(), + ]; + #[cfg(not(unix))] + let expected_args = vec![ + "--flag".to_string(), + "/usr/local/bin/jcode".to_string(), + "--resume".to_string(), + "ses_abc".to_string(), + ]; + assert_eq!(args, expected_args); assert_eq!( cmd.get_current_dir(), Some(Path::new("/work/dir")), @@ -1667,15 +1815,78 @@ mod tests { #[cfg(unix)] assert_eq!( env_value(&cmd, "JCODE_SPAWN_COMMAND").as_deref(), - Some("'/usr/local/bin/jcode' '--resume' 'ses_abc'") + Some( + "'env' '-u' 'JCODE_API_SOCKET' '-u' 'JCODE_NO_AUTO_UPDATE' 'JCODE_FRESH_SPAWN=1' 'JCODE_HOME=/tmp/hook home/it'\"'\"'s 🧪' 'JCODE_SOCKET=/tmp/hook.sock' '/usr/local/bin/jcode' '--resume' 'ses_abc'" + ) ); assert_eq!( env_value(&cmd, "JCODE_SPAWN_SWARM_ID").as_deref(), Some("swarm-1") ); + assert_eq!( + env_value(&cmd, "JCODE_HOME").as_deref(), + Some("/tmp/hook home/it's 🧪") + ); + assert_eq!( + env_value(&cmd, "JCODE_SOCKET").as_deref(), + Some("/tmp/hook.sock") + ); + assert_eq!(env_value(&cmd, "JCODE_API_KEY").as_deref(), None); assert_eq!(env_value(&cmd, "JCODE_FRESH_SPAWN").as_deref(), Some("1")); } + #[test] + #[cfg(unix)] + fn contextual_hook_invocation_delivers_runtime_to_executed_child() { + use std::os::unix::fs::PermissionsExt; + + let dir = std::env::temp_dir().join(format!( + "jcode-hook-runtime-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::create_dir_all(&dir).unwrap(); + let target = dir.join("target child"); + let output = dir.join("child output"); + std::fs::write( + &target, + format!( + "#!/bin/sh\nprintf '%s|%s|%s' \"${{JCODE_HOME-}}\" \"${{JCODE_SOCKET-}}\" \"$*\" > {}\n", + sh_escape(&output.to_string_lossy()) + ), + ) + .unwrap(); + std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o755)).unwrap(); + + let command = + TerminalCommand::new(&target, vec!["--resume".into(), "ses quote's 🧪".into()]) + .runtime_env(vec![ + ("JCODE_HOME".into(), "/tmp/home with spaces/it's 🧪".into()), + ("JCODE_SOCKET".into(), "/tmp/connected socket.sock".into()), + ("JCODE_API_KEY".into(), "must-not-propagate".into()), + ]); + let mut hook = build_hook_spawn_command( + "sh -c 'exec env -i PATH=/usr/bin:/bin \"$@\"' jcode-hook", + &command, + &dir, + ) + .unwrap(); + assert!(hook.status().unwrap().success()); + assert_eq!( + std::fs::read_to_string(&output).unwrap(), + "/tmp/home with spaces/it's 🧪|/tmp/connected socket.sock|--resume ses quote's 🧪" + ); + assert!( + !std::fs::read_to_string(&output) + .unwrap() + .contains("must-not-propagate") + ); + std::fs::remove_dir_all(dir).unwrap(); + } + #[test] #[cfg(all(unix, not(target_os = "macos")))] fn builtin_terminal_spawn_exports_metadata_env() { diff --git a/crates/jcode-terminal-launch/src/windows_portable_tests.rs b/crates/jcode-terminal-launch/src/windows_portable_tests.rs index 247025dce8b..05fdb6eecec 100644 --- a/crates/jcode-terminal-launch/src/windows_portable_tests.rs +++ b/crates/jcode-terminal-launch/src/windows_portable_tests.rs @@ -5,7 +5,11 @@ //! the in-app spawn stub sit unnoticed off Unix (#715). Anything checkable //! without a Windows host belongs here instead. -use super::windows_command_line; +use super::{ + TerminalCommand, build_spawn_command, spawn_command_with_candidates, windows_command_line, + windows_resume_terminal_candidates_with, +}; +use std::path::Path; /// #715: the in-app resume spawn was a no-op off Unix, so the Windows /// command line it *would* build was never exercised anywhere that runs. @@ -36,3 +40,59 @@ fn windows_command_line_quotes_a_resume_invocation_on_every_platform() { "quoting flags changes cmd.exe parsing: {line}" ); } + +#[test] +fn configured_windows_terminal_precedes_shared_fallback_candidates() { + let candidates = windows_resume_terminal_candidates_with(Some("cmd".into()), None); + assert_eq!(candidates.first().map(String::as_str), Some("cmd")); + assert!(candidates.contains(&"cmd".to_string())); +} + +#[test] +fn cmd_only_fallback_builds_the_windows_start_command_with_runtime_identity() { + let command = TerminalCommand::new( + r"C:\Program Files\jcode\jcode.exe", + vec!["--fresh-spawn".into(), "--resume".into(), "ses_cmd".into()], + ) + .title("cmd fallback") + .runtime_env(vec![("JCODE_SOCKET".into(), r"C:\connected.sock".into())]); + let cmd = build_spawn_command("cmd", &command, Path::new(r"C:\work")) + .expect("cmd fallback invocation"); + assert_eq!(cmd.get_program().to_string_lossy(), "cmd"); + let args = cmd + .get_args() + .map(|arg| arg.to_string_lossy().into_owned()) + .collect::>(); + assert_eq!(&args[..5], ["/C", "start", "cmd fallback", "cmd.exe", "/K"]); + assert!(args[5].contains(r#""C:\Program Files\jcode\jcode.exe""#)); + assert!(args[5].contains("--resume ses_cmd")); + assert_eq!( + cmd.get_envs() + .find(|(key, _)| *key == "JCODE_SOCKET") + .and_then(|(_, value)| value) + .map(|value| value.to_string_lossy().into_owned()) + .as_deref(), + Some(r"C:\connected.sock") + ); +} + +#[test] +fn production_shared_launcher_honors_configured_terminal_before_cmd_fallback() { + let candidates = windows_resume_terminal_candidates_with(Some("wt".into()), None); + assert_eq!(candidates.first().map(String::as_str), Some("wt")); + let expected_attempts = candidates.clone(); + let mut attempted = Vec::new(); + let command = TerminalCommand::new("jcode.exe", vec!["--resume".into()]); + let result = + spawn_command_with_candidates(&command, Path::new(r"C:\work"), candidates, |candidate| { + attempted.push(candidate.get_program().to_string_lossy().into_owned()); + if candidate.get_program() == "cmd" { + Ok(()) + } else { + Err(std::io::Error::from(std::io::ErrorKind::NotFound)) + } + }) + .unwrap(); + assert!(result); + assert_eq!(attempted, expected_attempts); +} diff --git a/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs b/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs index af93f72f0e4..be3afa5d20c 100644 --- a/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs +++ b/crates/jcode-tui/src/tui/app/swarm_panel_key_tests.rs @@ -1,4 +1,8 @@ -use super::{SwarmPanelAction, swarm_agent_shortcut_index, swarm_panel_action_for_key}; +use super::{ + SwarmPanelAction, swarm_agent_shortcut_index, swarm_panel_action_for_key, + swarm_popout_launch_context, +}; +use crate::tui::TuiState as _; use crossterm::event::{KeyCode, KeyModifiers}; #[test] @@ -40,6 +44,228 @@ fn direct_swarm_shortcuts_work_without_panel_focus_and_empty_swarm_is_safe() { assert!(app.handle_swarm_agent_shortcut(KeyCode::Char('p'), KeyModifiers::ALT)); } +#[test] +fn popout_request_preserves_selected_session_and_uses_connected_socket_only_locally() { + let socket = std::path::Path::new("/tmp/connected socket.sock"); + let context = swarm_popout_launch_context(false, socket); + assert!( + context + .runtime_env + .iter() + .any(|(key, value)| { key == "JCODE_SOCKET" && value == "/tmp/connected socket.sock" }) + ); + + let remote_context = swarm_popout_launch_context(true, socket); + assert!(remote_context.runtime_env.is_empty()); +} + +#[test] +fn local_popout_uses_shared_launcher_command_but_ssh_remains_client_mode() { + let exe = std::path::Path::new(r"C:\Program Files\jcode\jcode.exe"); + let context = jcode_app_core::session_launch::SessionSpawnContext::default() + .with_runtime_env(vec![("JCODE_SOCKET".into(), r"C:\connected.sock".into())]); + let local = super::swarm_agent_terminal_command(exe, "ses_local", &context); + assert_eq!(local.program, exe); + assert_eq!(local.args[0], "--fresh-spawn"); + assert_eq!(local.args[1], "--resume"); + assert_eq!(local.args[2], "ses_local"); + assert_eq!(&local.args[3..], ["--socket", r"C:\connected.sock"]); + assert_eq!(local.session_id.as_deref(), Some("ses_local")); + assert!(local.fresh_spawn); + assert_eq!( + local.runtime_env, + [("JCODE_SOCKET".into(), r"C:\connected.sock".into())] + ); + + let mut selected = None; + let result = super::spawn_swarm_agent_window_with_launchers( + exe, + "ses_local", + std::path::Path::new(r"C:\work"), + &context, + false, + |command, _cwd| { + selected = Some(command.clone()); + Ok(true) + }, + |_, _, _| panic!("local daemon popout must use the shared launcher"), + ) + .unwrap(); + assert!(result); + let selected = selected.unwrap(); + assert_eq!(selected.program, local.program); + assert_eq!(selected.args, local.args); + assert_eq!(selected.title, local.title); + assert_eq!(selected.fresh_spawn, local.fresh_spawn); + assert_eq!(selected.kind, local.kind); + assert_eq!(selected.session_id, local.session_id); + assert_eq!(selected.extra_env, local.extra_env); + assert_eq!(selected.runtime_env, local.runtime_env); + assert_eq!(selected.client_terminal_env, local.client_terminal_env); + + let mut ssh_called = false; + super::spawn_swarm_agent_window_with_launchers( + exe, + "ses_remote", + std::path::Path::new(r"C:\work"), + &context, + true, + |_, _| panic!("SSH popout must keep its existing client launcher"), + |_, session_id, _| { + ssh_called = true; + assert_eq!(session_id, "ses_remote"); + Ok(true) + }, + ) + .unwrap(); + assert!(ssh_called); +} + +#[test] +fn swarm_popout_daemon_client_preserves_runtime_and_ssh_stays_context_free() { + const CHILD: &str = "JCODE_TEST_SWARM_POPOUT_CHILD"; + let scenario = std::env::var(CHILD).ok(); + if scenario.is_none() { + let wrapper_home = tempfile::tempdir().expect("disposable wrapper home"); + let test_name = std::thread::current() + .name() + .expect("named test thread") + .to_owned(); + for (scenario, ssh_host) in [("daemon", None), ("ssh", Some("remote.example"))] { + let home = wrapper_home.path().join(scenario); + let jcode_home = home.join(".jcode"); + let runtime = home.join("runtime"); + std::fs::create_dir_all(&jcode_home).expect("create disposable JCODE_HOME"); + std::fs::create_dir_all(&runtime).expect("create disposable runtime"); + let socket = runtime.join("connected.sock"); + let mut command = std::process::Command::new(std::env::current_exe().unwrap()); + command + .args(["--exact", &test_name, "--nocapture"]) + .env_clear() + .env(CHILD, scenario) + .env("HOME", &home) + .env("JCODE_HOME", &jcode_home) + .env("JCODE_RUNTIME_DIR", &runtime) + .env("JCODE_SOCKET", &socket); + if let Some(ssh_host) = ssh_host { + command.env("JCODE_SSH_REMOTE", ssh_host); + } + let output = command + .output() + .expect("run isolated pop-out regression child"); + assert!( + output.status.success(), + "isolated {scenario} pop-out regression failed:\n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!( + String::from_utf8_lossy(&output.stdout) + .contains(&format!("SWARM_POPOUT_{scenario}_VERIFIED")), + "isolated {scenario} assertions did not run:\n{}", + String::from_utf8_lossy(&output.stdout) + ); + } + return; + } + + let scenario = scenario.unwrap(); + let ssh = scenario == "ssh"; + assert_eq!(crate::tui::is_ssh_remote(), ssh); + + let coordinator_id = "ses_popout_coordinator"; + let mut app = + crate::tui::app::App::new_for_remote_with_options(Some(coordinator_id.to_string()), true); + assert!( + app.is_remote, + "normal and SSH TUI clients are remote clients" + ); + app.remote_session_id = Some(coordinator_id.to_string()); + app.swarm_enabled = true; + let member = |id: &str, name: &str| crate::protocol::SwarmMemberStatus { + session_id: id.to_string(), + friendly_name: Some(name.to_string()), + status: "running".to_string(), + detail: None, + task_label: None, + role: None, + is_headless: Some(true), + live_attachments: None, + status_age_secs: Some(1), + output_tail: None, + report_back_to_session_id: Some(coordinator_id.to_string()), + todo_progress: None, + todo_items: Vec::new(), + runtime: crate::protocol::SwarmMemberRuntime::default(), + }; + app.remote_swarm_members = vec![ + member("ses_popout_agent_a", "Agent A"), + member("ses_popout_agent_b", "Agent B"), + ]; + app.swarm_panel_selected = 1; + + let displayed = app.inline_swarm_members(); + assert_eq!( + displayed.len(), + 2, + "fixture reaches the inline display filter" + ); + assert_eq!(app.swarm_panel_selected(), 1); + let display_order = crate::tui::info_widget::swarm_gallery::members_display_order(&displayed); + let selected_id = display_order[app.swarm_panel_selected].clone(); + let selected_label = displayed + .iter() + .find(|member| member.session_id == selected_id) + .and_then(|member| member.friendly_name.as_deref()) + .expect("selected display member label"); + + let mut captured = None; + app.pop_out_selected_swarm_agent_with_launcher(|exe, session_id, cwd, context| { + captured = Some(( + exe.to_path_buf(), + session_id.to_string(), + cwd.to_path_buf(), + context.clone(), + )); + Ok(true) + }); + let (exe, session_id, cwd, context) = captured.expect("production launch request captured"); + assert_eq!(exe, super::launch_client_executable()); + assert_eq!(cwd, std::env::current_dir().unwrap()); + assert_eq!( + session_id, selected_id, + "pop-out uses selected display member" + ); + assert_eq!( + app.status_notice().as_deref(), + Some(format!("Opened {selected_label} in a new window").as_str()), + "success notice is constructed by the production pop-out path" + ); + + if ssh { + assert!(context.kind.is_none()); + assert!(context.extra_env.is_empty()); + assert!(context.runtime_env.is_empty()); + assert!(context.client_terminal_env.is_empty()); + println!("SWARM_POPOUT_ssh_VERIFIED"); + } else { + let runtime = std::env::var("JCODE_RUNTIME_DIR").unwrap(); + let home = std::env::var("JCODE_HOME").unwrap(); + let socket = std::env::var("JCODE_SOCKET").unwrap(); + let runtime_value = |key: &str| { + context + .runtime_env + .iter() + .find_map(|(name, value)| (name == key).then_some(value.as_str())) + .unwrap_or_else(|| panic!("missing {key} from production launch context")) + }; + assert_eq!(runtime_value("JCODE_HOME"), home); + assert_eq!(runtime_value("JCODE_RUNTIME_DIR"), runtime); + assert_eq!(runtime_value("JCODE_SOCKET"), socket); + println!("SWARM_POPOUT_daemon_VERIFIED"); + } +} + /// Plain typing (letters, space, enter, arrows without alt) must pass /// through so the user can keep writing into the chat input while the /// panel is focused. diff --git a/crates/jcode-tui/src/tui/app/tui_state.rs b/crates/jcode-tui/src/tui/app/tui_state.rs index bde0e9be799..870686e471b 100644 --- a/crates/jcode-tui/src/tui/app/tui_state.rs +++ b/crates/jcode-tui/src/tui/app/tui_state.rs @@ -2469,7 +2469,7 @@ impl App { let mut opened = 0; let mut failures = Vec::new(); for session_id in order { - match open_swarm_agent_window(&session_id) { + match open_swarm_agent_window(&session_id, crate::tui::is_ssh_remote()) { Ok(true) => opened += 1, Ok(false) => failures.push(format!("{session_id}: no terminal emulator found")), Err(error) => failures.push(format!("{session_id}: {error}")), @@ -2496,6 +2496,20 @@ impl App { /// Open the currently selected swarm agent's session in a new terminal /// window (pop-out), reusing the resume-in-new-terminal launcher. pub(crate) fn pop_out_selected_swarm_agent(&mut self) { + self.pop_out_selected_swarm_agent_with_launcher(|exe, session_id, cwd, context| { + spawn_swarm_agent_window(exe, session_id, cwd, context, crate::tui::is_ssh_remote()) + }); + } + + fn pop_out_selected_swarm_agent_with_launcher( + &mut self, + launcher: impl FnMut( + &std::path::Path, + &str, + &std::path::Path, + &jcode_app_core::session_launch::SessionSpawnContext, + ) -> anyhow::Result, + ) { let members = self.inline_swarm_members(); if members.is_empty() { self.set_status_notice("No swarm agents to open"); @@ -2513,7 +2527,11 @@ impl App { .and_then(|m| m.friendly_name.clone()) .unwrap_or_else(|| session_id.chars().take(8).collect()); - match open_swarm_agent_window(&session_id) { + match open_swarm_agent_window_with_launcher( + &session_id, + crate::tui::is_ssh_remote(), + launcher, + ) { Ok(true) => self.set_status_notice(format!("Opened {label} in a new window")), Ok(false) => self.set_status_notice(format!( "Could not open a terminal for {label} (no emulator found)" @@ -2523,7 +2541,26 @@ impl App { } } -fn open_swarm_agent_window(session_id: &str) -> anyhow::Result { +fn open_swarm_agent_window(session_id: &str, is_ssh_remote: bool) -> anyhow::Result { + open_swarm_agent_window_with_launcher( + session_id, + is_ssh_remote, + |exe, session_id, cwd, context| { + spawn_swarm_agent_window(exe, session_id, cwd, context, is_ssh_remote) + }, + ) +} + +fn open_swarm_agent_window_with_launcher( + session_id: &str, + is_ssh_remote: bool, + mut launcher: impl FnMut( + &std::path::Path, + &str, + &std::path::Path, + &jcode_app_core::session_launch::SessionSpawnContext, + ) -> anyhow::Result, +) -> anyhow::Result { let exe = super::launch_client_executable(); let cwd = crate::session::Session::load_startup_stub(session_id) .ok() @@ -2531,8 +2568,81 @@ fn open_swarm_agent_window(session_id: &str) -> anyhow::Result { .map(std::path::PathBuf::from) .filter(|path| path.is_dir()) .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| ".".into())); - let socket = std::env::var("JCODE_SOCKET").ok(); - super::spawn_in_new_terminal(&exe, session_id, &cwd, socket.as_deref()) + let context = swarm_popout_launch_context(is_ssh_remote, &crate::server::socket_path()); + launcher(&exe, session_id, &cwd, &context) +} + +fn spawn_swarm_agent_window( + exe: &std::path::Path, + session_id: &str, + cwd: &std::path::Path, + context: &jcode_app_core::session_launch::SessionSpawnContext, + is_ssh_remote: bool, +) -> anyhow::Result { + spawn_swarm_agent_window_with_launchers( + exe, + session_id, + cwd, + context, + is_ssh_remote, + |command, cwd| jcode_app_core::terminal_launch::spawn_command_in_new_terminal(command, cwd), + |exe, session_id, cwd| { + let socket = std::env::var("JCODE_SOCKET").ok(); + super::spawn_in_new_terminal(exe, session_id, cwd, socket.as_deref()) + }, + ) +} + +fn swarm_agent_terminal_command( + exe: &std::path::Path, + session_id: &str, + context: &jcode_app_core::session_launch::SessionSpawnContext, +) -> jcode_app_core::terminal_launch::TerminalCommand { + let title = jcode_app_core::session_launch::resumed_window_title(session_id); + let mut args = vec!["--fresh-spawn".into(), "--resume".into(), session_id.into()]; + if let Some(socket) = context + .runtime_env + .iter() + .find(|(key, _)| key == "JCODE_SOCKET") + .map(|(_, value)| value) + { + args.extend(["--socket".into(), socket.clone()]); + } + let mut command = jcode_app_core::terminal_launch::TerminalCommand::new(exe, args) + .title(title) + .fresh_spawn() + .kind("resume") + .session_id(session_id); + for (key, value) in &context.extra_env { + command = command.spawn_env(key.clone(), value.clone()); + } + if !context.runtime_env.is_empty() { + command = command.runtime_env(context.runtime_env.clone()); + } + if !context.client_terminal_env.is_empty() { + command = command.client_terminal_env(context.client_terminal_env.clone()); + } + command +} + +fn spawn_swarm_agent_window_with_launchers( + exe: &std::path::Path, + session_id: &str, + cwd: &std::path::Path, + context: &jcode_app_core::session_launch::SessionSpawnContext, + is_ssh_remote: bool, + mut local_launcher: impl FnMut( + &jcode_app_core::terminal_launch::TerminalCommand, + &std::path::Path, + ) -> anyhow::Result, + mut ssh_launcher: impl FnMut(&std::path::Path, &str, &std::path::Path) -> anyhow::Result, +) -> anyhow::Result { + if is_ssh_remote { + ssh_launcher(exe, session_id, cwd) + } else { + let command = swarm_agent_terminal_command(exe, session_id, context); + local_launcher(&command, cwd) + } } /// Decode direct agent badges without capturing plain digits or other chords. @@ -2551,6 +2661,24 @@ pub(crate) fn swarm_agent_shortcut_index( } } +/// Runtime context for opening a swarm agent's window: local daemon clients +/// carry their effective runtime so the new terminal connects back to the same +/// one; SSH clients keep the historical context-free behavior. +fn swarm_popout_launch_context( + is_ssh_remote: bool, + socket: &std::path::Path, +) -> jcode_app_core::session_launch::SessionSpawnContext { + if is_ssh_remote { + jcode_app_core::session_launch::SessionSpawnContext::default() + } else { + jcode_app_core::session_launch::SessionSpawnContext::default() + .with_local_runtime(socket) + .with_client_terminal_env( + jcode_app_core::terminal_launch::snapshot_client_terminal_env(), + ) + } +} + /// What a key press should do while the swarm panel is focused. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum SwarmPanelAction { diff --git a/docs/SPAWN_HOOK.md b/docs/SPAWN_HOOK.md index d147f8fbaba..48e7d2467ed 100644 --- a/docs/SPAWN_HOOK.md +++ b/docs/SPAWN_HOOK.md @@ -61,6 +61,30 @@ The hook (and any terminal spawned by the built-in fallback) receives: | `JCODE_SPAWN_COORDINATOR_SESSION_ID` | (swarm spawns) The coordinator session that requested the spawn | | `JCODE_FRESH_SPAWN` | `1` when the spawn is a fresh window handoff | +When a spawn carries explicit local runtime context (`JCODE_HOME`, +`JCODE_RUNTIME_DIR`, `JCODE_SOCKET`, or the other allowlisted runtime +variables), the appended argv is a runtime-aware invocation, not just the bare +jcode program: on Unix it begins with `env`, clears inherited API-socket and +auto-update overrides when they were not explicitly supplied, applies the +allowlisted values, then executes jcode and its original arguments. This is +necessary for remote-control hooks such as kitty, where the hook process's +environment is not inherited by a child launched by the already-running kitty +server. Hooks must forward and execute all appended argv entries unchanged. +For example: + +```bash +kitty @ --to unix:/tmp/kitty.sock launch --type=tab -- "$@" +``` + +On Windows the appended argv stays the plain ` ` +sequence; the runtime context reaches the hook through its process +environment (`JCODE_*` variables) instead. + +With no explicit runtime context, argv remains the historical +` ` sequence. `JCODE_SPAWN_COMMAND` describes the full +runtime-aware command as a shell-escaped string for hooks that accept one +command string. + ### Client terminal environment (multi-terminal routing) The jcode server process is long-lived: it captures terminal-identifying env From 407221e4aa10a9b565ef3d2066f813552fba81f1 Mon Sep 17 00:00:00 2001 From: Sergii Maksymov Date: Sun, 8 Nov 2026 03:51:58 -0800 Subject: [PATCH 102/236] fix(tui): follow live terminal theme changes (OSC 11) and refresh the tmux background cache (#2047) --- crates/jcode-base/src/config/default_file.rs | 1 + crates/jcode-config-types/src/display.rs | 5 +- crates/jcode-tui-style/src/theme_mode.rs | 8 +- crates/jcode-tui/src/tui/app.rs | 4 +- crates/jcode-tui/src/tui/app/input.rs | 3 +- crates/jcode-tui/src/tui/app/local.rs | 14 +- crates/jcode-tui/src/tui/app/remote.rs | 10 +- .../jcode-tui/src/tui/app/remote/reconnect.rs | 2 +- crates/jcode-tui/src/tui/app/replay.rs | 3 +- crates/jcode-tui/src/tui/app/run_shell.rs | 14 + crates/jcode-tui/src/tui/mod.rs | 1 + crates/jcode-tui/src/tui/terminal_events.rs | 562 ++++++++++++++++++ .../src/tui/terminal_events_pty_tests.rs | 167 ++++++ crates/jcode-tui/src/tui/theme_detect.rs | 54 +- docs/TERMINAL_CAPABILITIES.md | 31 + 15 files changed, 852 insertions(+), 27 deletions(-) create mode 100644 crates/jcode-tui/src/tui/terminal_events.rs create mode 100644 crates/jcode-tui/src/tui/terminal_events_pty_tests.rs diff --git a/crates/jcode-base/src/config/default_file.rs b/crates/jcode-base/src/config/default_file.rs index 69414688a21..5ff3413c807 100644 --- a/crates/jcode-base/src/config/default_file.rs +++ b/crates/jcode-base/src/config/default_file.rs @@ -298,6 +298,7 @@ prompt_entry_animation = true # copy_badge_alt_label = "" # Color theme: "auto" (query the terminal background), "dark", or "light". +# Auto follows light/dark changes during the session (refreshes every 5 seconds). # theme = "auto" # Per-role color overrides. Every color the TUI renders is configurable: the diff --git a/crates/jcode-config-types/src/display.rs b/crates/jcode-config-types/src/display.rs index efe91f93f48..6fc29f0c22a 100644 --- a/crates/jcode-config-types/src/display.rs +++ b/crates/jcode-config-types/src/display.rs @@ -100,8 +100,9 @@ pub struct DisplayConfig { #[serde(default = "default_true")] pub keybinding_hints: bool, /// Color theme: "auto" (detect terminal background), "dark", or "light". - /// Auto queries the terminal's background color (OSC 11) at startup and - /// adapts jcode's palette for light backgrounds. Default: auto. + /// Auto queries the terminal's background color (OSC 11) at startup, + /// periodically during the session, and on focus. Adapts jcode's palette + /// when the terminal switches light/dark mode. Default: auto. #[serde(default)] pub theme: String, /// Per-role color overrides, e.g. `user = "#8ab4f8"`. Any TUI color can be diff --git a/crates/jcode-tui-style/src/theme_mode.rs b/crates/jcode-tui-style/src/theme_mode.rs index 402fae840a3..c7cc248a689 100644 --- a/crates/jcode-tui-style/src/theme_mode.rs +++ b/crates/jcode-tui-style/src/theme_mode.rs @@ -12,8 +12,8 @@ //! the terminal's own (light) default background shows through, exactly like //! it does on dark themes today. //! -//! The mode itself is set once at startup by the TUI's terminal-background -//! detection (OSC 11 query / `JCODE_THEME` / `display.theme` config) and +//! The mode is initialized by the TUI's terminal-background detection and +//! refreshed during auto-theme sessions (OSC 11 / `JCODE_THEME` / config). It //! defaults to dark, which keeps every existing code path byte-identical. use ratatui::buffer::Buffer; @@ -32,8 +32,8 @@ pub enum ThemeMode { static THEME_MODE: AtomicU8 = AtomicU8::new(0); -/// Set the global theme mode. Called once at startup after terminal -/// background detection (and again if the user overrides it). +/// Set the global theme mode at startup and when the terminal background +/// changes during an auto-theme session (or the user overrides it). pub fn set_theme_mode(mode: ThemeMode) { THEME_MODE.store( match mode { diff --git a/crates/jcode-tui/src/tui/app.rs b/crates/jcode-tui/src/tui/app.rs index b47f61ff8cd..19904e814de 100644 --- a/crates/jcode-tui/src/tui/app.rs +++ b/crates/jcode-tui/src/tui/app.rs @@ -23,11 +23,11 @@ use crate::session::{Session, StoredMessage}; use crate::skill::SkillRegistry; use crate::tool::selfdev::ReloadContext; use crate::tool::{Registry, ToolContext}; +use crate::tui::terminal_events::EventStream; use anyhow::Result; use auth::PendingLogin; use crossterm::event::{ - Event, EventStream, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, - MouseEventKind, + Event, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, MouseEventKind, }; use debug::DebugTrace; use futures::StreamExt; diff --git a/crates/jcode-tui/src/tui/app/input.rs b/crates/jcode-tui/src/tui/app/input.rs index 9d6159916b0..8e40c6cbd26 100644 --- a/crates/jcode-tui/src/tui/app/input.rs +++ b/crates/jcode-tui/src/tui/app/input.rs @@ -16,10 +16,11 @@ use crate::bus::{ Bus, BusEvent, ClipboardPasteCompleted, ClipboardPasteContent, ClipboardPasteKind, InputShellCompleted, }; +use crate::tui::terminal_events::EventStream; use crate::util::truncate_str; use anyhow::Result; use base64::Engine; -use crossterm::event::{EventStream, KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use ratatui::DefaultTerminal; use std::path::PathBuf; use std::process::Stdio; diff --git a/crates/jcode-tui/src/tui/app/local.rs b/crates/jcode-tui/src/tui/app/local.rs index 67d4aa1e560..51daccd9090 100644 --- a/crates/jcode-tui/src/tui/app/local.rs +++ b/crates/jcode-tui/src/tui/app/local.rs @@ -8,8 +8,9 @@ use crate::message::{ format_background_task_notification_markdown, }; use crate::session::StoredDisplayRole; +use crate::tui::terminal_events::EventStream; use anyhow::Result; -use crossterm::event::{Event, EventStream, KeyEventKind}; +use crossterm::event::{Event, KeyEventKind}; use ratatui::DefaultTerminal; use std::time::{Duration, Instant}; use tokio::sync::broadcast::Receiver; @@ -90,7 +91,9 @@ pub(super) fn handle_tick(app: &mut App) -> bool { // draw site. Excluding it here instead would mean animation ticks request // no paint at all, which drops the animation to whatever unrelated events // happen to trigger (~4fps in practice). - let mut needs_redraw = reset_redraw | crate::tui::periodic_redraw_required(app); + let mut needs_redraw = reset_redraw + | crate::tui::periodic_redraw_required(app) + | crate::tui::terminal_events::theme_redraw_pending(); needs_redraw |= app.poll_oauth_account_telemetry(); needs_redraw |= app.retire_expired_status_notice(); needs_redraw |= app.flush_pending_resize_redraw(); @@ -189,11 +192,10 @@ pub(super) fn handle_terminal_event( let mut needs_redraw = apply_terminal_event(app, terminal, event)?; const MAX_DRAINED_EVENTS_PER_WAKE: usize = 32; for _ in 0..MAX_DRAINED_EVENTS_PER_WAKE { - if !crossterm::event::poll(std::time::Duration::ZERO).unwrap_or(false) { - break; - } - if let Ok(event) = crossterm::event::read() { + if let Ok(Some(event)) = crate::tui::terminal_events::try_read() { needs_redraw |= apply_terminal_event(app, terminal, Some(Ok(event)))?; + } else { + break; } } Ok(needs_redraw) diff --git a/crates/jcode-tui/src/tui/app/remote.rs b/crates/jcode-tui/src/tui/app/remote.rs index 4d33857856a..f78c002cb4d 100644 --- a/crates/jcode-tui/src/tui/app/remote.rs +++ b/crates/jcode-tui/src/tui/app/remote.rs @@ -109,7 +109,8 @@ pub(super) async fn handle_tick(app: &mut App, remote: &mut RemoteConnection) -> .as_ref() .is_some_and(|state| state.kind == crate::tui::PickerKind::Model), }); - let mut needs_redraw = crate::tui::periodic_redraw_required(app); + let mut needs_redraw = crate::tui::periodic_redraw_required(app) + | crate::tui::terminal_events::theme_redraw_pending(); needs_redraw |= app.poll_pending_startup_transcript(); if remote.has_loaded_history() && let Err(error) = remote.poll_account_usage().await @@ -527,11 +528,10 @@ pub(super) async fn handle_terminal_event( // lag. Mirrors the identical drain in `local::handle_terminal_event`. const MAX_DRAINED_EVENTS_PER_WAKE: usize = 32; for _ in 0..MAX_DRAINED_EVENTS_PER_WAKE { - if !crossterm::event::poll(std::time::Duration::ZERO).unwrap_or(false) { - break; - } - if let Ok(event) = crossterm::event::read() { + if let Ok(Some(event)) = crate::tui::terminal_events::try_read() { needs_redraw |= apply_terminal_event(app, terminal, remote, Some(Ok(event))).await?; + } else { + break; } } Ok(needs_redraw) diff --git a/crates/jcode-tui/src/tui/app/remote/reconnect.rs b/crates/jcode-tui/src/tui/app/remote/reconnect.rs index 2dcf1e77e62..0501722452d 100644 --- a/crates/jcode-tui/src/tui/app/remote/reconnect.rs +++ b/crates/jcode-tui/src/tui/app/remote/reconnect.rs @@ -5,8 +5,8 @@ use super::{ use crate::tool::selfdev::ReloadContext; use crate::tui::app::PendingReloadReconnectStatus; use crate::tui::backend::{RemoteConnection, RemoteDisconnectReason}; +use crate::tui::terminal_events::EventStream; use anyhow::Result; -use crossterm::event::EventStream; use futures::StreamExt; use ratatui::DefaultTerminal; use std::time::{Duration, Instant}; diff --git a/crates/jcode-tui/src/tui/app/replay.rs b/crates/jcode-tui/src/tui/app/replay.rs index 638c9650fb0..c359583c9d6 100644 --- a/crates/jcode-tui/src/tui/app/replay.rs +++ b/crates/jcode-tui/src/tui/app/replay.rs @@ -1,8 +1,9 @@ use super::{App, DisplayMessage, ProcessingStatus, RunResult}; use crate::replay::{PaneReplayInput, ReplayEvent, TimelineEvent}; use crate::tui::backend::{RemoteEventState, ReplayRemoteState}; +use crate::tui::terminal_events::EventStream; use anyhow::Result; -use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind, KeyModifiers}; +use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers}; use futures::StreamExt; use ratatui::{ DefaultTerminal, Frame, Terminal, backend::TestBackend, buffer::Buffer, layout::Rect, diff --git a/crates/jcode-tui/src/tui/app/run_shell.rs b/crates/jcode-tui/src/tui/app/run_shell.rs index ab453b42765..21c41ba12ac 100644 --- a/crates/jcode-tui/src/tui/app/run_shell.rs +++ b/crates/jcode-tui/src/tui/app/run_shell.rs @@ -305,6 +305,7 @@ fn idle_animation_fast_path_blocked_reason( #[derive(Default)] pub(super) struct StatusSpinnerRenderer { last_frame: Option, + last_theme: jcode_tui_style::ThemeMode, last_full_frame_at: Option, /// Animated rectangle whose surrounding cells are currently seeded into /// ratatui's working buffer. @@ -374,6 +375,9 @@ impl StatusSpinnerRenderer { } pub(super) fn idle_animation_only_available(&self, app: &App) -> bool { + if self.last_theme != jcode_tui_style::theme_mode() { + return false; + } let blocked = idle_animation_fast_path_blocked_reason(&IdleAnimationFastPathInputs { has_previous_frame: self.last_frame.is_some(), animation_active: crate::tui::idle_donut_active(app), @@ -418,6 +422,9 @@ impl StatusSpinnerRenderer { app: &App, terminal: &mut DefaultTerminal, ) -> Result { + if self.last_theme != jcode_tui_style::theme_mode() { + return Ok(false); + } let Some(previous_frame) = self.last_frame.as_ref() else { return Ok(false); }; @@ -498,6 +505,9 @@ impl StatusSpinnerRenderer { ) -> Result<()> { // Painting a frame is progress, including during long streaming turns. crate::logging::watchdog::beat("tui.draw"); + if crate::tui::terminal_events::take_theme_redraw() { + app.force_full_repaint = true; + } app.refresh_terminal_title_metrics(); app.sync_herdr_agent_state(); app.poll_oauth_account_telemetry(); @@ -577,6 +587,7 @@ impl StatusSpinnerRenderer { if crate::tui::ui::last_idle_animation_area().is_some() { crate::tui::ui::note_idle_animation_full_repaint(); } + self.last_theme = jcode_tui_style::theme_mode(); self.last_frame = Some(completed_buffer); app.finish_idle_hibernation_frame(); self.last_full_frame_at = Some(Instant::now()); @@ -606,6 +617,9 @@ impl StatusSpinnerRenderer { if status_symbol.is_none() { return Ok(false); } + if self.last_theme != jcode_tui_style::theme_mode() { + return Ok(false); + } let Some(previous_frame) = self.last_frame.as_ref() else { return Ok(false); }; diff --git a/crates/jcode-tui/src/tui/mod.rs b/crates/jcode-tui/src/tui/mod.rs index 65d320d6f47..9d4e0ee3a5d 100644 --- a/crates/jcode-tui/src/tui/mod.rs +++ b/crates/jcode-tui/src/tui/mod.rs @@ -69,6 +69,7 @@ pub mod screenshot; pub(crate) mod session_facts; pub mod session_picker; mod stream_buffer; +mod terminal_events; pub mod terminal_setup; pub mod test_harness; pub mod theme_detect; diff --git a/crates/jcode-tui/src/tui/terminal_events.rs b/crates/jcode-tui/src/tui/terminal_events.rs new file mode 100644 index 00000000000..bb1bbee864d --- /dev/null +++ b/crates/jcode-tui/src/tui/terminal_events.rs @@ -0,0 +1,562 @@ +//! Runtime terminal-background queries using the existing crossterm reader. +//! +//! Never run colorsaurus once an event reader exists: two stdin readers race +//! and a color report can become composer input. Crossterm 0.29 decodes OSC +//! reports as Alt+] / ordinary characters / Alt+\\ (or Ctrl+G). Intercept them +//! before *any* picker, shortcut, or composer sees them. The async reader and +//! synchronous burst drains share this filter, just like crossterm's reader. + +use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers}; +use futures::Stream; +use jcode_tui_style::ThemeMode; +use std::collections::VecDeque; +use std::future::Future; +use std::io::{self, Write}; +use std::pin::Pin; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Mutex, OnceLock}; +use std::task::{Context, Poll}; +use std::time::{Duration, Instant}; + +const QUERY_INTERVAL: Duration = Duration::from_secs(5); +const PREFIX_TIMEOUT: Duration = Duration::from_millis(250); +const REPORT_TIMEOUT: Duration = Duration::from_secs(2); +const MAX_REPORT_LEN: usize = 40; + +static THEME_REDRAW_PENDING: AtomicBool = AtomicBool::new(false); + +pub(super) fn theme_redraw_pending() -> bool { + THEME_REDRAW_PENDING.load(Ordering::Relaxed) +} + +pub(super) fn take_theme_redraw() -> bool { + THEME_REDRAW_PENDING.swap(false, Ordering::Relaxed) +} + +#[derive(Default)] +struct ColorReports { + held: Vec, + body: String, + started: Option, + ready: VecDeque, + mode: Option, + expecting_until: Option, + split_escape: bool, +} + +impl ColorReports { + fn recognized(&self) -> bool { + self.body.starts_with("11;rgb:") || self.body.starts_with("11;rgba:") + } + + fn reset(&mut self, replay: bool) { + if replay { + self.ready.extend(self.held.drain(..)); + } else { + self.held.clear(); + } + self.body.clear(); + self.started = None; + self.split_escape = false; + } + + fn expire(&mut self, now: Instant) { + let timeout = if self.recognized() { + REPORT_TIMEOUT + } else { + PREFIX_TIMEOUT + }; + if self + .started + .is_some_and(|started| now.duration_since(started) >= timeout) + { + // Replay an ordinary Alt+] chord, but never replay half a report. + self.reset(!self.recognized()); + } + } + + fn push(&mut self, event: Event, now: Instant) { + self.expire(now); + let Event::Key(key) = &event else { + if !self.recognized() { + self.reset(true); + } + self.ready.push_back(event); + return; + }; + if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + self.ready.push_back(event); + return; + } + // A read ending exactly after ESC makes crossterm emit a bare Escape + // key rather than an Alt chord. Reassemble only an expected query's + // introducer, or the terminator of a report already being consumed. + if self.split_escape { + let plain = key.modifiers.is_empty(); + if self.body.is_empty() && key.code == KeyCode::Char(']') && plain { + self.split_escape = false; + self.held.push(event); + return; + } + if self.recognized() && key.code == KeyCode::Char('\\') && plain { + self.mode = parse_background_report(&self.body); + self.expecting_until = None; + self.reset(false); + return; + } + if self.recognized() { + let escape = self.held.pop().expect("held split Escape"); + self.reset(false); + self.ready.push_back(escape); + } else { + self.reset(true); + } + } + if key.code == KeyCode::Esc + && key.modifiers.is_empty() + && (self.recognized() + || (self.started.is_none() + && self.expecting_until.is_some_and(|until| now < until))) + { + self.started.get_or_insert(now); + self.split_escape = true; + self.held.push(event); + return; + } + if key.code == KeyCode::Char(']') && key.modifiers == KeyModifiers::ALT { + self.reset(!self.recognized()); + self.started = Some(now); + self.held.push(event); + return; + } + if self.started.is_none() { + self.ready.push_back(event); + return; + } + let terminator = (key.code == KeyCode::Char('\\') && key.modifiers == KeyModifiers::ALT) + || (key.code == KeyCode::Char('g') && key.modifiers == KeyModifiers::CONTROL); + if terminator && self.recognized() { + self.mode = parse_background_report(&self.body); + self.expecting_until = None; + self.reset(false); + return; + } + if let KeyCode::Char(ch) = key.code + && (key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT) + && ch.is_ascii() + { + self.body.push(ch); + self.held.push(event); + let prefix = "11;rgb:".starts_with(&self.body) || "11;rgba:".starts_with(&self.body); + if self.body.len() > MAX_REPORT_LEN || (!prefix && !self.recognized()) { + self.reset(!self.recognized()); + } + } else { + self.reset(!self.recognized()); + self.ready.push_back(event); + } + } +} + +fn parse_background_report(body: &str) -> Option { + let (rgb, count) = if let Some(rgb) = body.strip_prefix("11;rgb:") { + (rgb, 3) + } else { + (body.strip_prefix("11;rgba:")?, 4) + }; + let components: Vec<_> = rgb.split('/').collect(); + if components.len() != count { + return None; + } + let mut channels = Vec::with_capacity(count); + for component in components { + if !(1..=4).contains(&component.len()) || !component.bytes().all(|b| b.is_ascii_hexdigit()) + { + return None; + } + let value = u32::from_str_radix(component, 16).ok()?; + let max = (1u32 << (component.len() * 4)) - 1; + channels.push((value * u32::from(u16::MAX) / max) as u16); + } + let background = terminal_colorsaurus::Color::rgb(channels[0], channels[1], channels[2]); + Some(if background.perceived_lightness() > 0.5 { + ThemeMode::Light + } else { + ThemeMode::Dark + }) +} + +#[derive(Default)] +struct RuntimeTheme { + reports: ColorReports, + last_query: Option, + cache_query_at: Option, +} + +fn runtime() -> &'static Mutex { + static RUNTIME: OnceLock> = OnceLock::new(); + RUNTIME.get_or_init(|| Mutex::new(RuntimeTheme::default())) +} + +fn background_query(passthrough: bool) -> &'static [u8] { + if passthrough { + // tmux caches ordinary OSC 11 replies at attachment. Ask the outer + // terminal directly, doubling ESC bytes inside its DCS envelope. + b"\x1bPtmux;\x1b\x1b]11;?\x1b\x1b\\\x1b\\" + } else { + b"\x1b]11;?\x1b\\" + } +} + +fn tmux_passthrough_enabled() -> bool { + static ENABLED: OnceLock = OnceLock::new(); + *ENABLED.get_or_init(|| { + let term = std::env::var("TERM").unwrap_or_default(); + if std::env::var_os("TMUX").is_none() + || !(term.starts_with("tmux") || term.starts_with("screen")) + { + return false; + } + use std::process::{Command, Stdio}; + let Ok(mut child) = Command::new("tmux") + .args(["show-options", "-Apv", "allow-passthrough"]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + else { + return false; + }; + // Only once per TUI, bounded so a broken/missing mux cannot stall input. + let deadline = Instant::now() + Duration::from_millis(150); + loop { + match child.try_wait() { + Ok(Some(status)) if status.success() => { + return child + .wait_with_output() + .is_ok_and(|output| matches!(output.stdout.trim_ascii(), b"on" | b"all")); + } + Ok(Some(_)) => return false, + Ok(None) if Instant::now() < deadline => { + std::thread::sleep(Duration::from_millis(2)); + } + _ => { + let _ = child.kill(); + let _ = child.wait(); + return false; + } + } + } + }) +} + +fn query_if_due(state: &mut RuntimeTheme, now: Instant, focused: bool) { + if !super::theme_detect::runtime_queries_enabled() { + return; + } + if state.cache_query_at.is_some_and(|at| now >= at) { + state.cache_query_at = None; + // tmux consumes outer-terminal color replies to update its cache. + // Read that refreshed cache on the next timer tick, without blocking + // the input stream or competing for stdin. + write_background_query(state, now, false); + return; + } + let interval = if focused { + Duration::from_secs(1) + } else { + QUERY_INTERVAL + }; + if state + .last_query + .is_some_and(|last| now.duration_since(last) < interval) + { + return; + } + state.last_query = Some(now); + let passthrough = tmux_passthrough_enabled(); + if passthrough { + state.cache_query_at = Some(now + PREFIX_TIMEOUT); + } + write_background_query(state, now, passthrough); +} + +fn write_background_query(state: &mut RuntimeTheme, now: Instant, passthrough: bool) { + // Query only the background. No stdin read and no blocking response wait. + let query = background_query(passthrough); + let mut stdout = io::stdout().lock(); + if stdout + .write_all(query) + .and_then(|()| stdout.flush()) + .is_ok() + { + state.reports.expecting_until = Some(now + REPORT_TIMEOUT); + } +} + +fn take_event(state: &mut RuntimeTheme) -> Option { + if let Some(mode) = state.reports.mode.take() + && super::theme_detect::auto_theme_enabled() + && mode != jcode_tui_style::theme_mode() + { + jcode_tui_style::set_theme_mode(mode); + THEME_REDRAW_PENDING.store(true, Ordering::Relaxed); + // Reuse the existing resize invalidation/redraw path without inventing + // a keypress or changing focus state. The actual dimensions stay intact. + if let Ok((width, height)) = crossterm::terminal::size() { + return Some(Event::Resize(width, height)); + } + } + state.reports.ready.pop_front() +} + +/// Nonblocking burst drain with the same report filter as [`EventStream`]. +/// Returning None means there is currently no user event, even if report bytes +/// were consumed. The async reader will deliver any remaining report/replay. +pub(super) fn try_read() -> io::Result> { + let mut state = runtime().lock().unwrap_or_else(|error| error.into_inner()); + if let Some(event) = take_event(&mut state) { + return Ok(Some(event)); + } + for _ in 0..64 { + if !crossterm::event::poll(Duration::ZERO)? { + return Ok(None); + } + let event = crossterm::event::read()?; + let now = Instant::now(); + if event == Event::FocusGained { + query_if_due(&mut state, now, true); + } + state.reports.push(event, now); + if let Some(event) = take_event(&mut state) { + return Ok(Some(event)); + } + } + Ok(None) +} + +/// Crossterm's event stream, with nonblocking periodic OSC 11 queries and report +/// filtering. The timer is polled even during LLM output and tool execution. +pub(super) struct EventStream { + inner: crossterm::event::EventStream, + tick: Pin>, +} + +impl EventStream { + pub(super) fn new() -> Self { + Self { + inner: crossterm::event::EventStream::new(), + tick: Box::pin(tokio::time::sleep(Duration::ZERO)), + } + } +} + +impl Stream for EventStream { + type Item = io::Result; + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let mut state = runtime().lock().unwrap_or_else(|error| error.into_inner()); + // Poll the timer first so a continuously-ready stream of input cannot + // starve a theme refresh. It only writes a tiny query every five seconds. + if self.tick.as_mut().poll(cx).is_ready() { + let now = Instant::now(); + state.reports.expire(now); + query_if_due(&mut state, now, false); + self.tick + .as_mut() + .reset(tokio::time::Instant::now() + PREFIX_TIMEOUT); + let _ = self.tick.as_mut().poll(cx); + } + if let Some(event) = take_event(&mut state) { + return Poll::Ready(Some(Ok(event))); + } + // Bound the work per poll while draining the key-shaped color report. + for _ in 0..64 { + match Pin::new(&mut self.inner).poll_next(cx) { + Poll::Ready(Some(Ok(event))) => { + let now = Instant::now(); + if event == Event::FocusGained { + query_if_due(&mut state, now, true); + } + state.reports.push(event, now); + if let Some(event) = take_event(&mut state) { + return Poll::Ready(Some(Ok(event))); + } + } + other => return other, + } + } + cx.waker().wake_by_ref(); + Poll::Pending + } +} + +#[cfg(test)] +mod tests { + #[test] + fn background_queries_bypass_tmux_cache_only_with_passthrough() { + assert_eq!(super::background_query(false), b"\x1b]11;?\x1b\\"); + assert_eq!( + super::background_query(true), + b"\x1bPtmux;\x1b\x1b]11;?\x1b\x1b\\\x1b\\" + ); + } + use super::*; + use crossterm::event::KeyEvent; + + fn key(ch: char, modifiers: KeyModifiers) -> Event { + Event::Key(KeyEvent::new(KeyCode::Char(ch), modifiers)) + } + + fn report(filter: &mut ColorReports, body: &str, bel: bool, now: Instant) { + filter.push(key(']', KeyModifiers::ALT), now); + for ch in body.chars() { + filter.push(key(ch, KeyModifiers::NONE), now); + } + filter.push( + if bel { + key('g', KeyModifiers::CONTROL) + } else { + key('\\', KeyModifiers::ALT) + }, + now, + ); + } + + #[test] + fn osc_reports_switch_both_directions_without_becoming_input() { + let mut filter = ColorReports::default(); + let now = Instant::now(); + report(&mut filter, "11;rgb:ffff/ffff/ffff", false, now); + assert_eq!(filter.mode.take(), Some(ThemeMode::Light)); + assert!(filter.ready.is_empty()); + report(&mut filter, "11;rgb:00/00/00", true, now); + assert_eq!(filter.mode.take(), Some(ThemeMode::Dark)); + assert!(filter.ready.is_empty()); + } + + #[test] + fn ordinary_alt_chords_and_pastes_survive() { + let mut filter = ColorReports::default(); + let now = Instant::now(); + let events = [ + key(']', KeyModifiers::ALT), + key('x', KeyModifiers::NONE), + Event::Paste("11;rgb:ff/ff/ff".into()), + ]; + for event in &events { + filter.push(event.clone(), now); + } + assert_eq!(filter.ready.into_iter().collect::>(), events); + assert_eq!(filter.mode, None); + } + + #[test] + fn fragmented_report_is_held_until_terminator() { + let mut filter = ColorReports::default(); + let now = Instant::now(); + filter.push(key(']', KeyModifiers::ALT), now); + for (i, ch) in "11;rgb:ffff/ffff/ffff".chars().enumerate() { + filter.push( + key(ch, KeyModifiers::NONE), + now + Duration::from_millis(i as u64 * 10), + ); + assert!(filter.ready.is_empty()); + assert_eq!(filter.mode, None); + } + filter.push( + key('\\', KeyModifiers::ALT), + now + Duration::from_millis(230), + ); + assert_eq!(filter.mode, Some(ThemeMode::Light)); + } + + #[test] + fn reassembles_split_escape_introducer_and_terminator() { + let now = Instant::now(); + let mut filter = ColorReports { + expecting_until: Some(now + REPORT_TIMEOUT), + ..ColorReports::default() + }; + let escape = Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + filter.push(escape.clone(), now); + filter.push( + key(']', KeyModifiers::NONE), + now + Duration::from_millis(20), + ); + for ch in "11;rgb:ffff/ffff/ffff".chars() { + filter.push(key(ch, KeyModifiers::NONE), now + Duration::from_millis(40)); + } + filter.push(escape, now + Duration::from_millis(60)); + filter.push( + key('\\', KeyModifiers::NONE), + now + Duration::from_millis(80), + ); + assert_eq!(filter.mode, Some(ThemeMode::Light)); + assert!(filter.ready.is_empty()); + assert!(filter.expecting_until.is_none()); + } + + #[test] + fn genuine_escape_is_replayed_and_is_not_delayed_outside_a_query() { + let now = Instant::now(); + let escape = Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + let mut filter = ColorReports::default(); + filter.push(escape.clone(), now); + assert_eq!(filter.ready.pop_front(), Some(escape.clone())); + filter.expecting_until = Some(now + REPORT_TIMEOUT); + filter.push(escape.clone(), now); + filter.expire(now + PREFIX_TIMEOUT); + assert_eq!(filter.ready.pop_front(), Some(escape.clone())); + filter.push(escape.clone(), now); + filter.push( + key('x', KeyModifiers::NONE), + now + Duration::from_millis(20), + ); + assert_eq!(filter.ready.pop_front(), Some(escape)); + assert_eq!(filter.ready.pop_front(), Some(key('x', KeyModifiers::NONE))); + } + + #[test] + fn timeout_replays_unrecognized_chord_but_drops_partial_report() { + let now = Instant::now(); + let mut filter = ColorReports::default(); + filter.push(key(']', KeyModifiers::ALT), now); + filter.expire(now + PREFIX_TIMEOUT); + assert_eq!(filter.ready.pop_front(), Some(key(']', KeyModifiers::ALT))); + filter.push(key(']', KeyModifiers::ALT), now); + for ch in "11;rgb:ff/".chars() { + filter.push(key(ch, KeyModifiers::NONE), now); + } + filter.expire(now + REPORT_TIMEOUT); + assert!(filter.ready.is_empty()); + filter.push(key('x', KeyModifiers::NONE), now + REPORT_TIMEOUT); + assert_eq!(filter.ready.pop_front(), Some(key('x', KeyModifiers::NONE))); + } + + #[test] + fn validates_color_components_and_ignores_alpha() { + assert_eq!( + parse_background_report("11;rgba:ffff/ffff/ffff/0000"), + Some(ThemeMode::Light) + ); + assert_eq!( + parse_background_report("11;rgb:f/f/f"), + Some(ThemeMode::Light) + ); + for body in [ + "10;rgb:ff/ff/ff", + "11;rgb:/ff/ff", + "11;rgb:fffff/ff/ff", + "11;rgb:gg/ff/ff", + "11;rgb:ff/ff", + "11;rgb:ff/ff/ff/ff", + ] { + assert_eq!(parse_background_report(body), None, "{body}"); + } + } +} + +#[cfg(all(test, unix))] +#[path = "terminal_events_pty_tests.rs"] +mod pty_tests; diff --git a/crates/jcode-tui/src/tui/terminal_events_pty_tests.rs b/crates/jcode-tui/src/tui/terminal_events_pty_tests.rs new file mode 100644 index 00000000000..8a824c0950b --- /dev/null +++ b/crates/jcode-tui/src/tui/terminal_events_pty_tests.rs @@ -0,0 +1,167 @@ +//! End-to-end PTY check, in a fresh process so crossterm's process-global +//! terminal reader cannot race other unit tests or inherit locked mutexes. + +use super::*; +use std::io::{Read, Write}; +use std::os::fd::{AsRawFd, FromRawFd}; +use std::os::unix::process::CommandExt; +use std::process::{Command, Stdio}; + +async fn next_event(events: &mut EventStream) -> Event { + use futures::StreamExt; + tokio::time::timeout(Duration::from_secs(10), events.next()) + .await + .unwrap() + .unwrap() + .unwrap() +} + +#[test] +fn pty_child() { + if std::env::var_os("JCODE_TEST_THEME_PTY").is_none() { + return; + } + crossterm::terminal::enable_raw_mode().unwrap(); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + runtime.block_on(async { + jcode_tui_style::set_theme_mode(ThemeMode::Dark); + let mut events = EventStream::new(); + assert!(matches!( + next_event(&mut events).await, + Event::Resize(80, 24) + )); + assert_eq!(jcode_tui_style::theme_mode(), ThemeMode::Light); + assert_eq!( + next_event(&mut events).await, + Event::Key(crossterm::event::KeyEvent::new( + KeyCode::Char('x'), + KeyModifiers::NONE + )) + ); + // The next reply is only sent when the periodic query appears, while + // the process is otherwise idle. No user message or redraw is needed. + assert!(matches!( + next_event(&mut events).await, + Event::Resize(80, 24) + )); + assert_eq!(jcode_tui_style::theme_mode(), ThemeMode::Dark); + // Terminal paste must remain ordinary input, even when it contains a + // string resembling a color report. + assert_eq!( + next_event(&mut events).await, + Event::Paste("11;rgb:ff/ff/ff".to_string()) + ); + }); + println!("PTY_THEME_OK"); +} + +#[test] +fn live_queries_and_fragmented_color_replies_work_through_crossterm() { + let mut master = -1; + let mut slave = -1; + let size = libc::winsize { + ws_row: 24, + ws_col: 80, + ws_xpixel: 0, + ws_ypixel: 0, + }; + // SAFETY: openpty initializes both descriptors, with no name or termios. + assert_eq!( + unsafe { + libc::openpty( + &mut master, + &mut slave, + std::ptr::null_mut(), + std::ptr::null(), + &size, + ) + }, + 0 + ); + // SAFETY: these descriptors are newly allocated and exclusively owned. + let mut master = unsafe { std::fs::File::from_raw_fd(master) }; + let slave = unsafe { std::fs::File::from_raw_fd(slave) }; + let mut command = Command::new(std::env::current_exe().unwrap()); + command + .args([ + "--exact", + "tui::terminal_events::pty_tests::pty_child", + "--nocapture", + ]) + .env("JCODE_TEST_THEME_PTY", "1") + .env("JCODE_THEME", "auto") + .env("TERM", "xterm-256color") + .stdin(Stdio::from(slave.try_clone().unwrap())) + .stdout(Stdio::from(slave.try_clone().unwrap())) + .stderr(Stdio::from(slave)); + // SAFETY: child-only controlling-terminal setup immediately before exec. + // No allocation, locks, or runtime activity occurs in this closure. + unsafe { + command.pre_exec(|| { + if libc::login_tty(libc::STDIN_FILENO) != 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) + }); + } + let mut child = command.spawn().unwrap(); + let deadline = Instant::now() + Duration::from_secs(20); + let mut output = Vec::new(); + let mut query_count = 0; + let status = loop { + if let Some(status) = child.try_wait().unwrap() { + break status; + } + if Instant::now() >= deadline { + child.kill().unwrap(); + child.wait().unwrap(); + panic!("PTY test timed out: {}", String::from_utf8_lossy(&output)); + } + let mut fd = libc::pollfd { + fd: master.as_raw_fd(), + events: libc::POLLIN, + revents: 0, + }; + // SAFETY: fd describes the live master, the array length is one. + if unsafe { libc::poll(&mut fd, 1, 50) } <= 0 || fd.revents & libc::POLLIN == 0 { + continue; + } + let mut bytes = [0; 4096]; + match master.read(&mut bytes) { + Ok(0) => continue, + Ok(len) => output.extend_from_slice(&bytes[..len]), + Err(error) if error.raw_os_error() == Some(libc::EIO) => continue, + Err(error) => panic!("PTY read failed: {error}"), + } + let seen = output + .windows(b"\x1b]11;?\x1b\\".len()) + .filter(|window| *window == b"\x1b]11;?\x1b\\") + .count(); + if seen > query_count { + query_count = seen; + if seen == 1 { + // Split every byte, including immediately after ESC. This + // exercises crossterm's bare-Escape fallback on SSH fragments. + for byte in b"\x1b]11;rgb:ffff/ffff/ffff\x1b\\x" { + master.write_all(&[*byte]).unwrap(); + master.flush().unwrap(); + std::thread::sleep(Duration::from_millis(20)); + } + } else if seen == 2 { + master + .write_all(b"\x1b]11;rgb:0000/0000/0000\x07\x1b[200~11;rgb:ff/ff/ff\x1b[201~") + .unwrap(); + master.flush().unwrap(); + } + } + }; + assert!( + status.success(), + "PTY child failed: {}", + String::from_utf8_lossy(&output) + ); + assert!(query_count >= 2, "periodic query was not emitted"); +} diff --git a/crates/jcode-tui/src/tui/theme_detect.rs b/crates/jcode-tui/src/tui/theme_detect.rs index ece963d6567..c9996bd6271 100644 --- a/crates/jcode-tui/src/tui/theme_detect.rs +++ b/crates/jcode-tui/src/tui/theme_detect.rs @@ -1,6 +1,7 @@ //! Terminal light/dark theme detection. //! -//! Resolves the theme mode once per process, before the TUI enters raw mode: +//! Resolves the initial theme before the TUI enters raw mode. Auto mode is +//! refreshed by `terminal_events` during the session using the existing reader: //! //! 1. `JCODE_THEME=dark|light` env override (also accepts `auto`). //! 2. `display.theme` config: "dark", "light", or "auto"/empty. @@ -57,6 +58,10 @@ fn take_prewarmed_theme_mode() -> Option { /// the (potentially blocking, sub-second) terminal query and later calls are /// free. Must be called before entering raw mode / the alternate screen. pub fn init_theme_mode() -> ThemeMode { + if DETECTED.get().is_some() { + init_palette(); + return jcode_tui_style::theme_mode(); + } let mode = match take_prewarmed_theme_mode() { Some(prewarmed) => *DETECTED.get_or_init(|| prewarmed), None => *DETECTED.get_or_init(resolve_theme_mode), @@ -74,6 +79,10 @@ pub fn init_theme_mode() -> ThemeMode { /// input. Prefer the theme captured by the previous process and otherwise resolve /// configuration without querying the terminal. pub fn init_theme_mode_for_resume(inherited_theme: Option<&str>) -> ThemeMode { + if DETECTED.get().is_some() { + init_palette(); + return jcode_tui_style::theme_mode(); + } let inherited_theme = inherited_theme.and_then(|value| match value { "dark" => Some(ThemeMode::Dark), "light" => Some(ThemeMode::Light), @@ -125,13 +134,38 @@ fn resolve_theme_mode_without_terminal_query() -> ThemeMode { resolve_configured_theme(false) } -fn resolve_configured_theme(query_terminal: bool) -> ThemeMode { - let configured = std::env::var("JCODE_THEME") +fn configured_theme() -> String { + std::env::var("JCODE_THEME") .ok() .filter(|v| !v.trim().is_empty()) - .unwrap_or_else(|| crate::config::config().display.theme.clone()); + .unwrap_or_else(|| crate::config::config().display.theme.clone()) +} + +fn is_auto_theme(configured: &str) -> bool { + !matches!( + configured.trim().to_ascii_lowercase().as_str(), + "dark" | "light" + ) +} + +pub(super) fn auto_theme_enabled() -> bool { + is_auto_theme(&configured_theme()) +} - match configured.trim().to_ascii_lowercase().as_str() { +pub(super) fn runtime_queries_enabled() -> bool { + use std::io::IsTerminal; + auto_theme_enabled() + && std::io::stdin().is_terminal() + && std::io::stdout().is_terminal() + && terminal_background_query_supported( + std::env::var("TERM").ok().as_deref(), + std::env::var("TERM_PROGRAM").ok().as_deref(), + std::env::var("LC_TERMINAL").ok().as_deref(), + ) +} + +fn resolve_configured_theme(query_terminal: bool) -> ThemeMode { + match configured_theme().trim().to_ascii_lowercase().as_str() { "dark" => return ThemeMode::Dark, "light" => return ThemeMode::Light, "" | "auto" => {} @@ -318,6 +352,16 @@ mod tests { terminal_background_query_supported, }; + #[test] + fn explicit_theme_overrides_disable_runtime_detection() { + for value in ["dark", "light", " LIGHT ", "Dark"] { + assert!(!super::is_auto_theme(value), "{value}"); + } + for value in ["", "auto", " AUTO ", "unknown"] { + assert!(super::is_auto_theme(value), "{value}"); + } + } + #[test] fn skips_terminals_without_osc_query_support() { for term in [None, Some(""), Some("dumb"), Some("linux"), Some("cons25")] { diff --git a/docs/TERMINAL_CAPABILITIES.md b/docs/TERMINAL_CAPABILITIES.md index 179ae3d9d9a..a0419fcaa40 100644 --- a/docs/TERMINAL_CAPABILITIES.md +++ b/docs/TERMINAL_CAPABILITIES.md @@ -121,6 +121,37 @@ tmux is the most common source of rendering issues in TUI apps because it interp - **TERM mismatch**: If the inner `TERM` doesn't match tmux's advertised capabilities (e.g., app sees `xterm-256color` but tmux only passes `screen-256color`), color/capability negotiation fails silently. - **Clipboard**: OSC 52 clipboard support works but must be explicitly enabled (`set -g set-clipboard on`). +### Adaptive terminal theme + +With `display.theme = "auto"` (the default), jcode queries the terminal's +background using OSC 11 at startup, every five seconds during the session, and +when focus returns (rate limited). This also runs while waiting for a model, +streaming output, and executing tools, so day/night terminal changes do not +require restarting jcode or submitting another message. Background replies are +consumed before shortcuts, pickers, and the composer see them. Full-frame +repainting replaces cached animation frames when the theme changes. + +`JCODE_THEME=light|dark` or `display.theme = "light"|"dark"` disables live +detection. Terminals that do not answer retain the current theme. Background +queries need to be supported by the multiplexer as well as the outer terminal. + +Tmux normally answers OSC 11 from a cached background captured at attachment. +For live changes in terminals that do not notify tmux about palette updates, +set `set -g allow-passthrough on` in your tmux configuration before launching +jcode. When that option is enabled, jcode refreshes the outer terminal color +using a tmux DCS envelope, then reads tmux's updated cache on the next timer +tick. The option is checked once at TUI +startup with a bounded local tmux command. Without it, ordinary queries still +work, but theme changes depend on tmux refreshing its cache (for example when +reattaching, or receiving supported terminal theme notifications). Passthrough +permits applications to send control sequences to the outer terminal, so enable +it only for trusted applications. Clipboard forwarding remains plain OSC 52 and +only needs `set-clipboard on`, independent of this theme-query option. + +This implementation uses OSC 11 replies and periodic probing, not the newer +DEC mode 2031 color-scheme notification subscription, which the current +crossterm input parser does not expose. + --- ## Recommendations for TUI Developers From bd73df2c8f545e4d44d29f8d393f61c4c0c3feca Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:52:38 -0800 Subject: [PATCH 103/236] session picker search: narrow literal token extensions to prefix-matching segments Short literal tokens record which segments contain them, and so does any narrowed result. Typing alph -> alpha then scans only those segments, with origin ranking identical to a full scan. --- .../transcript_search/token_cache.rs | 167 +++++++++++++++++- 1 file changed, 162 insertions(+), 5 deletions(-) diff --git a/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs b/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs index 90b788fad9d..4712c928c17 100644 --- a/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs +++ b/crates/jcode-tui/src/tui/session_picker/transcript_search/token_cache.rs @@ -12,6 +12,9 @@ const MAX_ENTRIES: usize = 4096; const MAX_BYTES: usize = 1024 * 1024; const MAX_TOKEN_BYTES: usize = 4096; const MAX_PREFIX_PROBES: usize = 64; +/// Larger match lists are not worth their memory: such tokens are common, and +/// the full scan for them stops early on the first user-prompt hit anyway. +const MAX_NARROW_SEGMENTS: usize = 2048; /// A token found in a transcript: its first-match fuzzy score and the /// strongest origin it appears in. @@ -30,6 +33,10 @@ pub(crate) fn hit(score: i64) -> Hit { struct Evidence { outcome: Option, + /// Literal mode only: every segment containing the token, when bounded. + /// A longer token can only occur in these, so typing `alph` -> `alpha` + /// rescans a few segments instead of the whole transcript. + segments: Option>, used: u64, bytes: usize, } @@ -100,6 +107,32 @@ impl Cache { } None } + /// The segment list of the longest cached literal prefix of `token` that + /// matched. None means no narrowing is available. + fn narrowing(&mut self, text: &Arc, token: &str) -> Option> { + let generation = self.generations.get_mut(&(Arc::as_ptr(text) as usize))?; + if !generation.text.ptr_eq(&Arc::downgrade(text)) { + return None; + } + let mode = &mut generation.modes[0]; + let ends: Vec = token + .char_indices() + .skip(1) + .take(MAX_PREFIX_PROBES) + .map(|(end, _)| end) + .collect(); + for end in ends.into_iter().rev() { + if let Some(entry) = mode.get_mut(&token[..end]) + && entry.outcome.is_some() + && let Some(segments) = &entry.segments + { + self.clock = self.clock.wrapping_add(1); + entry.used = self.clock; + return Some(segments.clone()); + } + } + None + } fn evict(&mut self, incoming_bytes: usize) { // Batch eviction amortizes the global LRU pass over many insertions. // Hits only touch timestamps, without allocating or pruning anything. @@ -133,6 +166,16 @@ impl Cache { } } fn insert(&mut self, text: &Arc, token: &str, fuzzy: bool, outcome: Option) { + self.insert_narrowed(text, token, fuzzy, outcome, None); + } + fn insert_narrowed( + &mut self, + text: &Arc, + token: &str, + fuzzy: bool, + outcome: Option, + segments: Option>, + ) { if token.len() > MAX_TOKEN_BYTES { return; } @@ -142,7 +185,9 @@ impl Cache { } self.insertions += 1; let token = token.to_owned(); - let bytes = EVIDENCE_BYTES + token.capacity(); + let bytes = EVIDENCE_BYTES + + token.capacity() + + segments.as_ref().map_or(0, |list| list.len() * 4 + 16); // Reserve generation overhead even for an existing generation: eviction // could remove its final entry before this insertion recreates it. let incoming_bytes = bytes + GENERATION_BYTES; @@ -171,6 +216,7 @@ impl Cache { token, Evidence { outcome, + segments, used: self.clock, bytes, }, @@ -272,9 +318,30 @@ fn resolve( } let cached = super::super::preview_result(cache.lock(), "token cache lock")?.get(text, token, fuzzy); - let outcome = match cached { - Some(outcome) => outcome, - None => { + let narrowing = if cached.is_none() && !fuzzy { + super::super::preview_result(cache.lock(), "token cache lock")?.narrowing(text, token) + } else { + None + }; + let outcome = match (cached, narrowing) { + (Some(outcome), _) => outcome, + (None, Some(candidates)) => { + let (outcome, segments) = + literal_in_segments(text, token, candidates.iter().copied(), cancel)?; + super::super::preview_result(cache.lock(), "token cache lock")? + .insert_narrowed(text, token, false, outcome, segments); + outcome + } + (None, None) if !fuzzy && token.chars().count() <= 2 => { + // Short literal tokens are the prefixes of whatever is typed + // next. Record where they match so extensions scan only there. + let (outcome, segments) = + literal_in_segments(text, token, 0..text.text.len() as u32, cancel)?; + super::super::preview_result(cache.lock(), "token cache lock")? + .insert_narrowed(text, token, false, outcome, segments); + outcome + } + (None, None) => { let outcome = scan(text, token, cancel, fuzzy)?; // Publish each completed token independently, even if a later // token cancels. Incomplete negatives never reach this point. @@ -291,6 +358,45 @@ fn resolve( Some(outcomes) } } +/// Literal match over the given segments, with the same semantics as the +/// literal scanner (substring match, score 0, strongest origin), plus the +/// list of matching segments when it is small enough to keep. +fn literal_in_segments( + text: &Segments, + token: &str, + candidates: impl Iterator, + cancel: &AtomicBool, +) -> Option<(Option, Option>)> { + let mut best: Option = None; + let mut found = Vec::new(); + let mut bounded = true; + for (step, index) in candidates.enumerate() { + if step % 256 == 0 && cancel.load(Ordering::Relaxed) { + return None; + } + let (Some(segment), Some(source)) = ( + text.text.get(index as usize), + text.sources.get(index as usize), + ) else { + continue; + }; + if segment.contains(token) { + best = super::source::stronger(best, Some(*source)); + if bounded { + if found.len() < MAX_NARROW_SEGMENTS { + found.push(index); + } else { + bounded = false; + found = Vec::new(); + } + } + } + } + let outcome = best.map(|source| Hit { score: 0, source }); + let segments = (bounded && outcome.is_some()).then(|| Arc::from(found)); + Some((outcome, segments)) +} + #[cfg(test)] fn scan_token( text: &Segments, @@ -458,6 +564,55 @@ mod tests { assert!(result[0].unwrap().score > 0); } + #[test] + fn literal_extensions_scan_only_segments_matching_their_prefix() { + let cache = Mutex::new(Cache::default()); + let text = Arc::new(Segments { + text: tokens(&["alpine", "nothing", "alpha tool", "beta", "alpha prompt"]), + sources: vec![ + Source::Other, + Source::User, + Source::ToolOutput, + Source::User, + Source::User, + ], + }); + let cancel = AtomicBool::new(false); + let never = |_: &Segments, _: &str, _: &AtomicBool, _: bool| -> Option> { + panic!("extension must reuse its prefix's segments") + }; + let resolve_one = + |token: &str, + scan: &dyn Fn(&Segments, &str, &AtomicBool, bool) -> Option>| { + resolve(&cache, &text, &tokens(&[token]), &cancel, false, scan).unwrap()[0] + }; + let user = Some(Hit { + score: 0, + source: Source::User, + }); + assert_eq!(resolve_one("al", &never), user); + assert_eq!(resolve_one("alp", &never), user); + assert_eq!(resolve_one("alpha", &never), user); + assert_eq!( + resolve_one("alpine", &never), + Some(Hit { + score: 0, + source: Source::Other + }) + ); + assert_eq!(resolve_one("alphax", &never), None); + // Narrowed evidence agrees with a full scan. + for token in ["alp", "alpha", "alpine", "alphax", "alpha tool"] { + assert_eq!( + scan_token(&text, token, &cancel, false).unwrap(), + resolve_one(token, &never), + "{token}" + ); + } + let narrowed = cache.lock().unwrap().narrowing(&text, "alphaz").unwrap(); + assert_eq!(&*narrowed, &[2, 4]); + } + #[test] fn cancellation_keeps_completed_work_not_incomplete_negatives() { let cache = Mutex::new(Cache::default()); @@ -598,7 +753,9 @@ mod tests { Some(vec![None]) ); } - assert_eq!(calls, 2); + // Short literal tokens resolve through the segment-recording scan, so + // only the fuzzy lookup reaches the injected scanner. + assert_eq!(calls, 1); let mut guard = cache.lock().unwrap(); guard.insert(&text, "alpha", false, Some(hit(0))); assert_eq!(guard.get(&text, "alphabeta", false), None); From 5ca1e11904019426fb9b5193472d0b19dab87bf0 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:53:56 -0800 Subject: [PATCH 104/236] ACP: report max_tokens/max_turn_requests/refusal/cancelled stop reasons and scope cancellation to the active prompt (#2094) --- src/cli/acp.rs | 114 ++++++++++++++++++++++++++++++++++++++++--- src/cli/acp/tests.rs | 54 ++++++++++++++++++++ 2 files changed, 161 insertions(+), 7 deletions(-) diff --git a/src/cli/acp.rs b/src/cli/acp.rs index 7998c6a8334..fbfc28b0af1 100644 --- a/src/cli/acp.rs +++ b/src/cli/acp.rs @@ -90,6 +90,8 @@ struct DaemonSession { next_request_id: AtomicU64, active_prompt_id: Mutex>, prompt_running: AtomicBool, + /// Set by `session/cancel` while a prompt is running; scoped to that prompt. + cancel_requested: AtomicBool, ui_state: Mutex, /// Workspace root reported by the ACP client, used to compose the /// project-local skill overlay for slash-skill resolution. @@ -195,6 +197,86 @@ fn add_optional_tokens(total: &mut Option, tokens: Option) { } } +/// Tracks daemon events for one ACP prompt and derives its ACP stop reason. +/// +/// Only the terminal outcome of the whole turn counts: a limit or refusal +/// followed by another model request (a successful continuation) is reset. +#[derive(Debug, Default)] +struct TurnOutcome { + acked: bool, + interrupted: bool, + terminal: Option<&'static str>, +} + +impl TurnOutcome { + fn observe(&mut self, event: &ServerEvent, prompt_id: u64) { + match event { + ServerEvent::Ack { id } if *id == prompt_id => self.acked = true, + // Interrupts read before our prompt was acknowledged are stale + // leftovers from an earlier turn, not this prompt's outcome. + ServerEvent::Interrupted if self.acked => self.interrupted = true, + ServerEvent::KvCacheRequest { .. } | ServerEvent::RetryRollback { .. } => { + self.terminal = None; + } + ServerEvent::TurnStopped { + reason, + message, + provider_stop_reason, + } => match reason { + crate::protocol::TurnStopReason::LimitReached => { + let raw = provider_stop_reason.as_deref().unwrap_or_default(); + let lower = message.to_ascii_lowercase(); + self.terminal = Some( + if raw.contains("turn_requests") + || lower.contains("request limit") + || lower.contains("max turn") + { + "max_turn_requests" + } else { + "max_tokens" + }, + ); + } + crate::protocol::TurnStopReason::ProviderGuardrail => { + self.terminal = Some("refusal"); + } + crate::protocol::TurnStopReason::Interrupted if self.acked => { + self.interrupted = true; + } + _ => {} + }, + ServerEvent::ProviderGuardrail { stop_reason, .. } => { + if stop_reason.as_deref().is_some_and(is_refusal_stop_reason) { + self.terminal = Some("refusal"); + } + } + _ => {} + } + } + + fn stop_reason(&self, cancel_requested: bool) -> &'static str { + if cancel_requested || self.interrupted { + "cancelled" + } else { + self.terminal.unwrap_or("end_turn") + } + } +} + +fn is_refusal_stop_reason(reason: &str) -> bool { + let reason = reason.to_ascii_lowercase(); + [ + "refusal", + "content_filter", + "safety", + "blocked", + "prohibited", + "guardrail", + ] + .iter() + .any(|needle| reason.contains(needle)) +} + fn prompt_response(stop_reason: &str, usage: &TurnUsage) -> Value { let mut response = json!({ "stopReason": stop_reason }); if let Some(usage) = usage.to_acp() { @@ -243,6 +325,7 @@ impl DaemonSession { next_request_id: AtomicU64::new(next_request_id), active_prompt_id: Mutex::new(None), prompt_running: AtomicBool::new(false), + cancel_requested: AtomicBool::new(false), ui_state: Mutex::new(SessionUiState::default()), working_dir: None, active_skill: Mutex::new(None), @@ -608,8 +691,14 @@ impl AcpRuntime { sessions.get(&session_id).cloned() }; if let Some(session) = session { - let cancel_id = session.next_id(); - let _ = session.send(&Request::Cancel { id: cancel_id }).await; + // Only forward a cancel when an ACP prompt is in flight. An idle + // daemon cancel can emit a stray interrupt that the next prompt + // would otherwise misattribute to itself. + if session.prompt_running.load(Ordering::SeqCst) { + session.cancel_requested.store(true, Ordering::SeqCst); + let cancel_id = session.next_id(); + let _ = session.send(&Request::Cancel { id: cancel_id }).await; + } } if let Some(id) = message.id { self.write_result(id, json!({})).await?; @@ -1089,6 +1178,7 @@ impl AcpRuntime { let active_skill = session.active_skill.lock().await.clone(); let prompt_id = session.next_id(); + session.cancel_requested.store(false, Ordering::SeqCst); { let mut active = session.active_prompt_id.lock().await; *active = Some(prompt_id); @@ -1110,7 +1200,7 @@ impl AcpRuntime { } let mut mapper = EventMapper::new(session.session_id.clone(), self.profile); - let mut stop_reason = "end_turn".to_string(); + let mut outcome = TurnOutcome::default(); let mut turn_usage = TurnUsage::default(); loop { let event = match session.read_event().await { @@ -1124,13 +1214,21 @@ impl AcpRuntime { self.write_jcode_extension_event(&session.session_id, &event) .await?; } + outcome.observe(&event, prompt_id); match event { ServerEvent::Ack { .. } => {} ServerEvent::Done { id } if id == prompt_id => break, - ServerEvent::Interrupted => { - stop_reason = "cancelled".to_string(); - } + ServerEvent::Interrupted + | ServerEvent::TurnStopped { .. } + | ServerEvent::ProviderGuardrail { .. } => {} ServerEvent::Error { id, message, .. } if id == prompt_id => { + if session.cancel_requested.load(Ordering::SeqCst) { + // Cancellation-induced failures still complete as cancelled. + cleanup_prompt_state(&session).await; + self.write_result(rpc_id, prompt_response("cancelled", &turn_usage)) + .await?; + return Ok(()); + } cleanup_prompt_state(&session).await; self.write_error_value(rpc_id, JSONRPC_SERVER_ERROR, message) .await?; @@ -1225,8 +1323,9 @@ impl AcpRuntime { } } + let stop_reason = outcome.stop_reason(session.cancel_requested.load(Ordering::SeqCst)); cleanup_prompt_state(&session).await; - self.write_result(rpc_id, prompt_response(&stop_reason, &turn_usage)) + self.write_result(rpc_id, prompt_response(stop_reason, &turn_usage)) .await?; Ok(()) } @@ -1400,6 +1499,7 @@ async fn cleanup_prompt_state(session: &DaemonSession) { let mut active = session.active_prompt_id.lock().await; *active = None; } + session.cancel_requested.store(false, Ordering::SeqCst); session.prompt_running.store(false, Ordering::SeqCst); } diff --git a/src/cli/acp/tests.rs b/src/cli/acp/tests.rs index bca5ad59f27..cb6bcdf00a6 100644 --- a/src/cli/acp/tests.rs +++ b/src/cli/acp/tests.rs @@ -556,3 +556,57 @@ fn context_limit_falls_back_to_default_for_unknown_models() { crate::provider::DEFAULT_CONTEXT_LIMIT as u64 ); } + +#[test] +fn acp_turn_outcome_maps_terminal_limits_and_refusals() { + use crate::protocol::TurnStopReason; + let limit = |raw: Option<&str>, msg: &str| ServerEvent::TurnStopped { + reason: TurnStopReason::LimitReached, + message: msg.to_string(), + provider_stop_reason: raw.map(str::to_string), + }; + let mut o = TurnOutcome::default(); + o.observe(&ServerEvent::Ack { id: 7 }, 7); + assert_eq!(o.stop_reason(false), "end_turn"); + o.observe(&limit(Some("max_tokens"), "truncated"), 7); + assert_eq!(o.stop_reason(false), "max_tokens"); + // A continued model request clears an intermediate limit. + o.observe(&ServerEvent::RetryRollback { attempt: 1, max: 3 }, 7); + assert_eq!(o.stop_reason(false), "end_turn"); + o.observe(&limit(Some("max_turn_requests"), "limit"), 7); + assert_eq!(o.stop_reason(false), "max_turn_requests"); + + let mut o = TurnOutcome::default(); + o.observe(&ServerEvent::Ack { id: 7 }, 7); + o.observe( + &ServerEvent::ProviderGuardrail { + stop_reason: Some("refusal".into()), + message: "blocked".into(), + }, + 7, + ); + assert_eq!(o.stop_reason(false), "refusal"); + // Cancellation wins over everything for the cancelled prompt. + assert_eq!(o.stop_reason(true), "cancelled"); + + // Non-refusal provider notices are not refusals. + let mut o = TurnOutcome::default(); + o.observe( + &ServerEvent::ProviderGuardrail { + stop_reason: Some("end_turn".into()), + message: "empty".into(), + }, + 7, + ); + assert_eq!(o.stop_reason(false), "end_turn"); +} + +#[test] +fn acp_turn_outcome_ignores_stale_interrupt_before_ack() { + let mut o = TurnOutcome::default(); + o.observe(&ServerEvent::Interrupted, 9); + o.observe(&ServerEvent::Ack { id: 9 }, 9); + assert_eq!(o.stop_reason(false), "end_turn"); + o.observe(&ServerEvent::Interrupted, 9); + assert_eq!(o.stop_reason(false), "cancelled"); +} From 997c6262d0c925841887e54e27cb03efb31c1012 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:54:38 -0800 Subject: [PATCH 105/236] test: drop IPv6 zone-id base URL assertion the url parser rejects (#2018 follow-up) --- crates/jcode-provider-metadata/src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/jcode-provider-metadata/src/lib.rs b/crates/jcode-provider-metadata/src/lib.rs index ff6992045c2..098261ccffb 100644 --- a/crates/jcode-provider-metadata/src/lib.rs +++ b/crates/jcode-provider-metadata/src/lib.rs @@ -401,10 +401,6 @@ mod tests { normalize_api_base("http://[2001:db8::1]:8080/v1").as_deref(), Some("http://[2001:db8::1]:8080/v1") ); - assert_eq!( - normalize_api_base("http://[fe80::1%25eth0]:8080/v1").as_deref(), - Some("http://[fe80::1%25eth0]:8080/v1") - ); // Named hosts still need a local name or https. assert_eq!(normalize_api_base("http://example.com/v1"), None); assert_eq!( From 858e7657ce9ed72833d2f2775a907f3a21a5dd5f Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:55:57 -0800 Subject: [PATCH 106/236] tui: coalesce event-driven full redraws to 60fps in the remote loop --- .../src/translate_tests.rs | 5 ++- .../src/lib.rs | 6 +++- crates/jcode-sdk/src/lib.rs | 8 ++--- crates/jcode-tui/src/tui/app/helpers.rs | 13 ++------ crates/jcode-tui/src/tui/app/run_shell.rs | 31 +++++++++++++++++-- crates/jcode-tui/src/tui/backend_tests.rs | 5 ++- src/cli/args/cloud_runtime.rs | 30 ++++++++++++++---- src/cli/cloud_runtime.rs | 15 +++++++-- 8 files changed, 85 insertions(+), 28 deletions(-) diff --git a/crates/jcode-harness-api-server/src/translate_tests.rs b/crates/jcode-harness-api-server/src/translate_tests.rs index d1b97d40414..a857ea54445 100644 --- a/crates/jcode-harness-api-server/src/translate_tests.rs +++ b/crates/jcode-harness-api-server/src/translate_tests.rs @@ -3250,7 +3250,10 @@ fn history_image_boundaries_survive_loss_of_tool_data() { }; assert_eq!(messages.len(), 3); assert_eq!(messages[1].role, "tool"); - let tool = messages[1].tool.as_ref().expect("tool rows carry their call"); + let tool = messages[1] + .tool + .as_ref() + .expect("tool rows carry their call"); assert_eq!((tool.id.as_str(), tool.name.as_str()), ("read-1", "read")); assert!(messages[0].tool.is_none()); assert_eq!(images.len(), 1); diff --git a/crates/jcode-provider-anthropic-runtime/src/lib.rs b/crates/jcode-provider-anthropic-runtime/src/lib.rs index 98853953d76..3aec409efc1 100644 --- a/crates/jcode-provider-anthropic-runtime/src/lib.rs +++ b/crates/jcode-provider-anthropic-runtime/src/lib.rs @@ -2704,7 +2704,11 @@ mod sse_utf8_chunk_tests { let split = payload.find('你').unwrap() + 1; // inside the 3-byte char let mut utf8 = jcode_base::util::Utf8StreamDecoder::new(); let mut buffer = String::new(); - for chunk in [&bytes[..split], &bytes[split..split + 3], &bytes[split + 3..]] { + for chunk in [ + &bytes[..split], + &bytes[split..split + 3], + &bytes[split + 3..], + ] { buffer.push_str(&utf8.decode(chunk)); } let event = parse_sse_event(&mut buffer).expect("complete event"); diff --git a/crates/jcode-sdk/src/lib.rs b/crates/jcode-sdk/src/lib.rs index b92978c1395..00f6641b894 100644 --- a/crates/jcode-sdk/src/lib.rs +++ b/crates/jcode-sdk/src/lib.rs @@ -198,10 +198,10 @@ pub use turn_retry::{FailureKind, RetryDecision, RetryPolicy, TurnRetry, classif /// The protocol types, re-exported so a client needs one dependency, not two. pub use jcode_harness_api as api; pub use jcode_harness_api::{ - ApiEvent, ApiRequest, HistoryMessage, HistoryTool, ModelRouteInfo, PermissionDecision, RenderedImage, - RenderedImageAnchor, RenderedImageSource, ResponseStats, SessionInfo, SessionToolDefinition, - SidePanelPage, SidePanelPageFormat, SidePanelPageSource, SidePanelSnapshot, TextMatch, - ToolConfiguration, TurnStopReason, api_socket_path, + ApiEvent, ApiRequest, HistoryMessage, HistoryTool, ModelRouteInfo, PermissionDecision, + RenderedImage, RenderedImageAnchor, RenderedImageSource, ResponseStats, SessionInfo, + SessionToolDefinition, SidePanelPage, SidePanelPageFormat, SidePanelPageSource, + SidePanelSnapshot, TextMatch, ToolConfiguration, TurnStopReason, api_socket_path, }; pub use jcode_harness_api::{ ModelUsage, RuntimeCommand, RuntimeCommandOutput, compare_model_usage, diff --git a/crates/jcode-tui/src/tui/app/helpers.rs b/crates/jcode-tui/src/tui/app/helpers.rs index 6375e69b04c..343886f5022 100644 --- a/crates/jcode-tui/src/tui/app/helpers.rs +++ b/crates/jcode-tui/src/tui/app/helpers.rs @@ -600,18 +600,11 @@ fn copy_to_local_clipboard(text: &str) -> bool { if clipboard_helper::copy_via_clipboard_helper("wl-copy", &[], text) { return true; } - if clipboard_helper::copy_via_clipboard_helper( - "xclip", - &["-selection", "clipboard"], - text, - ) { + if clipboard_helper::copy_via_clipboard_helper("xclip", &["-selection", "clipboard"], text) + { return true; } - if clipboard_helper::copy_via_clipboard_helper( - "xsel", - &["--clipboard", "--input"], - text, - ) { + if clipboard_helper::copy_via_clipboard_helper("xsel", &["--clipboard", "--input"], text) { return true; } arboard::Clipboard::new() diff --git a/crates/jcode-tui/src/tui/app/run_shell.rs b/crates/jcode-tui/src/tui/app/run_shell.rs index 21c41ba12ac..5f1dab0a71d 100644 --- a/crates/jcode-tui/src/tui/app/run_shell.rs +++ b/crates/jcode-tui/src/tui/app/run_shell.rs @@ -36,6 +36,8 @@ const STATUS_SPINNER_FPS: f32 = 12.5; const FIRST_FRAME_GIT_BUDGET: std::time::Duration = std::time::Duration::from_millis(40); /// Tick cadence while the reload fast-start transcript is still rendering. const STARTUP_TRANSCRIPT_POLL: std::time::Duration = std::time::Duration::from_millis(10); +/// Minimum spacing between non-input full frames in the remote loop (~60fps). +const EVENT_REDRAW_MIN_INTERVAL: Duration = Duration::from_millis(16); pub(super) const STATUS_SPINNER_ONLY_INTERVAL: Duration = Duration::from_millis(80); pub(super) fn redraw_timer(period: Duration) -> tokio::time::Interval { @@ -943,6 +945,8 @@ impl App { needs_redraw = true; } + let mut input_redraw = true; + let mut deferred_redraw_at: Option = None; // Main event loop loop { self.sync_sleep_guard(); @@ -980,7 +984,20 @@ impl App { || last_unfocused_draw .map(|t| t.elapsed() >= UNFOCUSED_IDLE_REDRAW_MIN_INTERVAL) .unwrap_or(true); - if allow_redraw { + // Coalesce event-driven bursts: server events can land a + // few ms apart and each used to trigger a full draw that + // changed nothing. Input-driven and forced redraws still + // paint immediately so typing latency is unaffected. + let coalesce_until = (!input_redraw + && !self.force_full_redraw + && !self.force_full_repaint) + .then_some(status_spinner_renderer.last_full_frame_at) + .flatten() + .map(|at| at + EVENT_REDRAW_MIN_INTERVAL) + .filter(|deadline| *deadline > Instant::now()); + deferred_redraw_at = coalesce_until.map(tokio::time::Instant::from_std); + if allow_redraw && deferred_redraw_at.is_none() { + input_redraw = false; // Idle animated screen: repaint only the animation rows // when nothing else is due (see the local loop). if status_spinner_renderer.idle_animation_only_available(&self) @@ -1026,7 +1043,9 @@ impl App { biased; event = event_stream.next() => { if event.is_some() { - needs_redraw |= remote::handle_terminal_event(&mut self, &mut terminal, &mut remote_conn, event).await?; + let event_redraw = remote::handle_terminal_event(&mut self, &mut terminal, &mut remote_conn, event).await?; + needs_redraw |= event_redraw; + input_redraw |= event_redraw; } else if super::terminal_liveness::terminal_abandoned() { // Input EOF with the controlling terminal gone: // orphaned client (see local loop). Exit; the @@ -1048,6 +1067,14 @@ impl App { _ = redraw_interval.tick() => { needs_redraw |= remote::handle_tick(&mut self, &mut remote_conn).await; } + _ = async { + match deferred_redraw_at { + Some(at) => tokio::time::sleep_until(at).await, + None => futures::future::pending::<()>().await, + } + } => { + deferred_redraw_at = None; + } event = remote_conn.next_event() => { let (outcome, event_redraw) = remote::handle_remote_event( &mut self, diff --git a/crates/jcode-tui/src/tui/backend_tests.rs b/crates/jcode-tui/src/tui/backend_tests.rs index e8f3517ebae..7e60ccdb484 100644 --- a/crates/jcode-tui/src/tui/backend_tests.rs +++ b/crates/jcode-tui/src/tui/backend_tests.rs @@ -822,5 +822,8 @@ async fn request_send_does_not_block_ui_when_server_stops_reading() { } }) .await; - assert!(result.is_ok(), "request sends must not wait on the socket writer"); + assert!( + result.is_ok(), + "request sends must not wait on the socket writer" + ); } diff --git a/src/cli/args/cloud_runtime.rs b/src/cli/args/cloud_runtime.rs index fe6328d0d04..7805222429b 100644 --- a/src/cli/args/cloud_runtime.rs +++ b/src/cli/args/cloud_runtime.rs @@ -76,18 +76,36 @@ mod tests { action: CloudRuntimeCommand::Push { artifact: Some(artifact), from_source: false, reload: false, .. } })) if artifact == std::path::Path::new("portable.tar.gz") )); - assert!(Args::try_parse_from([ - "jcode", "cloud", "runtime", "push", "--artifact", "a.tar.gz", "--from-source" - ]) - .is_err()); + assert!( + Args::try_parse_from([ + "jcode", + "cloud", + "runtime", + "push", + "--artifact", + "a.tar.gz", + "--from-source" + ]) + .is_err() + ); let parsed = Args::try_parse_from([ - "jcode", "cloud", "runtime", "push", "--from-source", "--reload" + "jcode", + "cloud", + "runtime", + "push", + "--from-source", + "--reload", ]) .unwrap(); assert!(matches!( parsed.command, Some(Command::Cloud(CloudCommand::Runtime { - action: CloudRuntimeCommand::Push { artifact: None, from_source: true, reload: true, wait_secs: 1800 } + action: CloudRuntimeCommand::Push { + artifact: None, + from_source: true, + reload: true, + wait_secs: 1800 + } })) )); assert!(Args::try_parse_from(["jcode", "cloud", "runtime", "reload", "--force"]).is_ok()); diff --git a/src/cli/cloud_runtime.rs b/src/cli/cloud_runtime.rs index eba2e35a077..90ae72880c3 100644 --- a/src/cli/cloud_runtime.rs +++ b/src/cli/cloud_runtime.rs @@ -138,7 +138,10 @@ fn build_from_source() -> Result { bail!("{} is not a jcode checkout", root.display()); } let target = "x86_64-unknown-linux-gnu"; - eprintln!("[cloud build] building portable Linux runtime in {}", root.display()); + eprintln!( + "[cloud build] building portable Linux runtime in {}", + root.display() + ); let builder = if Command::new("justrust").arg("--version").output().is_ok() { "justrust" } else { @@ -164,7 +167,11 @@ fn build_from_source() -> Result { if !status.success() { bail!("portable Linux build failed ({status})"); } - let binary = root.join("target").join(target).join("selfdev").join("jcode"); + let binary = root + .join("target") + .join(target) + .join("selfdev") + .join("jcode"); let output = root.join("target/cloud-runtime/jcode-linux-x86_64.tar.gz"); eprintln!("[cloud build] packaging"); let status = Command::new(&script) @@ -275,7 +282,9 @@ pub(crate) fn run_cli(action: CloudRuntimeCommand) -> Result<()> { if then_reload { reload(wait_secs, false) } else { - eprintln!("Active daemons are not restarted. Run `jcode cloud runtime reload` to switch."); + eprintln!( + "Active daemons are not restarted. Run `jcode cloud runtime reload` to switch." + ); Ok(()) } } From cb0582a8ab26c7f96e5b750aae654093b741ba46 Mon Sep 17 00:00:00 2001 From: Jeremy Huang <94247773+1jehuang@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:58:28 -0800 Subject: [PATCH 107/236] vendor ratatui-jcode: in-tree fork of ratatui-core 0.1.0 and ratatui-widgets 0.3.0 (unmodified baseline) --- Cargo.lock | 4 - Cargo.toml | 6 + vendor/ratatui-jcode/LICENSE | 22 + vendor/ratatui-jcode/README.md | 16 + vendor/ratatui-jcode/ratatui-core/Cargo.toml | 191 + vendor/ratatui-jcode/ratatui-core/README.md | 67 + .../ratatui-jcode/ratatui-core/src/backend.rs | 405 ++ .../ratatui-core/src/backend/test.rs | 1077 +++ .../ratatui-jcode/ratatui-core/src/buffer.rs | 9 + .../ratatui-core/src/buffer/assert.rs | 71 + .../ratatui-core/src/buffer/buffer.rs | 1371 ++++ .../ratatui-core/src/buffer/cell.rs | 377 + .../ratatui-jcode/ratatui-core/src/layout.rs | 333 + .../ratatui-core/src/layout/alignment.rs | 89 + .../ratatui-core/src/layout/constraint.rs | 526 ++ .../ratatui-core/src/layout/direction.rs | 63 + .../ratatui-core/src/layout/flex.rs | 212 + .../ratatui-core/src/layout/layout.rs | 2838 ++++++++ .../ratatui-core/src/layout/margin.rs | 79 + .../ratatui-core/src/layout/offset.rs | 66 + .../ratatui-core/src/layout/position.rs | 253 + .../ratatui-core/src/layout/rect.rs | 1114 +++ .../ratatui-core/src/layout/rect/iter.rs | 356 + .../ratatui-core/src/layout/rect/ops.rs | 136 + .../ratatui-core/src/layout/size.rs | 147 + vendor/ratatui-jcode/ratatui-core/src/lib.rs | 82 + .../ratatui-jcode/ratatui-core/src/style.rs | 1069 +++ .../ratatui-core/src/style/anstyle.rs | 348 + .../ratatui-core/src/style/color.rs | 788 +++ .../ratatui-core/src/style/palette.rs | 6 + .../src/style/palette/material.rs | 608 ++ .../src/style/palette/tailwind.rs | 653 ++ .../src/style/palette_conversion.rs | 82 + .../ratatui-core/src/style/stylize.rs | 668 ++ .../ratatui-jcode/ratatui-core/src/symbols.rs | 15 + .../ratatui-core/src/symbols/bar.rs | 51 + .../ratatui-core/src/symbols/block.rs | 51 + .../ratatui-core/src/symbols/border.rs | 709 ++ .../ratatui-core/src/symbols/braille.rs | 21 + .../ratatui-core/src/symbols/half_block.rs | 3 + .../ratatui-core/src/symbols/line.rs | 259 + .../ratatui-core/src/symbols/marker.rs | 82 + .../ratatui-core/src/symbols/merge.rs | 748 ++ .../ratatui-core/src/symbols/pixel.rs | 30 + .../ratatui-core/src/symbols/scrollbar.rs | 46 + .../ratatui-core/src/symbols/shade.rs | 5 + .../ratatui-core/src/terminal.rs | 40 + .../ratatui-core/src/terminal/frame.rs | 192 + .../ratatui-core/src/terminal/terminal.rs | 926 +++ .../ratatui-core/src/terminal/viewport.rs | 58 + vendor/ratatui-jcode/ratatui-core/src/text.rs | 64 + .../ratatui-core/src/text/grapheme.rs | 84 + .../ratatui-core/src/text/line.rs | 1678 +++++ .../ratatui-core/src/text/masked.rs | 149 + .../ratatui-core/src/text/span.rs | 904 +++ .../ratatui-core/src/text/text.rs | 1434 ++++ .../ratatui-jcode/ratatui-core/src/widgets.rs | 9 + .../src/widgets/stateful_widget.rs | 193 + .../ratatui-core/src/widgets/widget.rs | 174 + .../ratatui-jcode/ratatui-widgets/Cargo.toml | 177 + .../ratatui-jcode/ratatui-widgets/README.md | 104 + .../ratatui-widgets/src/barchart.rs | 1507 ++++ .../ratatui-widgets/src/barchart/bar.rs | 336 + .../ratatui-widgets/src/barchart/bar_group.rs | 170 + .../ratatui-widgets/src/block.rs | 2158 ++++++ .../ratatui-widgets/src/block/padding.rs | 205 + .../ratatui-widgets/src/borders.rs | 307 + .../ratatui-widgets/src/calendar.rs | 458 ++ .../ratatui-widgets/src/canvas.rs | 1188 ++++ .../ratatui-widgets/src/canvas/circle.rs | 80 + .../ratatui-widgets/src/canvas/line.rs | 306 + .../ratatui-widgets/src/canvas/map.rs | 209 + .../ratatui-widgets/src/canvas/points.rs | 29 + .../ratatui-widgets/src/canvas/rectangle.rs | 202 + .../ratatui-widgets/src/canvas/world.rs | 6298 +++++++++++++++++ .../ratatui-widgets/src/chart.rs | 1629 +++++ .../ratatui-widgets/src/clear.rs | 72 + .../ratatui-widgets/src/gauge.rs | 623 ++ .../ratatui-jcode/ratatui-widgets/src/lib.rs | 134 + .../ratatui-jcode/ratatui-widgets/src/list.rs | 657 ++ .../ratatui-widgets/src/list/item.rs | 353 + .../ratatui-widgets/src/list/rendering.rs | 1272 ++++ .../ratatui-widgets/src/list/state.rs | 357 + .../ratatui-jcode/ratatui-widgets/src/logo.rs | 260 + .../ratatui-widgets/src/mascot.rs | 260 + .../ratatui-widgets/src/paragraph.rs | 1239 ++++ .../ratatui-widgets/src/polyfills.rs | 156 + .../ratatui-widgets/src/reflow.rs | 729 ++ .../ratatui-widgets/src/scrollbar.rs | 1123 +++ .../ratatui-widgets/src/sparkline.rs | 723 ++ .../ratatui-widgets/src/table.rs | 2305 ++++++ .../ratatui-widgets/src/table/cell.rs | 222 + .../src/table/highlight_spacing.rs | 77 + .../ratatui-widgets/src/table/row.rs | 345 + .../ratatui-widgets/src/table/state.rs | 745 ++ .../ratatui-jcode/ratatui-widgets/src/tabs.rs | 805 +++ 96 files changed, 49873 insertions(+), 4 deletions(-) create mode 100644 vendor/ratatui-jcode/LICENSE create mode 100644 vendor/ratatui-jcode/README.md create mode 100644 vendor/ratatui-jcode/ratatui-core/Cargo.toml create mode 100644 vendor/ratatui-jcode/ratatui-core/README.md create mode 100644 vendor/ratatui-jcode/ratatui-core/src/backend.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/backend/test.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/buffer.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/buffer/cell.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/alignment.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/constraint.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/direction.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/flex.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/layout.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/margin.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/offset.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/position.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/rect.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/rect/iter.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/rect/ops.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/layout/size.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/lib.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/anstyle.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/color.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/palette.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/palette/material.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/bar.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/block.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/border.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/braille.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/half_block.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/line.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/marker.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/merge.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/pixel.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/scrollbar.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/symbols/shade.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/terminal.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/terminal/frame.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/terminal/terminal.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/terminal/viewport.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/text.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/text/grapheme.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/text/line.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/text/masked.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/text/span.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/text/text.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/widgets.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/widgets/stateful_widget.rs create mode 100644 vendor/ratatui-jcode/ratatui-core/src/widgets/widget.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/Cargo.toml create mode 100644 vendor/ratatui-jcode/ratatui-widgets/README.md create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/barchart.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/barchart/bar.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/barchart/bar_group.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/block.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/block/padding.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/borders.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/calendar.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas/circle.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas/line.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas/map.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas/points.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas/rectangle.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/canvas/world.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/chart.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/clear.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/gauge.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/lib.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/list.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/list/item.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/list/rendering.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/list/state.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/logo.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/mascot.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/paragraph.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/polyfills.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/reflow.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/scrollbar.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/sparkline.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/table.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/table/cell.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/table/highlight_spacing.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/table/row.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/table/state.rs create mode 100644 vendor/ratatui-jcode/ratatui-widgets/src/tabs.rs diff --git a/Cargo.lock b/Cargo.lock index 4931f4acfa4..c55ff456ebd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7094,8 +7094,6 @@ dependencies = [ [[package]] name = "ratatui-core" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ef8dea09a92caaf73bff7adb70b76162e5937524058a7e5bff37869cbbec293" dependencies = [ "bitflags 2.10.0", "compact_str", @@ -7162,8 +7160,6 @@ dependencies = [ [[package]] name = "ratatui-widgets" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7dbfa023cd4e604c2553483820c5fe8aa9d71a42eea5aa77c6e7f35756612db" dependencies = [ "bitflags 2.10.0", "hashbrown 0.16.1", diff --git a/Cargo.toml b/Cargo.toml index 69b3e282fb8..ae21d4658fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -637,3 +637,9 @@ jcode-provider-core = { path = "crates/jcode-provider-core" } jcode-tui = { path = "crates/jcode-tui", default-features = false, features = ["test-support"] } [build-dependencies] + +# ratatui-jcode: in-tree fork of ratatui-core / ratatui-widgets tuned for +# jcode's per-frame hot paths. See vendor/ratatui-jcode/README.md. +[patch.crates-io] +ratatui-core = { path = "vendor/ratatui-jcode/ratatui-core" } +ratatui-widgets = { path = "vendor/ratatui-jcode/ratatui-widgets" } diff --git a/vendor/ratatui-jcode/LICENSE b/vendor/ratatui-jcode/LICENSE new file mode 100644 index 00000000000..776bf200770 --- /dev/null +++ b/vendor/ratatui-jcode/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2016-2022 Florian Dehau +Copyright (c) 2023-2025 The Ratatui Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/ratatui-jcode/README.md b/vendor/ratatui-jcode/README.md new file mode 100644 index 00000000000..75fa3eb9dae --- /dev/null +++ b/vendor/ratatui-jcode/README.md @@ -0,0 +1,16 @@ +# ratatui-jcode + +In-tree fork of [ratatui](https://github.com/ratatui/ratatui) `ratatui-core` +0.1.0 and `ratatui-widgets` 0.3.0 (MIT, see `LICENSE`), wired in through +`[patch.crates-io]` in the workspace `Cargo.toml`. + +Purpose: per-frame CPU work in release builds. Profiling the jcode TUI client +showed ~25-35% of busy CPU inside ratatui (`Buffer::diff`, `Cell` clone/eq/reset, +`Paragraph` grapheme splitting and width measurement). + +Rules: +- Keep the public API source-compatible with upstream so other crates in the + graph (ratatui-image, ratatui-crossterm) keep building unmodified. +- Mark every divergence from upstream with a `// ratatui-jcode:` comment so it + can be rebased or upstreamed. +- Prefer upstreaming generally useful wins. diff --git a/vendor/ratatui-jcode/ratatui-core/Cargo.toml b/vendor/ratatui-jcode/ratatui-core/Cargo.toml new file mode 100644 index 00000000000..bce9f065ee9 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/Cargo.toml @@ -0,0 +1,191 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +autotests = false +autoexamples = false +autobenches = false +edition = "2024" +rust-version = "1.86.0" +name = "ratatui-core" +version = "0.1.0" +authors = [ + "Florian Dehau ", + "The Ratatui Developers", +] +build = false +exclude = [ + "assets/*", + ".github", + "Makefile.toml", + "CONTRIBUTING.md", + "*.log", + "tags", +] +autolib = false +autobins = false +description = """ + Core types and traits for the Ratatui Terminal UI library. + Widget libraries should use this crate. Applications should use the main Ratatui crate. +""" +homepage = "https://ratatui.rs" +documentation = "https://docs.rs/ratatui-core" +readme = "README.md" +keywords = [ + "tui", + "terminal", + "dashboard", +] +categories = ["command-line-interface"] +license = "MIT" +repository = "https://github.com/ratatui/ratatui" +resolver = "2" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = [ + "--cfg", + "docsrs", +] + +[features] +anstyle = ["dep:anstyle"] +default = [] +layout-cache = ["std"] +palette = [ + "std", + "dep:palette", +] +portable-atomic = ["kasuari/portable-atomic"] +scrolling-regions = [] +serde = [ + "std", + "dep:serde", + "bitflags/serde", + "compact_str/serde", +] +std = [ + "itertools/use_std", + "thiserror/std", + "kasuari/std", + "compact_str/std", + "unicode-truncate/std", + "strum/std", +] +underline-color = [] + +[lib] +name = "ratatui_core" +path = "src/lib.rs" + + +[dependencies.anstyle] +version = "1" +optional = true + +[dependencies.bitflags] +version = "2.10" + +[dependencies.compact_str] +version = "0.9" +default-features = false + +[dependencies.document-features] +version = "0.2" +optional = true + +[dependencies.hashbrown] +version = "0.16" + +[dependencies.indoc] +version = "2" + +[dependencies.itertools] +version = "0.14" +features = ["use_alloc"] +default-features = false + +[dependencies.kasuari] +version = "0.4" +default-features = false + +[dependencies.lru] +version = "0.16" + +[dependencies.palette] +version = "0.7" +optional = true + +[dependencies.serde] +version = "1" +features = ["derive"] +optional = true + +[dependencies.strum] +version = "0.27" +features = ["derive"] +default-features = false + +[dependencies.thiserror] +version = "2" +default-features = false + +[dependencies.unicode-segmentation] +version = "1" + +[dependencies.unicode-truncate] +version = "2" +default-features = false + +[dependencies.unicode-width] +version = ">=0.2.0, <=0.2.2" + + + + +[lints.clippy] +as_underscore = "warn" +cast_possible_truncation = "allow" +cast_possible_wrap = "allow" +cast_precision_loss = "allow" +cast_sign_loss = "allow" +deref_by_slicing = "warn" +else_if_without_else = "warn" +empty_line_after_doc_comments = "warn" +equatable_if_let = "warn" +fn_to_numeric_cast_any = "warn" +format_push_string = "warn" +implicit_clone = "warn" +map_err_ignore = "warn" +missing_const_for_fn = "warn" +missing_errors_doc = "allow" +missing_panics_doc = "allow" +mixed_read_write_in_expression = "warn" +mod_module_files = "warn" +module_inception = "allow" +module_name_repetitions = "allow" +must_use_candidate = "allow" +needless_pass_by_ref_mut = "warn" +needless_raw_strings = "warn" +or_fun_call = "warn" +redundant_type_annotations = "warn" +rest_pat_in_fully_bound_structs = "warn" +string_lit_chars_any = "warn" +string_slice = "warn" +unnecessary_self_imports = "warn" +use_self = "warn" + +[lints.clippy.pedantic] +level = "warn" +priority = -1 + +[lints.rust] +unsafe_code = "forbid" diff --git a/vendor/ratatui-jcode/ratatui-core/README.md b/vendor/ratatui-jcode/ratatui-core/README.md new file mode 100644 index 00000000000..65eb36637bb --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/README.md @@ -0,0 +1,67 @@ +# Ratatui Core + +[![Crates.io](https://img.shields.io/crates/v/ratatui-core)](https://crates.io/crates/ratatui-core) +[![Documentation](https://docs.rs/ratatui-core/badge.svg)](https://docs.rs/ratatui-core) +[![License](https://img.shields.io/crates/l/ratatui-core)](../LICENSE) + + + + +**ratatui-core** is the core library of the [ratatui] project, +providing the essential building blocks for creating rich terminal user interfaces in Rust. + +[ratatui]: https://github.com/ratatui/ratatui + +### Why `ratatui-core`? + +The `ratatui-core` crate is split from the main [`ratatui`](https://crates.io/crates/ratatui) crate +to offer better stability for widget library authors. Widget libraries should generally depend +on `ratatui-core`, benefiting from a stable API and reducing the need for frequent updates. + +Applications, on the other hand, should depend on the main `ratatui` crate, which includes +built-in widgets and additional features. + +## Installation + +Add `ratatui-core` to your `Cargo.toml`: + +```shell +cargo add ratatui-core +``` + +## Crate Organization + +`ratatui-core` is part of the Ratatui workspace that was modularized in version 0.30.0 to +improve compilation times, API stability, and dependency management. This crate provides the +foundational types and traits that other crates in the workspace depend on. + +**When to use `ratatui-core`:** + +- Building widget libraries that implement [`Widget`] or [`StatefulWidget`] +- Creating lightweight applications that don't need built-in widgets +- You want minimal dependencies and faster compilation times +- You need maximum API stability (core types change less frequently) + +**When to use the main [`ratatui`] crate:** + +- Building applications that use built-in widgets +- You want convenience and don't mind slightly longer compilation times +- You need backend implementations and terminal management utilities + +For detailed information about the workspace organization, see [ARCHITECTURE.md]. + +[`ratatui`]: https://crates.io/crates/ratatui +[`Widget`]: widgets::Widget +[`StatefulWidget`]: widgets::StatefulWidget +[ARCHITECTURE.md]: https://github.com/ratatui/ratatui/blob/main/ARCHITECTURE.md + +## Contributing + +We welcome contributions from the community! Please see our [CONTRIBUTING](../CONTRIBUTING.md) +guide for more details on how to get involved. + +### License + +This project is licensed under the MIT License. See the [LICENSE](../LICENSE) file for details. + + diff --git a/vendor/ratatui-jcode/ratatui-core/src/backend.rs b/vendor/ratatui-jcode/ratatui-core/src/backend.rs new file mode 100644 index 00000000000..9eb29c283ad --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/backend.rs @@ -0,0 +1,405 @@ +#![warn(missing_docs)] +//! This module provides the backend implementations for different terminal libraries. +//! +//! It defines the [`Backend`] trait which is used to abstract over the specific terminal library +//! being used. +//! +//! Supported terminal backends: +//! - [Crossterm]: enable the `crossterm` feature (enabled by default) and use [`CrosstermBackend`] +//! - [Termion]: enable the `termion` feature and use [`TermionBackend`] +//! - [Termwiz]: enable the `termwiz` feature and use [`TermwizBackend`] +//! +//! Additionally, a [`TestBackend`] is provided for testing purposes. +//! +//! See the [Backend Comparison] section of the [Ratatui Website] for more details on the different +//! backends. +//! +//! Each backend supports a number of features, such as [raw mode](#raw-mode), [alternate +//! screen](#alternate-screen), and [mouse capture](#mouse-capture). These features are generally +//! not enabled by default, and must be enabled by the application before they can be used. See the +//! documentation for each backend for more details. +//! +//! Note: most applications should use the [`Terminal`] struct instead of directly calling methods +//! on the backend. +//! +//! # Example +//! +//! ```rust,ignore +//! use std::io::stdout; +//! +//! use ratatui::{backend::CrosstermBackend, Terminal}; +//! +//! let backend = CrosstermBackend::new(stdout()); +//! let mut terminal = Terminal::new(backend)?; +//! terminal.clear()?; +//! terminal.draw(|frame| { +//! // -- snip -- +//! })?; +//! # std::io::Result::Ok(()) +//! ``` +//! +//! See the the [Examples] directory for more examples. +//! +//! # Raw Mode +//! +//! Raw mode is a mode where the terminal does not perform any processing or handling of the input +//! and output. This means that features such as echoing input characters, line buffering, and +//! special character processing (e.g., CTRL-C for SIGINT) are disabled. This is useful for +//! applications that want to have complete control over the terminal input and output, processing +//! each keystroke themselves. +//! +//! For example, in raw mode, the terminal will not perform line buffering on the input, so the +//! application will receive each key press as it is typed, instead of waiting for the user to +//! press enter. This makes it suitable for real-time applications like text editors, +//! terminal-based games, and more. +//! +//! Each backend handles raw mode differently, so the behavior may vary depending on the backend +//! being used. Be sure to consult the backend's specific documentation for exact details on how it +//! implements raw mode. +//! +//! # Alternate Screen +//! +//! The alternate screen is a separate buffer that some terminals provide, distinct from the main +//! screen. When activated, the terminal will display the alternate screen, hiding the current +//! content of the main screen. Applications can write to this screen as if it were the regular +//! terminal display, but when the application exits, the terminal will switch back to the main +//! screen, and the contents of the alternate screen will be cleared. This is useful for +//! applications like text editors or terminal games that want to use the full terminal window +//! without disrupting the command line or other terminal content. +//! +//! This creates a seamless transition between the application and the regular terminal session, as +//! the content displayed before launching the application will reappear after the application +//! exits. +//! +//! Note that not all terminal emulators support the alternate screen, and even those that do may +//! handle it differently. As a result, the behavior may vary depending on the backend being used. +//! Always consult the specific backend's documentation to understand how it implements the +//! alternate screen. +//! +//! # Mouse Capture +//! +//! Mouse capture is a mode where the terminal captures mouse events such as clicks, scrolls, and +//! movement, and sends them to the application as special sequences or events. This enables the +//! application to handle and respond to mouse actions, providing a more interactive and graphical +//! user experience within the terminal. It's particularly useful for applications like +//! terminal-based games, text editors, or other programs that require more direct interaction from +//! the user. +//! +//! Each backend handles mouse capture differently, with variations in the types of events that can +//! be captured and how they are represented. As such, the behavior may vary depending on the +//! backend being used, and developers should consult the specific backend's documentation to +//! understand how it implements mouse capture. +//! +//! [`CrosstermBackend`]: https://docs.rs/ratatui/latest/ratatui/backend/struct.CrosstermBackend.html +//! [`TermionBackend`]: https://docs.rs/ratatui/latest/ratatui/backend/struct.TermionBackend.html +//! [`TermwizBackend`]: https://docs.rs/ratatui/latest/ratatui/backend/struct.TermwizBackend.html +//! [`Terminal`]: https://docs.rs/ratatui/latest/ratatui/struct.Terminal.html +//! [Crossterm]: https://crates.io/crates/crossterm +//! [Termion]: https://crates.io/crates/termion +//! [Termwiz]: https://crates.io/crates/termwiz +//! [Examples]: https://github.com/ratatui/ratatui/tree/main/ratatui/examples/README.md +//! [Backend Comparison]: https://ratatui.rs/concepts/backends/comparison/ +//! [Ratatui Website]: https://ratatui.rs + +use strum::{Display, EnumString}; + +use crate::buffer::Cell; +use crate::layout::{Position, Size}; + +mod test; +pub use self::test::TestBackend; + +/// Enum representing the different types of clearing operations that can be performed +/// on the terminal screen. +#[derive(Debug, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)] +pub enum ClearType { + /// Clear the entire screen. + All, + /// Clear everything after the cursor. + AfterCursor, + /// Clear everything before the cursor. + BeforeCursor, + /// Clear the current line. + CurrentLine, + /// Clear everything from the cursor until the next newline. + UntilNewLine, +} + +/// The window size in characters (columns / rows) as well as pixels. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub struct WindowSize { + /// Size of the window in characters (columns / rows). + pub columns_rows: Size, + /// Size of the window in pixels. + /// + /// The `pixels` fields may not be implemented by all terminals and return `0,0`. See + /// under section "Get and set window + /// size" / TIOCGWINSZ where the fields are commented as "unused". + pub pixels: Size, +} + +/// The `Backend` trait provides an abstraction over different terminal libraries. It defines the +/// methods required to draw content, manipulate the cursor, and clear the terminal screen. +/// +/// Most applications should not need to interact with the `Backend` trait directly as the +/// [`Terminal`] struct provides a higher level interface for interacting with the terminal. +/// +/// [`Terminal`]: https://docs.rs/ratatui/latest/ratatui/struct.Terminal.html +pub trait Backend { + /// Error type associated with this Backend. + type Error: core::error::Error; + + /// Draw the given content to the terminal screen. + /// + /// The content is provided as an iterator over `(u16, u16, &Cell)` tuples, where the first two + /// elements represent the x and y coordinates, and the third element is a reference to the + /// [`Cell`] to be drawn. + fn draw<'a, I>(&mut self, content: I) -> Result<(), Self::Error> + where + I: Iterator; + + /// Insert `n` line breaks to the terminal screen. + /// + /// This method is optional and may not be implemented by all backends. + fn append_lines(&mut self, _n: u16) -> Result<(), Self::Error> { + Ok(()) + } + + /// Hide the cursor on the terminal screen. + /// + /// + /// See also [`show_cursor`]. + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::backend::{TestBackend}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::backend::Backend; + /// + /// backend.hide_cursor()?; + /// // do something with hidden cursor + /// backend.show_cursor()?; + /// # std::io::Result::Ok(()) + /// ``` + /// + /// [`show_cursor`]: Self::show_cursor + fn hide_cursor(&mut self) -> Result<(), Self::Error>; + + /// Show the cursor on the terminal screen. + /// + /// See [`hide_cursor`] for an example. + /// + /// [`hide_cursor`]: Self::hide_cursor + fn show_cursor(&mut self) -> Result<(), Self::Error>; + + /// Get the current cursor position on the terminal screen. + /// + /// The returned tuple contains the x and y coordinates of the cursor. + /// The origin (0, 0) is at the top left corner of the screen. + /// + /// See [`set_cursor_position`] for an example. + /// + /// [`set_cursor_position`]: Self::set_cursor_position + fn get_cursor_position(&mut self) -> Result; + + /// Set the cursor position on the terminal screen to the given x and y coordinates. + /// + /// The origin (0, 0) is at the top left corner of the screen. + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::backend::{TestBackend}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::{backend::Backend, layout::Position}; + /// + /// backend.set_cursor_position(Position { x: 10, y: 20 })?; + /// assert_eq!(backend.get_cursor_position()?, Position { x: 10, y: 20 }); + /// # std::io::Result::Ok(()) + /// ``` + fn set_cursor_position>(&mut self, position: P) -> Result<(), Self::Error>; + + /// Get the current cursor position on the terminal screen. + /// + /// The returned tuple contains the x and y coordinates of the cursor. The origin + /// (0, 0) is at the top left corner of the screen. + #[deprecated = "use `get_cursor_position()` instead which returns `Result`"] + fn get_cursor(&mut self) -> Result<(u16, u16), Self::Error> { + let Position { x, y } = self.get_cursor_position()?; + Ok((x, y)) + } + + /// Set the cursor position on the terminal screen to the given x and y coordinates. + /// + /// The origin (0, 0) is at the top left corner of the screen. + #[deprecated = "use `set_cursor_position((x, y))` instead which takes `impl Into`"] + fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), Self::Error> { + self.set_cursor_position(Position { x, y }) + } + + /// Clears the whole terminal screen + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::backend::{TestBackend}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::backend::Backend; + /// + /// backend.clear()?; + /// # std::io::Result::Ok(()) + /// ``` + fn clear(&mut self) -> Result<(), Self::Error>; + + /// Clears a specific region of the terminal specified by the [`ClearType`] parameter + /// + /// This method is optional and may not be implemented by all backends. The default + /// implementation calls [`clear`] if the `clear_type` is [`ClearType::All`] and returns an + /// error otherwise. + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::{backend::{TestBackend}}; + /// # let mut backend = TestBackend::new(80, 25); + /// use ratatui::backend::{Backend, ClearType}; + /// + /// backend.clear_region(ClearType::All)?; + /// # std::io::Result::Ok(()) + /// ``` + /// + /// # Errors + /// + /// This method will return an error if the terminal screen could not be cleared. It will also + /// return an error if the `clear_type` is not supported by the backend. + /// + /// [`clear`]: Self::clear + fn clear_region(&mut self, clear_type: ClearType) -> Result<(), Self::Error>; + + /// Get the size of the terminal screen in columns/rows as a [`Size`]. + /// + /// The returned [`Size`] contains the width and height of the terminal screen. + /// + /// # Example + /// + /// ```rust,ignore + /// # use ratatui::{backend::{TestBackend}}; + /// # let backend = TestBackend::new(80, 25); + /// use ratatui::{backend::Backend, layout::Size}; + /// + /// assert_eq!(backend.size()?, Size::new(80, 25)); + /// # Result::Ok(()) + /// ``` + fn size(&self) -> Result; + + /// Get the size of the terminal screen in columns/rows and pixels as a [`WindowSize`]. + /// + /// The reason for this not returning only the pixel size, given the redundancy with the + /// `size()` method, is that the underlying backends most likely get both values with one + /// syscall, and the user is also most likely to need columns and rows along with pixel size. + fn window_size(&mut self) -> Result; + + /// Flush any buffered content to the terminal screen. + fn flush(&mut self) -> Result<(), Self::Error>; + + /// Scroll a region of the screen upwards, where a region is specified by a (half-open) range + /// of rows. + /// + /// Each row in the region is replaced by the row `line_count` rows below it, except the bottom + /// `line_count` rows, which are replaced by empty rows. If `line_count` is equal to or larger + /// than the number of rows in the region, then all rows are replaced with empty rows. + /// + /// If the region includes row 0, then `line_count` rows are copied into the bottom of the + /// scrollback buffer. These rows are first taken from the old contents of the region, starting + /// from the top. If there aren't sufficient rows in the region, then the remainder are empty + /// rows. + /// + /// The position of the cursor afterwards is undefined. + /// + /// The behavior is designed to match what ANSI terminals do when scrolling regions are + /// established. With ANSI terminals, a scrolling region can be established with the "^[[X;Yr" + /// sequence, where X and Y define the lines of the region. The scrolling region can be reset + /// to be the whole screen with the "^[[r" sequence. + /// + /// When a scrolling region is established in an ANSI terminal, various operations' behaviors + /// are changed in such a way that the scrolling region acts like a "virtual screen". In + /// particular, the scrolling sequence "^[[NS", which scrolls lines up by a count of N. + /// + /// On an ANSI terminal, this method will probably translate to something like: + /// "^[[X;Yr^[[NS^[[r". That is, set the scrolling region, scroll up, then reset the scrolling + /// region. + /// + /// For examples of how this function is expected to work, refer to the tests for + /// [`TestBackend::scroll_region_up`]. + #[cfg(feature = "scrolling-regions")] + fn scroll_region_up( + &mut self, + region: core::ops::Range, + line_count: u16, + ) -> Result<(), Self::Error>; + + /// Scroll a region of the screen downwards, where a region is specified by a (half-open) range + /// of rows. + /// + /// Each row in the region is replaced by the row `line_count` rows above it, except the top + /// `line_count` rows, which are replaced by empty rows. If `line_count` is equal to or larger + /// than the number of rows in the region, then all rows are replaced with empty rows. + /// + /// The position of the cursor afterwards is undefined. + /// + /// See the documentation for [`Self::scroll_region_down`] for more information about how this + /// is expected to be implemented for ANSI terminals. All of that applies, except the ANSI + /// sequence to scroll down is "^[[NT". + /// + /// This function is asymmetrical with regards to the scrollback buffer. The reason is that + /// this how terminals seem to implement things. + /// + /// For examples of how this function is expected to work, refer to the tests for + /// [`TestBackend::scroll_region_down`]. + #[cfg(feature = "scrolling-regions")] + fn scroll_region_down( + &mut self, + region: core::ops::Range, + line_count: u16, + ) -> Result<(), Self::Error>; +} + +#[cfg(test)] +mod tests { + use alloc::string::ToString; + + use strum::ParseError; + + use super::*; + + #[test] + fn clear_type_tostring() { + assert_eq!(ClearType::All.to_string(), "All"); + assert_eq!(ClearType::AfterCursor.to_string(), "AfterCursor"); + assert_eq!(ClearType::BeforeCursor.to_string(), "BeforeCursor"); + assert_eq!(ClearType::CurrentLine.to_string(), "CurrentLine"); + assert_eq!(ClearType::UntilNewLine.to_string(), "UntilNewLine"); + } + + #[test] + fn clear_type_from_str() { + assert_eq!("All".parse::(), Ok(ClearType::All)); + assert_eq!( + "AfterCursor".parse::(), + Ok(ClearType::AfterCursor) + ); + assert_eq!( + "BeforeCursor".parse::(), + Ok(ClearType::BeforeCursor) + ); + assert_eq!( + "CurrentLine".parse::(), + Ok(ClearType::CurrentLine) + ); + assert_eq!( + "UntilNewLine".parse::(), + Ok(ClearType::UntilNewLine) + ); + assert_eq!("".parse::(), Err(ParseError::VariantNotFound)); + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/backend/test.rs b/vendor/ratatui-jcode/ratatui-core/src/backend/test.rs new file mode 100644 index 00000000000..a99b058d7a0 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/backend/test.rs @@ -0,0 +1,1077 @@ +//! This module provides the `TestBackend` implementation for the [`Backend`] trait. +//! It is used in the integration tests to verify the correctness of the library. + +use alloc::string::String; +use alloc::vec; +use core::fmt::{self, Write}; +use core::iter; + +use unicode_width::UnicodeWidthStr; + +use crate::backend::{Backend, ClearType, WindowSize}; +use crate::buffer::{Buffer, Cell}; +use crate::layout::{Position, Rect, Size}; + +/// A [`Backend`] implementation used for integration testing that renders to an memory buffer. +/// +/// Note: that although many of the integration and unit tests in ratatui are written using this +/// backend, it is preferable to write unit tests for widgets directly against the buffer rather +/// than using this backend. This backend is intended for integration tests that test the entire +/// terminal UI. +/// +/// # Example +/// +/// ```rust,ignore +/// use ratatui::backend::{Backend, TestBackend}; +/// +/// let mut backend = TestBackend::new(10, 2); +/// backend.clear()?; +/// backend.assert_buffer_lines([" "; 2]); +/// # Result::Ok(()) +/// ``` +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct TestBackend { + buffer: Buffer, + scrollback: Buffer, + cursor: bool, + pos: (u16, u16), +} + +/// Returns a string representation of the given buffer for debugging purpose. +/// +/// This function is used to visualize the buffer content in a human-readable format. +/// It iterates through the buffer content and appends each cell's symbol to the view string. +/// If a cell is hidden by a multi-width symbol, it is added to the overwritten vector and +/// displayed at the end of the line. +fn buffer_view(buffer: &Buffer) -> String { + let mut view = String::with_capacity(buffer.content.len() + buffer.area.height as usize * 3); + for cells in buffer.content.chunks(buffer.area.width as usize) { + let mut overwritten = vec![]; + let mut skip: usize = 0; + view.push('"'); + for (x, c) in cells.iter().enumerate() { + if skip == 0 { + view.push_str(c.symbol()); + } else { + overwritten.push((x, c.symbol())); + } + skip = core::cmp::max(skip, c.symbol().width()).saturating_sub(1); + } + view.push('"'); + if !overwritten.is_empty() { + write!(&mut view, " Hidden by multi-width symbols: {overwritten:?}").unwrap(); + } + view.push('\n'); + } + view +} + +impl TestBackend { + /// Creates a new `TestBackend` with the specified width and height. + pub fn new(width: u16, height: u16) -> Self { + Self { + buffer: Buffer::empty(Rect::new(0, 0, width, height)), + scrollback: Buffer::empty(Rect::new(0, 0, width, 0)), + cursor: false, + pos: (0, 0), + } + } + + /// Creates a new `TestBackend` with the specified lines as the initial screen state. + /// + /// The backend's screen size is determined from the initial lines. + #[must_use] + pub fn with_lines<'line, Lines>(lines: Lines) -> Self + where + Lines: IntoIterator, + Lines::Item: Into>, + { + let buffer = Buffer::with_lines(lines); + let scrollback = Buffer::empty(Rect { + width: buffer.area.width, + ..Rect::ZERO + }); + Self { + buffer, + scrollback, + cursor: false, + pos: (0, 0), + } + } + + /// Returns a reference to the internal buffer of the `TestBackend`. + pub const fn buffer(&self) -> &Buffer { + &self.buffer + } + + /// Returns a reference to the internal scrollback buffer of the `TestBackend`. + /// + /// The scrollback buffer represents the part of the screen that is currently hidden from view, + /// but that could be accessed by scrolling back in the terminal's history. This would normally + /// be done using the terminal's scrollbar or an equivalent keyboard shortcut. + /// + /// The scrollback buffer starts out empty. Lines are appended when they scroll off the top of + /// the main buffer. This happens when lines are appended to the bottom of the main buffer + /// using [`Backend::append_lines`]. + /// + /// The scrollback buffer has a maximum height of [`u16::MAX`]. If lines are appended to the + /// bottom of the scrollback buffer when it is at its maximum height, a corresponding number of + /// lines will be removed from the top. + pub const fn scrollback(&self) -> &Buffer { + &self.scrollback + } + + /// Resizes the `TestBackend` to the specified width and height. + pub fn resize(&mut self, width: u16, height: u16) { + self.buffer.resize(Rect::new(0, 0, width, height)); + let scrollback_height = self.scrollback.area.height; + self.scrollback + .resize(Rect::new(0, 0, width, scrollback_height)); + } + + /// Asserts that the `TestBackend`'s buffer is equal to the expected buffer. + /// + /// This is a shortcut for `assert_eq!(self.buffer(), &expected)`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[expect(deprecated)] + #[track_caller] + pub fn assert_buffer(&self, expected: &Buffer) { + // TODO: use assert_eq!() + crate::assert_buffer_eq!(&self.buffer, expected); + } + + /// Asserts that the `TestBackend`'s scrollback buffer is equal to the expected buffer. + /// + /// This is a shortcut for `assert_eq!(self.scrollback(), &expected)`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[track_caller] + pub fn assert_scrollback(&self, expected: &Buffer) { + assert_eq!(&self.scrollback, expected); + } + + /// Asserts that the `TestBackend`'s scrollback buffer is empty. + /// + /// # Panics + /// + /// When the scrollback buffer is not equal, a panic occurs with a detailed error message + /// showing the differences between the expected and actual buffers. + pub fn assert_scrollback_empty(&self) { + let expected = Buffer { + area: Rect { + width: self.scrollback.area.width, + ..Rect::ZERO + }, + content: vec![], + }; + self.assert_scrollback(&expected); + } + + /// Asserts that the `TestBackend`'s buffer is equal to the expected lines. + /// + /// This is a shortcut for `assert_eq!(self.buffer(), &Buffer::with_lines(expected))`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[track_caller] + pub fn assert_buffer_lines<'line, Lines>(&self, expected: Lines) + where + Lines: IntoIterator, + Lines::Item: Into>, + { + self.assert_buffer(&Buffer::with_lines(expected)); + } + + /// Asserts that the `TestBackend`'s scrollback buffer is equal to the expected lines. + /// + /// This is a shortcut for `assert_eq!(self.scrollback(), &Buffer::with_lines(expected))`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual buffers. + #[track_caller] + pub fn assert_scrollback_lines<'line, Lines>(&self, expected: Lines) + where + Lines: IntoIterator, + Lines::Item: Into>, + { + self.assert_scrollback(&Buffer::with_lines(expected)); + } + + /// Asserts that the `TestBackend`'s cursor position is equal to the expected one. + /// + /// This is a shortcut for `assert_eq!(self.get_cursor_position().unwrap(), expected)`. + /// + /// # Panics + /// + /// When they are not equal, a panic occurs with a detailed error message showing the + /// differences between the expected and actual position. + #[track_caller] + pub fn assert_cursor_position>(&mut self, position: P) { + let actual = self.get_cursor_position().unwrap(); + assert_eq!(actual, position.into()); + } +} + +impl fmt::Display for TestBackend { + /// Formats the `TestBackend` for display by calling the `buffer_view` function + /// on its internal buffer. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", buffer_view(&self.buffer)) + } +} + +type Result = core::result::Result; + +impl Backend for TestBackend { + type Error = core::convert::Infallible; + + fn draw<'a, I>(&mut self, content: I) -> Result<()> + where + I: Iterator, + { + for (x, y, c) in content { + self.buffer[(x, y)] = c.clone(); + } + Ok(()) + } + + fn hide_cursor(&mut self) -> Result<()> { + self.cursor = false; + Ok(()) + } + + fn show_cursor(&mut self) -> Result<()> { + self.cursor = true; + Ok(()) + } + + fn get_cursor_position(&mut self) -> Result { + Ok(self.pos.into()) + } + + fn set_cursor_position>(&mut self, position: P) -> Result<()> { + self.pos = position.into().into(); + Ok(()) + } + + fn clear(&mut self) -> Result<()> { + self.buffer.reset(); + Ok(()) + } + + fn clear_region(&mut self, clear_type: ClearType) -> Result<()> { + let region = match clear_type { + ClearType::All => return self.clear(), + ClearType::AfterCursor => { + let index = self.buffer.index_of(self.pos.0, self.pos.1) + 1; + &mut self.buffer.content[index..] + } + ClearType::BeforeCursor => { + let index = self.buffer.index_of(self.pos.0, self.pos.1); + &mut self.buffer.content[..index] + } + ClearType::CurrentLine => { + let line_start_index = self.buffer.index_of(0, self.pos.1); + let line_end_index = self.buffer.index_of(self.buffer.area.width - 1, self.pos.1); + &mut self.buffer.content[line_start_index..=line_end_index] + } + ClearType::UntilNewLine => { + let index = self.buffer.index_of(self.pos.0, self.pos.1); + let line_end_index = self.buffer.index_of(self.buffer.area.width - 1, self.pos.1); + &mut self.buffer.content[index..=line_end_index] + } + }; + for cell in region { + cell.reset(); + } + Ok(()) + } + + /// Inserts n line breaks at the current cursor position. + /// + /// After the insertion, the cursor x position will be incremented by 1 (unless it's already + /// at the end of line). This is a common behaviour of terminals in raw mode. + /// + /// If the number of lines to append is fewer than the number of lines in the buffer after the + /// cursor y position then the cursor is moved down by n rows. + /// + /// If the number of lines to append is greater than the number of lines in the buffer after + /// the cursor y position then that number of empty lines (at most the buffer's height in this + /// case but this limit is instead replaced with scrolling in most backend implementations) will + /// be added after the current position and the cursor will be moved to the last row. + fn append_lines(&mut self, line_count: u16) -> Result<()> { + let Position { x: cur_x, y: cur_y } = self.get_cursor_position()?; + let Rect { width, height, .. } = self.buffer.area; + + // the next column ensuring that we don't go past the last column + let new_cursor_x = cur_x.saturating_add(1).min(width.saturating_sub(1)); + + let max_y = height.saturating_sub(1); + let lines_after_cursor = max_y.saturating_sub(cur_y); + + if line_count > lines_after_cursor { + // We need to insert blank lines at the bottom and scroll the lines from the top into + // scrollback. + let scroll_by: usize = (line_count - lines_after_cursor).into(); + let width: usize = self.buffer.area.width.into(); + let cells_to_scrollback = self.buffer.content.len().min(width * scroll_by); + + append_to_scrollback( + &mut self.scrollback, + self.buffer.content.splice( + 0..cells_to_scrollback, + iter::repeat_with(Default::default).take(cells_to_scrollback), + ), + ); + self.buffer.content.rotate_left(cells_to_scrollback); + append_to_scrollback( + &mut self.scrollback, + iter::repeat_with(Default::default).take(width * scroll_by - cells_to_scrollback), + ); + } + + let new_cursor_y = cur_y.saturating_add(line_count).min(max_y); + self.set_cursor_position(Position::new(new_cursor_x, new_cursor_y))?; + + Ok(()) + } + + fn size(&self) -> Result { + Ok(self.buffer.area.as_size()) + } + + fn window_size(&mut self) -> Result { + // Some arbitrary window pixel size, probably doesn't need much testing. + const WINDOW_PIXEL_SIZE: Size = Size { + width: 640, + height: 480, + }; + Ok(WindowSize { + columns_rows: self.buffer.area.as_size(), + pixels: WINDOW_PIXEL_SIZE, + }) + } + + fn flush(&mut self) -> Result<()> { + Ok(()) + } + + #[cfg(feature = "scrolling-regions")] + fn scroll_region_up(&mut self, region: core::ops::Range, scroll_by: u16) -> Result<()> { + let width: usize = self.buffer.area.width.into(); + let cell_region_start = width * region.start.min(self.buffer.area.height) as usize; + let cell_region_end = width * region.end.min(self.buffer.area.height) as usize; + let cell_region_len = cell_region_end - cell_region_start; + let cells_to_scroll_by = width * scroll_by as usize; + + // Deal with the simple case where nothing needs to be copied into scrollback. + if cell_region_start > 0 { + if cells_to_scroll_by >= cell_region_len { + // The scroll amount is large enough to clear the whole region. + self.buffer.content[cell_region_start..cell_region_end].fill_with(Default::default); + } else { + // Scroll up by rotating, then filling in the bottom with empty cells. + self.buffer.content[cell_region_start..cell_region_end] + .rotate_left(cells_to_scroll_by); + self.buffer.content[cell_region_end - cells_to_scroll_by..cell_region_end] + .fill_with(Default::default); + } + return Ok(()); + } + + // The rows inserted into the scrollback will first come from the buffer, and if that is + // insufficient, will then be blank rows. + let cells_from_region = cell_region_len.min(cells_to_scroll_by); + append_to_scrollback( + &mut self.scrollback, + self.buffer.content.splice( + 0..cells_from_region, + iter::repeat_with(Default::default).take(cells_from_region), + ), + ); + if cells_to_scroll_by < cell_region_len { + // Rotate the remaining cells to the front of the region. + self.buffer.content[cell_region_start..cell_region_end].rotate_left(cells_from_region); + } else { + // Splice cleared out the region. Insert empty rows in scrollback. + append_to_scrollback( + &mut self.scrollback, + iter::repeat_with(Default::default).take(cells_to_scroll_by - cell_region_len), + ); + } + Ok(()) + } + + #[cfg(feature = "scrolling-regions")] + fn scroll_region_down(&mut self, region: core::ops::Range, scroll_by: u16) -> Result<()> { + let width: usize = self.buffer.area.width.into(); + let cell_region_start = width * region.start.min(self.buffer.area.height) as usize; + let cell_region_end = width * region.end.min(self.buffer.area.height) as usize; + let cell_region_len = cell_region_end - cell_region_start; + let cells_to_scroll_by = width * scroll_by as usize; + + if cells_to_scroll_by >= cell_region_len { + // The scroll amount is large enough to clear the whole region. + self.buffer.content[cell_region_start..cell_region_end].fill_with(Default::default); + } else { + // Scroll up by rotating, then filling in the top with empty cells. + self.buffer.content[cell_region_start..cell_region_end] + .rotate_right(cells_to_scroll_by); + self.buffer.content[cell_region_start..cell_region_start + cells_to_scroll_by] + .fill_with(Default::default); + } + Ok(()) + } +} + +/// Append the provided cells to the bottom of a scrollback buffer. The number of cells must be a +/// multiple of the buffer's width. If the scrollback buffer ends up larger than 65535 lines tall, +/// then lines will be removed from the top to get it down to size. +fn append_to_scrollback(scrollback: &mut Buffer, cells: impl IntoIterator) { + scrollback.content.extend(cells); + let width = scrollback.area.width as usize; + let new_height = (scrollback.content.len() / width).min(u16::MAX as usize); + let keep_from = scrollback + .content + .len() + .saturating_sub(width * u16::MAX as usize); + scrollback.content.drain(0..keep_from); + scrollback.area.height = new_height as u16; +} + +#[cfg(test)] +mod tests { + use alloc::format; + + use itertools::Itertools as _; + + use super::*; + + #[test] + fn new() { + assert_eq!( + TestBackend::new(10, 2), + TestBackend { + buffer: Buffer::with_lines([" "; 2]), + scrollback: Buffer::empty(Rect::new(0, 0, 10, 0)), + cursor: false, + pos: (0, 0), + } + ); + } + #[test] + fn test_buffer_view() { + let buffer = Buffer::with_lines(["aaaa"; 2]); + assert_eq!(buffer_view(&buffer), "\"aaaa\"\n\"aaaa\"\n"); + } + + #[test] + fn buffer_view_with_overwrites() { + let multi_byte_char = "👨‍👩‍👧‍👦"; // renders 2 wide + let buffer = Buffer::with_lines([multi_byte_char]); + assert_eq!( + buffer_view(&buffer), + format!( + r#""{multi_byte_char}" Hidden by multi-width symbols: [(1, " ")] +"#, + ) + ); + } + + #[test] + fn buffer() { + let backend = TestBackend::new(10, 2); + backend.assert_buffer_lines([" "; 2]); + } + + #[test] + fn resize() { + let mut backend = TestBackend::new(10, 2); + backend.resize(5, 5); + backend.assert_buffer_lines([" "; 5]); + } + + #[test] + fn assert_buffer() { + let backend = TestBackend::new(10, 2); + backend.assert_buffer_lines([" "; 2]); + } + + #[test] + #[should_panic = "buffer contents not equal"] + fn assert_buffer_panics() { + let backend = TestBackend::new(10, 2); + backend.assert_buffer_lines(["aaaaaaaaaa"; 2]); + } + + #[test] + #[should_panic = "assertion `left == right` failed"] + fn assert_scrollback_panics() { + let backend = TestBackend::new(10, 2); + backend.assert_scrollback_lines(["aaaaaaaaaa"; 2]); + } + + #[test] + fn display() { + let backend = TestBackend::new(10, 2); + assert_eq!(format!("{backend}"), "\" \"\n\" \"\n"); + } + + #[test] + fn draw() { + let mut backend = TestBackend::new(10, 2); + let cell = Cell::new("a"); + backend.draw([(0, 0, &cell)].into_iter()).unwrap(); + backend.draw([(0, 1, &cell)].into_iter()).unwrap(); + backend.assert_buffer_lines(["a "; 2]); + } + + #[test] + fn hide_cursor() { + let mut backend = TestBackend::new(10, 2); + backend.hide_cursor().unwrap(); + assert!(!backend.cursor); + } + + #[test] + fn show_cursor() { + let mut backend = TestBackend::new(10, 2); + backend.show_cursor().unwrap(); + assert!(backend.cursor); + } + + #[test] + fn get_cursor_position() { + let mut backend = TestBackend::new(10, 2); + assert_eq!(backend.get_cursor_position().unwrap(), Position::ORIGIN); + } + + #[test] + fn assert_cursor_position() { + let mut backend = TestBackend::new(10, 2); + backend.assert_cursor_position(Position::ORIGIN); + } + + #[test] + fn set_cursor_position() { + let mut backend = TestBackend::new(10, 10); + backend + .set_cursor_position(Position { x: 5, y: 5 }) + .unwrap(); + assert_eq!(backend.pos, (5, 5)); + } + + #[test] + fn clear() { + let mut backend = TestBackend::new(4, 2); + let cell = Cell::new("a"); + backend.draw([(0, 0, &cell)].into_iter()).unwrap(); + backend.draw([(0, 1, &cell)].into_iter()).unwrap(); + backend.clear().unwrap(); + backend.assert_buffer_lines([" ", " "]); + } + + #[test] + fn clear_region_all() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend.clear_region(ClearType::All).unwrap(); + backend.assert_buffer_lines([ + " ", + " ", + " ", + " ", + " ", + ]); + } + + #[test] + fn clear_region_after_cursor() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 3, y: 2 }) + .unwrap(); + backend.clear_region(ClearType::AfterCursor).unwrap(); + backend.assert_buffer_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaa ", + " ", + " ", + ]); + } + + #[test] + fn clear_region_before_cursor() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 5, y: 3 }) + .unwrap(); + backend.clear_region(ClearType::BeforeCursor).unwrap(); + backend.assert_buffer_lines([ + " ", + " ", + " ", + " aaaaa", + "aaaaaaaaaa", + ]); + } + + #[test] + fn clear_region_current_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 3, y: 1 }) + .unwrap(); + backend.clear_region(ClearType::CurrentLine).unwrap(); + backend.assert_buffer_lines([ + "aaaaaaaaaa", + " ", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + } + + #[test] + fn clear_region_until_new_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + + backend + .set_cursor_position(Position { x: 3, y: 0 }) + .unwrap(); + backend.clear_region(ClearType::UntilNewLine).unwrap(); + backend.assert_buffer_lines([ + "aaa ", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + "aaaaaaaaaa", + ]); + } + + #[test] + fn append_lines_not_at_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend.set_cursor_position(Position::ORIGIN).unwrap(); + + // If the cursor is not at the last line in the terminal the addition of a + // newline simply moves the cursor down and to the right + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 1 }); + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 2, y: 2 }); + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 3, y: 3 }); + + backend.append_lines(1).unwrap(); + backend.assert_cursor_position(Position { x: 4, y: 4 }); + + // As such the buffer should remain unchanged + backend.assert_buffer_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + backend.assert_scrollback_empty(); + } + + #[test] + fn append_lines_at_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + // If the cursor is at the last line in the terminal the addition of a + // newline will scroll the contents of the buffer + backend + .set_cursor_position(Position { x: 0, y: 4 }) + .unwrap(); + + backend.append_lines(1).unwrap(); + + backend.assert_buffer_lines([ + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + ]); + backend.assert_scrollback_lines(["aaaaaaaaaa"]); + + // It also moves the cursor to the right, as is common of the behaviour of + // terminals in raw-mode + backend.assert_cursor_position(Position { x: 1, y: 4 }); + } + + #[test] + fn append_multiple_lines_not_at_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend.set_cursor_position(Position::ORIGIN).unwrap(); + + // If the cursor is not at the last line in the terminal the addition of multiple + // newlines simply moves the cursor n lines down and to the right by 1 + + backend.append_lines(4).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + // As such the buffer should remain unchanged + backend.assert_buffer_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + backend.assert_scrollback_empty(); + } + + #[test] + fn append_multiple_lines_past_last_line() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend + .set_cursor_position(Position { x: 0, y: 3 }) + .unwrap(); + + backend.append_lines(3).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + " ", + ]); + backend.assert_scrollback_lines(["aaaaaaaaaa", "bbbbbbbbbb"]); + } + + #[test] + fn append_multiple_lines_where_cursor_at_end_appends_height_lines() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend + .set_cursor_position(Position { x: 0, y: 4 }) + .unwrap(); + + backend.append_lines(5).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + " ", + " ", + " ", + " ", + " ", + ]); + backend.assert_scrollback_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + } + + #[test] + fn append_multiple_lines_where_cursor_appends_height_lines() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend.set_cursor_position(Position::ORIGIN).unwrap(); + + backend.append_lines(5).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + ]); + backend.assert_scrollback_lines(["aaaaaaaaaa"]); + } + + #[test] + fn append_multiple_lines_where_cursor_at_end_appends_more_than_height_lines() { + let mut backend = TestBackend::with_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + ]); + + backend + .set_cursor_position(Position { x: 0, y: 4 }) + .unwrap(); + + backend.append_lines(8).unwrap(); + backend.assert_cursor_position(Position { x: 1, y: 4 }); + + backend.assert_buffer_lines([ + " ", + " ", + " ", + " ", + " ", + ]); + backend.assert_scrollback_lines([ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + " ", + " ", + " ", + ]); + } + + #[test] + fn append_lines_truncates_beyond_u16_max() -> Result<()> { + let mut backend = TestBackend::new(10, 5); + + // Fill the scrollback with 65535 + 10 lines. + let row_count = u16::MAX as usize + 10; + for row in 0..=row_count { + if row > 4 { + backend.set_cursor_position(Position { x: 0, y: 4 })?; + backend.append_lines(1)?; + } + let cells = format!("{row:>10}").chars().map(Cell::from).collect_vec(); + let content = cells + .iter() + .enumerate() + .map(|(column, cell)| (column as u16, 4.min(row) as u16, cell)); + backend.draw(content)?; + } + + // check that the buffer contains the last 5 lines appended + backend.assert_buffer_lines([ + " 65541", + " 65542", + " 65543", + " 65544", + " 65545", + ]); + + // TODO: ideally this should be something like: + // let lines = (6..=65545).map(|row| format!("{row:>10}")); + // backend.assert_scrollback_lines(lines); + // but there's some truncation happening in Buffer::with_lines that needs to be fixed + assert_eq!( + Buffer { + area: Rect::new(0, 0, 10, 5), + content: backend.scrollback.content[0..10 * 5].to_vec(), + }, + Buffer::with_lines([ + " 6", + " 7", + " 8", + " 9", + " 10", + ]), + "first 5 lines of scrollback should have been truncated" + ); + + assert_eq!( + Buffer { + area: Rect::new(0, 0, 10, 5), + content: backend.scrollback.content[10 * 65530..10 * 65535].to_vec(), + }, + Buffer::with_lines([ + " 65536", + " 65537", + " 65538", + " 65539", + " 65540", + ]), + "last 5 lines of scrollback should have been appended" + ); + + // These checks come after the content checks as otherwise we won't see the failing content + // when these checks fail. + // Make sure the scrollback is the right size. + assert_eq!(backend.scrollback.area.width, 10); + assert_eq!(backend.scrollback.area.height, 65535); + assert_eq!(backend.scrollback.content.len(), 10 * 65535); + Ok(()) + } + + #[test] + fn size() { + let backend = TestBackend::new(10, 2); + assert_eq!(backend.size().unwrap(), Size::new(10, 2)); + } + + #[test] + fn flush() { + let mut backend = TestBackend::new(10, 2); + backend.flush().unwrap(); + } + + #[cfg(feature = "scrolling-regions")] + mod scrolling_regions { + use rstest::rstest; + + use super::*; + + const A: &str = "aaaa"; + const B: &str = "bbbb"; + const C: &str = "cccc"; + const D: &str = "dddd"; + const E: &str = "eeee"; + const S: &str = " "; + + #[rstest] + #[case([A, B, C, D, E], 0..5, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..5, 2, [A, B], [C, D, E, S, S])] + #[case([A, B, C, D, E], 0..5, 5, [A, B, C, D, E], [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..5, 7, [A, B, C, D, E, S, S], [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..3, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..3, 2, [A, B], [C, S, S, D, E])] + #[case([A, B, C, D, E], 0..3, 3, [A, B, C], [S, S, S, D, E])] + #[case([A, B, C, D, E], 0..3, 4, [A, B, C, S], [S, S, S, D, E])] + #[case([A, B, C, D, E], 1..4, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 1..4, 2, [], [A, D, S, S, E])] + #[case([A, B, C, D, E], 1..4, 3, [], [A, S, S, S, E])] + #[case([A, B, C, D, E], 1..4, 4, [], [A, S, S, S, E])] + #[case([A, B, C, D, E], 0..0, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..0, 2, [S, S], [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 0, [], [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 2, [], [A, B, C, D, E])] + fn scroll_region_up( + #[case] initial_screen: [&'static str; L], + #[case] range: core::ops::Range, + #[case] scroll_by: u16, + #[case] expected_scrollback: [&'static str; M], + #[case] expected_buffer: [&'static str; N], + ) { + let mut backend = TestBackend::with_lines(initial_screen); + backend.scroll_region_up(range, scroll_by).unwrap(); + if expected_scrollback.is_empty() { + backend.assert_scrollback_empty(); + } else { + backend.assert_scrollback_lines(expected_scrollback); + } + backend.assert_buffer_lines(expected_buffer); + } + + #[rstest] + #[case([A, B, C, D, E], 0..5, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..5, 2, [S, S, A, B, C])] + #[case([A, B, C, D, E], 0..5, 5, [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..5, 7, [S, S, S, S, S])] + #[case([A, B, C, D, E], 0..3, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..3, 2, [S, S, A, D, E])] + #[case([A, B, C, D, E], 0..3, 3, [S, S, S, D, E])] + #[case([A, B, C, D, E], 0..3, 4, [S, S, S, D, E])] + #[case([A, B, C, D, E], 1..4, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 1..4, 2, [A, S, S, B, E])] + #[case([A, B, C, D, E], 1..4, 3, [A, S, S, S, E])] + #[case([A, B, C, D, E], 1..4, 4, [A, S, S, S, E])] + #[case([A, B, C, D, E], 0..0, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 0..0, 2, [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 0, [A, B, C, D, E])] + #[case([A, B, C, D, E], 2..2, 2, [A, B, C, D, E])] + fn scroll_region_down( + #[case] initial_screen: [&'static str; M], + #[case] range: core::ops::Range, + #[case] scroll_by: u16, + #[case] expected_buffer: [&'static str; N], + ) { + let mut backend = TestBackend::with_lines(initial_screen); + backend.scroll_region_down(range, scroll_by).unwrap(); + backend.assert_scrollback_empty(); + backend.assert_buffer_lines(expected_buffer); + } + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/buffer.rs b/vendor/ratatui-jcode/ratatui-core/src/buffer.rs new file mode 100644 index 00000000000..e7ac4d741e0 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/buffer.rs @@ -0,0 +1,9 @@ +#![warn(missing_docs)] +//! A module for the [`Buffer`] and [`Cell`] types. + +mod assert; +mod buffer; +mod cell; + +pub use buffer::Buffer; +pub use cell::Cell; diff --git a/vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs b/vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs new file mode 100644 index 00000000000..c4afafb7888 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/buffer/assert.rs @@ -0,0 +1,71 @@ +/// Assert that two buffers are equal by comparing their areas and content. +/// +/// # Panics +/// When the buffers differ this method panics and displays the differences similar to +/// `assert_eq!()`. +#[deprecated = "use `assert_eq!(&actual, &expected)`"] +#[macro_export] +macro_rules! assert_buffer_eq { + ($actual_expr:expr, $expected_expr:expr) => { + match (&$actual_expr, &$expected_expr) { + (actual, expected) => { + assert!( + actual.area == expected.area, + "buffer areas not equal\nexpected: {expected:?}\nactual: {actual:?}", + ); + let nice_diff = expected + .diff(actual) + .into_iter() + .enumerate() + .map(|(i, (x, y, cell))| { + let expected_cell = &expected[(x, y)]; + ::alloc::format!("{i}: at ({x}, {y})\n expected: {expected_cell:?}\n actual: {cell:?}") + }) + .collect::<::alloc::vec::Vec<::alloc::string::String>>() + .join("\n"); + assert!( + nice_diff.is_empty(), + "buffer contents not equal\nexpected: {expected:?}\nactual: {actual:?}\ndiff:\n{nice_diff}", + ); + // shouldn't get here, but this guards against future behavior + // that changes equality but not area or content + assert_eq!( + actual, expected, + "buffers are not equal in an unexpected way. Please open an issue about this." + ); + } + } + }; +} + +#[expect(deprecated)] +#[cfg(test)] +mod tests { + use crate::buffer::Buffer; + use crate::layout::Rect; + use crate::style::{Color, Style}; + + #[test] + fn assert_buffer_eq_does_not_panic_on_equal_buffers() { + let buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + let other_buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + assert_buffer_eq!(buffer, other_buffer); + } + + #[should_panic = "buffer areas not equal"] + #[test] + fn assert_buffer_eq_panics_on_unequal_area() { + let buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + let other_buffer = Buffer::empty(Rect::new(0, 0, 6, 1)); + assert_buffer_eq!(buffer, other_buffer); + } + + #[should_panic = "buffer contents not equal"] + #[test] + fn assert_buffer_eq_panics_on_unequal_style() { + let buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + let mut other_buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); + other_buffer.set_string(0, 0, " ", Style::default().fg(Color::Red)); + assert_buffer_eq!(buffer, other_buffer); + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs b/vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs new file mode 100644 index 00000000000..a1d60fde3f9 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/buffer/buffer.rs @@ -0,0 +1,1371 @@ +use alloc::vec; +use alloc::vec::Vec; +use core::ops::{Index, IndexMut}; +use core::{cmp, fmt}; + +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; + +use crate::buffer::Cell; +use crate::layout::{Position, Rect}; +use crate::style::Style; +use crate::text::{Line, Span}; + +/// A buffer that maps to the desired content of the terminal after the draw call +/// +/// No widget in the library interacts directly with the terminal. Instead each of them is required +/// to draw their state to an intermediate buffer. It is basically a grid where each cell contains +/// a grapheme, a foreground color and a background color. This grid will then be used to output +/// the appropriate escape sequences and characters to draw the UI as the user has defined it. +/// +/// # Examples: +/// +/// ``` +/// use ratatui_core::buffer::{Buffer, Cell}; +/// use ratatui_core::layout::{Position, Rect}; +/// use ratatui_core::style::{Color, Style}; +/// +/// # fn foo() -> Option<()> { +/// let mut buf = Buffer::empty(Rect { +/// x: 0, +/// y: 0, +/// width: 10, +/// height: 5, +/// }); +/// +/// // indexing using Position +/// buf[Position { x: 0, y: 0 }].set_symbol("A"); +/// assert_eq!(buf[Position { x: 0, y: 0 }].symbol(), "A"); +/// +/// // indexing using (x, y) tuple (which is converted to Position) +/// buf[(0, 1)].set_symbol("B"); +/// assert_eq!(buf[(0, 1)].symbol(), "x"); +/// +/// // getting an Option instead of panicking if the position is outside the buffer +/// let cell = buf.cell_mut(Position { x: 0, y: 2 })?; +/// cell.set_symbol("C"); +/// +/// let cell = buf.cell(Position { x: 0, y: 2 })?; +/// assert_eq!(cell.symbol(), "C"); +/// +/// buf.set_string( +/// 3, +/// 0, +/// "string", +/// Style::default().fg(Color::Red).bg(Color::White), +/// ); +/// let cell = &buf[(5, 0)]; // cannot move out of buf, so we borrow it +/// assert_eq!(cell.symbol(), "r"); +/// assert_eq!(cell.fg, Color::Red); +/// assert_eq!(cell.bg, Color::White); +/// # Some(()) +/// # } +/// ``` +#[derive(Default, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Buffer { + /// The area represented by this buffer + pub area: Rect, + /// The content of the buffer. The length of this Vec should always be equal to area.width * + /// area.height + pub content: Vec, +} + +impl Buffer { + /// Returns a Buffer with all cells set to the default one + #[must_use] + pub fn empty(area: Rect) -> Self { + Self::filled(area, Cell::EMPTY) + } + + /// Returns a Buffer with all cells initialized with the attributes of the given Cell + #[must_use] + pub fn filled(area: Rect, cell: Cell) -> Self { + let size = area.area() as usize; + let content = vec![cell; size]; + Self { area, content } + } + + /// Returns a Buffer containing the given lines + #[must_use] + pub fn with_lines<'a, Iter>(lines: Iter) -> Self + where + Iter: IntoIterator, + Iter::Item: Into>, + { + let lines = lines.into_iter().map(Into::into).collect::>(); + let height = lines.len() as u16; + let width = lines.iter().map(Line::width).max().unwrap_or_default() as u16; + let mut buffer = Self::empty(Rect::new(0, 0, width, height)); + for (y, line) in lines.iter().enumerate() { + buffer.set_line(0, y as u16, line, width); + } + buffer + } + + /// Returns the content of the buffer as a slice + pub fn content(&self) -> &[Cell] { + &self.content + } + + /// Returns the area covered by this buffer + pub const fn area(&self) -> &Rect { + &self.area + } + + /// Returns a reference to the [`Cell`] at the given coordinates + /// + /// Callers should use [`Buffer[]`](Self::index) or [`Buffer::cell`] instead of this method. + /// + /// Note: idiomatically methods named `get` usually return `Option<&T>`, but this method panics + /// instead. This is kept for backwards compatibility. See [`cell`](Self::cell) for a safe + /// alternative. + /// + /// # Panics + /// + /// Panics if the index is out of bounds. + #[track_caller] + #[deprecated = "use `Buffer[(x, y)]` instead. To avoid panicking, use `Buffer::cell((x, y))`. Both methods take `impl Into`."] + #[must_use] + pub fn get(&self, x: u16, y: u16) -> &Cell { + let i = self.index_of(x, y); + &self.content[i] + } + + /// Returns a mutable reference to the [`Cell`] at the given coordinates. + /// + /// Callers should use [`Buffer[]`](Self::index_mut) or [`Buffer::cell_mut`] instead of this + /// method. + /// + /// Note: idiomatically methods named `get_mut` usually return `Option<&mut T>`, but this method + /// panics instead. This is kept for backwards compatibility. See [`cell_mut`](Self::cell_mut) + /// for a safe alternative. + /// + /// # Panics + /// + /// Panics if the position is outside the `Buffer`'s area. + #[track_caller] + #[deprecated = "use `Buffer[(x, y)]` instead. To avoid panicking, use `Buffer::cell_mut((x, y))`. Both methods take `impl Into`."] + #[must_use] + pub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell { + let i = self.index_of(x, y); + &mut self.content[i] + } + + /// Returns a reference to the [`Cell`] at the given position or [`None`] if the position is + /// outside the `Buffer`'s area. + /// + /// This method accepts any value that can be converted to [`Position`] (e.g. `(x, y)` or + /// `Position::new(x, y)`). + /// + /// For a method that panics when the position is outside the buffer instead of returning + /// `None`, use [`Buffer[]`](Self::index). + /// + /// # Examples + /// + /// ```rust + /// use ratatui_core::buffer::{Buffer, Cell}; + /// use ratatui_core::layout::{Position, Rect}; + /// + /// let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); + /// + /// assert_eq!(buffer.cell(Position::new(0, 0)), Some(&Cell::default())); + /// assert_eq!(buffer.cell(Position::new(10, 10)), None); + /// assert_eq!(buffer.cell((0, 0)), Some(&Cell::default())); + /// assert_eq!(buffer.cell((10, 10)), None); + /// ``` + #[must_use] + pub fn cell>(&self, position: P) -> Option<&Cell> { + let position = position.into(); + let index = self.index_of_opt(position)?; + self.content.get(index) + } + + /// Returns a mutable reference to the [`Cell`] at the given position or [`None`] if the + /// position is outside the `Buffer`'s area. + /// + /// This method accepts any value that can be converted to [`Position`] (e.g. `(x, y)` or + /// `Position::new(x, y)`). + /// + /// For a method that panics when the position is outside the buffer instead of returning + /// `None`, use [`Buffer[]`](Self::index_mut). + /// + /// # Examples + /// + /// ```rust + /// use ratatui_core::buffer::{Buffer, Cell}; + /// use ratatui_core::layout::{Position, Rect}; + /// use ratatui_core::style::{Color, Style}; + /// let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); + /// + /// if let Some(cell) = buffer.cell_mut(Position::new(0, 0)) { + /// cell.set_symbol("A"); + /// } + /// if let Some(cell) = buffer.cell_mut((0, 0)) { + /// cell.set_style(Style::default().fg(Color::Red)); + /// } + /// ``` + #[must_use] + pub fn cell_mut>(&mut self, position: P) -> Option<&mut Cell> { + let position = position.into(); + let index = self.index_of_opt(position)?; + self.content.get_mut(index) + } + + /// Returns the index in the `Vec` for the given global (x, y) coordinates. + /// + /// Global coordinates are offset by the Buffer's area offset (`x`/`y`). + /// + /// Usage discouraged, as it exposes `self.content` as a linearly indexable array, which limits + /// potential future abstractions. See . + /// + /// # Examples + /// + /// ``` + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let buffer = Buffer::empty(Rect::new(200, 100, 10, 10)); + /// // Global coordinates to the top corner of this buffer's area + /// assert_eq!(buffer.index_of(200, 100), 0); + /// ``` + /// + /// # Panics + /// + /// Panics when given an coordinate that is outside of this Buffer's area. + /// + /// ```should_panic + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let buffer = Buffer::empty(Rect::new(200, 100, 10, 10)); + /// // Top coordinate is outside of the buffer in global coordinate space, as the Buffer's area + /// // starts at (200, 100). + /// buffer.index_of(0, 0); // Panics + /// ``` + #[track_caller] + #[must_use] + pub fn index_of(&self, x: u16, y: u16) -> usize { + self.index_of_opt(Position { x, y }).unwrap_or_else(|| { + panic!( + "index outside of buffer: the area is {area:?} but index is ({x}, {y})", + area = self.area, + ) + }) + } + + /// Returns the index in the `Vec` for the given global (x, y) coordinates. + /// + /// Returns `None` if the given coordinates are outside of the Buffer's area. + /// + /// Note that this is private because of + #[must_use] + const fn index_of_opt(&self, position: Position) -> Option { + let area = self.area; + if !area.contains(position) { + return None; + } + // remove offset + let y = (position.y - self.area.y) as usize; + let x = (position.x - self.area.x) as usize; + let width = self.area.width as usize; + Some(y * width + x) + } + + /// Returns the (global) coordinates of a cell given its index. + /// + /// Global coordinates are offset by the Buffer's area offset (`x`/`y`). + /// + /// Usage discouraged, as it exposes `self.content` as a linearly indexable array, which limits + /// potential future abstractions. See . + /// + /// # Examples + /// + /// ``` + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let rect = Rect::new(200, 100, 10, 10); + /// let buffer = Buffer::empty(rect); + /// assert_eq!(buffer.pos_of(0), (200, 100)); + /// assert_eq!(buffer.pos_of(14), (204, 101)); + /// ``` + /// + /// # Panics + /// + /// Panics when given an index that is outside the Buffer's content. + /// + /// ```should_panic + /// use ratatui_core::buffer::Buffer; + /// use ratatui_core::layout::Rect; + /// + /// let rect = Rect::new(0, 0, 10, 10); // 100 cells in total + /// let buffer = Buffer::empty(rect); + /// // Index 100 is the 101th cell, which lies outside of the area of this Buffer. + /// buffer.pos_of(100); // Panics + /// ``` + #[must_use] + pub fn pos_of(&self, index: usize) -> (u16, u16) { + debug_assert!( + index < self.content.len(), + "Trying to get the coords of a cell outside the buffer: i={index} len={}", + self.content.len() + ); + let x = index % self.area.width as usize + self.area.x as usize; + let y = index / self.area.width as usize + self.area.y as usize; + ( + u16::try_from(x).expect("x overflow. This should never happen as area.width is u16"), + u16::try_from(y).expect("y overflow. This should never happen as area.height is u16"), + ) + } + + /// Print a string, starting at the position (x, y) + pub fn set_string(&mut self, x: u16, y: u16, string: T, style: S) + where + T: AsRef, + S: Into +//!
+//!
+//!
+//!
+//!
C50
+//!
C100
+//!
C200
+//!
C300
+//!
C400
+//!
C500
+//!
C600
+//!
C700
+//!
C800
+//!
C900
+//!
A100
+//!
A200
+//!
A400
+//!
A700
+//!
+//!
+//!
+//! +//! [`RED`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PINK`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PURPLE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`DEEP_PURPLE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`INDIGO`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLUE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIGHT_BLUE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`CYAN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`TEAL`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GREEN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIGHT_GREEN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIME`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`YELLOW`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`AMBER`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`ORANGE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`DEEP_ORANGE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BROWN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GRAY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLUE_GRAY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLACK`]
+//!
+//!
+//!
+//!
+//! +//! [`WHITE`]
+//!
+//!
+//!
+//!
+//! +//! # Example +//! +//! ```rust +//! use ratatui_core::style::Color; +//! use ratatui_core::style::palette::material::{BLUE, RED}; +//! +//! assert_eq!(RED.c500, Color::Rgb(244, 67, 54)); +//! assert_eq!(BLUE.c500, Color::Rgb(33, 150, 243)); +//! ``` +//! +//! [`matdesign-color` crate]: https://crates.io/crates/matdesign-color + +use crate::style::Color; + +/// A palette of colors for use in Material design with accent colors +/// +/// This is a collection of colors that are used in Material design. They consist of a set of +/// colors from 50 to 900, and a set of accent colors from 100 to 700. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct AccentedPalette { + pub c50: Color, + pub c100: Color, + pub c200: Color, + pub c300: Color, + pub c400: Color, + pub c500: Color, + pub c600: Color, + pub c700: Color, + pub c800: Color, + pub c900: Color, + pub a100: Color, + pub a200: Color, + pub a400: Color, + pub a700: Color, +} + +/// A palette of colors for use in Material design without accent colors +/// +/// This is a collection of colors that are used in Material design. They consist of a set of +/// colors from 50 to 900. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct NonAccentedPalette { + pub c50: Color, + pub c100: Color, + pub c200: Color, + pub c300: Color, + pub c400: Color, + pub c500: Color, + pub c600: Color, + pub c700: Color, + pub c800: Color, + pub c900: Color, +} + +impl AccentedPalette { + /// Create a new `AccentedPalette` from the given variants + /// + /// The variants should be in the format [0x00RRGGBB, ...] + pub const fn from_variants(variants: [u32; 14]) -> Self { + Self { + c50: Color::from_u32(variants[0]), + c100: Color::from_u32(variants[1]), + c200: Color::from_u32(variants[2]), + c300: Color::from_u32(variants[3]), + c400: Color::from_u32(variants[4]), + c500: Color::from_u32(variants[5]), + c600: Color::from_u32(variants[6]), + c700: Color::from_u32(variants[7]), + c800: Color::from_u32(variants[8]), + c900: Color::from_u32(variants[9]), + a100: Color::from_u32(variants[10]), + a200: Color::from_u32(variants[11]), + a400: Color::from_u32(variants[12]), + a700: Color::from_u32(variants[13]), + } + } +} + +impl NonAccentedPalette { + /// Create a new `NonAccented` from the given variants + /// + /// The variants should be in the format [0x00RRGGBB, ...] + pub const fn from_variants(variants: [u32; 10]) -> Self { + Self { + c50: Color::from_u32(variants[0]), + c100: Color::from_u32(variants[1]), + c200: Color::from_u32(variants[2]), + c300: Color::from_u32(variants[3]), + c400: Color::from_u32(variants[4]), + c500: Color::from_u32(variants[5]), + c600: Color::from_u32(variants[6]), + c700: Color::from_u32(variants[7]), + c800: Color::from_u32(variants[8]), + c900: Color::from_u32(variants[9]), + } + } +} + +// Accented palettes + +pub const RED: AccentedPalette = AccentedPalette::from_variants(variants::RED); +pub const PINK: AccentedPalette = AccentedPalette::from_variants(variants::PINK); +pub const PURPLE: AccentedPalette = AccentedPalette::from_variants(variants::PURPLE); +pub const DEEP_PURPLE: AccentedPalette = AccentedPalette::from_variants(variants::DEEP_PURPLE); +pub const INDIGO: AccentedPalette = AccentedPalette::from_variants(variants::INDIGO); +pub const BLUE: AccentedPalette = AccentedPalette::from_variants(variants::BLUE); +pub const LIGHT_BLUE: AccentedPalette = AccentedPalette::from_variants(variants::LIGHT_BLUE); +pub const CYAN: AccentedPalette = AccentedPalette::from_variants(variants::CYAN); +pub const TEAL: AccentedPalette = AccentedPalette::from_variants(variants::TEAL); +pub const GREEN: AccentedPalette = AccentedPalette::from_variants(variants::GREEN); +pub const LIGHT_GREEN: AccentedPalette = AccentedPalette::from_variants(variants::LIGHT_GREEN); +pub const LIME: AccentedPalette = AccentedPalette::from_variants(variants::LIME); +pub const YELLOW: AccentedPalette = AccentedPalette::from_variants(variants::YELLOW); +pub const AMBER: AccentedPalette = AccentedPalette::from_variants(variants::AMBER); +pub const ORANGE: AccentedPalette = AccentedPalette::from_variants(variants::ORANGE); +pub const DEEP_ORANGE: AccentedPalette = AccentedPalette::from_variants(variants::DEEP_ORANGE); + +// Unaccented palettes +pub const BROWN: NonAccentedPalette = NonAccentedPalette::from_variants(variants::BROWN); +pub const GRAY: NonAccentedPalette = NonAccentedPalette::from_variants(variants::GRAY); +pub const BLUE_GRAY: NonAccentedPalette = NonAccentedPalette::from_variants(variants::BLUE_GRAY); + +// Black and white included for completeness +pub const BLACK: Color = Color::from_u32(0x000000); +pub const WHITE: Color = Color::from_u32(0xFFFFFF); + +mod variants { + pub const RED: [u32; 14] = [ + 0xFFEBEE, 0xFFCDD2, 0xEF9A9A, 0xE57373, 0xEF5350, 0xF44336, 0xE53935, 0xD32F2F, 0xC62828, + 0xB71C1C, 0xFF8A80, 0xFF5252, 0xFF1744, 0xD50000, + ]; + pub const PINK: [u32; 14] = [ + 0xFCE4EC, 0xF8BBD0, 0xF48FB1, 0xF06292, 0xEC407A, 0xE91E63, 0xD81B60, 0xC2185B, 0xAD1457, + 0x880E4F, 0xFF80AB, 0xFF4081, 0xF50057, 0xC51162, + ]; + pub const PURPLE: [u32; 14] = [ + 0xF3E5F5, 0xE1BEE7, 0xCE93D8, 0xBA68C8, 0xAB47BC, 0x9C27B0, 0x8E24AA, 0x7B1FA2, 0x6A1B9A, + 0x4A148C, 0xEA80FC, 0xE040FB, 0xD500F9, 0xAA00FF, + ]; + pub const DEEP_PURPLE: [u32; 14] = [ + 0xEDE7F6, 0xD1C4E9, 0xB39DDB, 0x9575CD, 0x7E57C2, 0x673AB7, 0x5E35B1, 0x512DA8, 0x4527A0, + 0x311B92, 0xB388FF, 0x7C4DFF, 0x651FFF, 0x6200EA, + ]; + pub const INDIGO: [u32; 14] = [ + 0xE8EAF6, 0xC5CAE9, 0x9FA8DA, 0x7986CB, 0x5C6BC0, 0x3F51B5, 0x3949AB, 0x303F9F, 0x283593, + 0x1A237E, 0x8C9EFF, 0x536DFE, 0x3D5AFE, 0x304FFE, + ]; + pub const BLUE: [u32; 14] = [ + 0xE3F2FD, 0xBBDEFB, 0x90CAF9, 0x64B5F6, 0x42A5F5, 0x2196F3, 0x1E88E5, 0x1976D2, 0x1565C0, + 0x0D47A1, 0x82B1FF, 0x448AFF, 0x2979FF, 0x2962FF, + ]; + pub const LIGHT_BLUE: [u32; 14] = [ + 0xE1F5FE, 0xB3E5FC, 0x81D4FA, 0x4FC3F7, 0x29B6F6, 0x03A9F4, 0x039BE5, 0x0288D1, 0x0277BD, + 0x01579B, 0x80D8FF, 0x40C4FF, 0x00B0FF, 0x0091EA, + ]; + pub const CYAN: [u32; 14] = [ + 0xE0F7FA, 0xB2EBF2, 0x80DEEA, 0x4DD0E1, 0x26C6DA, 0x00BCD4, 0x00ACC1, 0x0097A7, 0x00838F, + 0x006064, 0x84FFFF, 0x18FFFF, 0x00E5FF, 0x00B8D4, + ]; + pub const TEAL: [u32; 14] = [ + 0xE0F2F1, 0xB2DFDB, 0x80CBC4, 0x4DB6AC, 0x26A69A, 0x009688, 0x00897B, 0x00796B, 0x00695C, + 0x004D40, 0xA7FFEB, 0x64FFDA, 0x1DE9B6, 0x00BFA5, + ]; + pub const GREEN: [u32; 14] = [ + 0xE8F5E9, 0xC8E6C9, 0xA5D6A7, 0x81C784, 0x66BB6A, 0x4CAF50, 0x43A047, 0x388E3C, 0x2E7D32, + 0x1B5E20, 0xB9F6CA, 0x69F0AE, 0x00E676, 0x00C853, + ]; + pub const LIGHT_GREEN: [u32; 14] = [ + 0xF1F8E9, 0xDCEDC8, 0xC5E1A5, 0xAED581, 0x9CCC65, 0x8BC34A, 0x7CB342, 0x689F38, 0x558B2F, + 0x33691E, 0xCCFF90, 0xB2FF59, 0x76FF03, 0x64DD17, + ]; + pub const LIME: [u32; 14] = [ + 0xF9FBE7, 0xF0F4C3, 0xE6EE9C, 0xDCE775, 0xD4E157, 0xCDDC39, 0xC0CA33, 0xAFB42B, 0x9E9D24, + 0x827717, 0xF4FF81, 0xEEFF41, 0xC6FF00, 0xAEEA00, + ]; + pub const YELLOW: [u32; 14] = [ + 0xFFFDE7, 0xFFF9C4, 0xFFF59D, 0xFFF176, 0xFFEE58, 0xFFEB3B, 0xFDD835, 0xFBC02D, 0xF9A825, + 0xF57F17, 0xFFFF8D, 0xFFFF00, 0xFFEA00, 0xFFD600, + ]; + pub const AMBER: [u32; 14] = [ + 0xFFF8E1, 0xFFECB3, 0xFFE082, 0xFFD54F, 0xFFCA28, 0xFFC107, 0xFFB300, 0xFFA000, 0xFF8F00, + 0xFF6F00, 0xFFE57F, 0xFFD740, 0xFFC400, 0xFFAB00, + ]; + pub const ORANGE: [u32; 14] = [ + 0xFFF3E0, 0xFFE0B2, 0xFFCC80, 0xFFB74D, 0xFFA726, 0xFF9800, 0xFB8C00, 0xF57C00, 0xEF6C00, + 0xE65100, 0xFFD180, 0xFFAB40, 0xFF9100, 0xFF6D00, + ]; + pub const DEEP_ORANGE: [u32; 14] = [ + 0xFBE9E7, 0xFFCCBC, 0xFFAB91, 0xFF8A65, 0xFF7043, 0xFF5722, 0xF4511E, 0xE64A19, 0xD84315, + 0xBF360C, 0xFF9E80, 0xFF6E40, 0xFF3D00, 0xDD2C00, + ]; + pub const BROWN: [u32; 10] = [ + 0xEFEBE9, 0xD7CCC8, 0xBCAAA4, 0xA1887F, 0x8D6E63, 0x795548, 0x6D4C41, 0x5D4037, 0x4E342E, + 0x3E2723, + ]; + pub const GRAY: [u32; 10] = [ + 0xFAFAFA, 0xF5F5F5, 0xEEEEEE, 0xE0E0E0, 0xBDBDBD, 0x9E9E9E, 0x757575, 0x616161, 0x424242, + 0x212121, + ]; + pub const BLUE_GRAY: [u32; 10] = [ + 0xECEFF1, 0xCFD8DC, 0xB0BEC5, 0x90A4AE, 0x78909C, 0x607D8B, 0x546E7A, 0x455A64, 0x37474F, + 0x263238, + ]; +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs b/vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs new file mode 100644 index 00000000000..926d6316a9f --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/style/palette/tailwind.rs @@ -0,0 +1,653 @@ +//! Represents the Tailwind CSS [default color palette][palette]. +//! +//! [palette]: https://tailwindcss.com/docs/customizing-colors#default-color-palette +//! +//! There are 22 palettes. Each palette has 11 colors, with variants from 50 to 950. Black and White +//! are also included for completeness and to avoid being affected by any terminal theme that might +//! be in use. +//! +//! +//!
+//!
+//!
+//!
+//!
C50
C100
C200
C300
C400
+//!
C500
C600
C700
C800
C900
+//!
C950
+//!
+//!
+//!
+//! +//! [`SLATE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GRAY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`ZINC`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`NEUTRAL`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`STONE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`RED`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`ORANGE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`AMBER`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`YELLOW`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`LIME`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`GREEN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`EMERALD`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`TEAL`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`CYAN`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`SKY`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLUE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`INDIGO`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`VIOLET`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PURPLE`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`FUCHSIA`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`PINK`]
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//!
+//! +//! [`BLACK`]
+//!
+//!
+//!
+//!
+//! +//! [`WHITE`]
+//!
+//!
+//!
+//!
+//! +//! # Example +//! +//! ```rust +//! use ratatui_core::style::Color; +//! use ratatui_core::style::palette::tailwind::{BLUE, RED}; +//! +//! assert_eq!(RED.c500, Color::Rgb(239, 68, 68)); +//! assert_eq!(BLUE.c500, Color::Rgb(59, 130, 246)); +//! ``` + +use crate::style::Color; + +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Palette { + pub c50: Color, + pub c100: Color, + pub c200: Color, + pub c300: Color, + pub c400: Color, + pub c500: Color, + pub c600: Color, + pub c700: Color, + pub c800: Color, + pub c900: Color, + pub c950: Color, +} + +#[rustfmt::skip] +///
+pub const BLACK: Color = Color::from_u32(0x000000); + +#[rustfmt::skip] +///
+pub const WHITE: Color = Color::from_u32(0xffffff); + +#[rustfmt::skip] +///
+pub const SLATE: Palette = Palette { + c50: Color::from_u32(0xf8fafc), + c100: Color::from_u32(0xf1f5f9), + c200: Color::from_u32(0xe2e8f0), + c300: Color::from_u32(0xcbd5e1), + c400: Color::from_u32(0x94a3b8), + c500: Color::from_u32(0x64748b), + c600: Color::from_u32(0x475569), + c700: Color::from_u32(0x334155), + c800: Color::from_u32(0x1e293b), + c900: Color::from_u32(0x0f172a), + c950: Color::from_u32(0x020617), +}; + +#[rustfmt::skip] +///
+pub const GRAY: Palette = Palette { + c50: Color::from_u32(0xf9fafb), + c100: Color::from_u32(0xf3f4f6), + c200: Color::from_u32(0xe5e7eb), + c300: Color::from_u32(0xd1d5db), + c400: Color::from_u32(0x9ca3af), + c500: Color::from_u32(0x6b7280), + c600: Color::from_u32(0x4b5563), + c700: Color::from_u32(0x374151), + c800: Color::from_u32(0x1f2937), + c900: Color::from_u32(0x111827), + c950: Color::from_u32(0x030712), +}; + +#[rustfmt::skip] +///
+pub const ZINC: Palette = Palette { + c50: Color::from_u32(0xfafafa), + c100: Color::from_u32(0xf4f4f5), + c200: Color::from_u32(0xe4e4e7), + c300: Color::from_u32(0xd4d4d8), + c400: Color::from_u32(0xa1a1aa), + c500: Color::from_u32(0x71717a), + c600: Color::from_u32(0x52525b), + c700: Color::from_u32(0x3f3f46), + c800: Color::from_u32(0x27272a), + c900: Color::from_u32(0x18181b), + c950: Color::from_u32(0x09090b), +}; + +#[rustfmt::skip] +///
+pub const NEUTRAL: Palette = Palette { + c50: Color::from_u32(0xfafafa), + c100: Color::from_u32(0xf5f5f5), + c200: Color::from_u32(0xe5e5e5), + c300: Color::from_u32(0xd4d4d4), + c400: Color::from_u32(0xa3a3a3), + c500: Color::from_u32(0x737373), + c600: Color::from_u32(0x525252), + c700: Color::from_u32(0x404040), + c800: Color::from_u32(0x262626), + c900: Color::from_u32(0x171717), + c950: Color::from_u32(0x0a0a0a), +}; + +#[rustfmt::skip] +///
+pub const STONE: Palette = Palette { + c50: Color::from_u32(0xfafaf9), + c100: Color::from_u32(0xf5f5f4), + c200: Color::from_u32(0xe7e5e4), + c300: Color::from_u32(0xd6d3d1), + c400: Color::from_u32(0xa8a29e), + c500: Color::from_u32(0x78716c), + c600: Color::from_u32(0x57534e), + c700: Color::from_u32(0x44403c), + c800: Color::from_u32(0x292524), + c900: Color::from_u32(0x1c1917), + c950: Color::from_u32(0x0c0a09), +}; + +#[rustfmt::skip] +///
+pub const RED: Palette = Palette { + c50: Color::from_u32(0xfef2f2), + c100: Color::from_u32(0xfee2e2), + c200: Color::from_u32(0xfecaca), + c300: Color::from_u32(0xfca5a5), + c400: Color::from_u32(0xf87171), + c500: Color::from_u32(0xef4444), + c600: Color::from_u32(0xdc2626), + c700: Color::from_u32(0xb91c1c), + c800: Color::from_u32(0x991b1b), + c900: Color::from_u32(0x7f1d1d), + c950: Color::from_u32(0x450a0a), +}; + +#[rustfmt::skip] +///
+pub const ORANGE: Palette = Palette { + c50: Color::from_u32(0xfff7ed), + c100: Color::from_u32(0xffedd5), + c200: Color::from_u32(0xfed7aa), + c300: Color::from_u32(0xfdba74), + c400: Color::from_u32(0xfb923c), + c500: Color::from_u32(0xf97316), + c600: Color::from_u32(0xea580c), + c700: Color::from_u32(0xc2410c), + c800: Color::from_u32(0x9a3412), + c900: Color::from_u32(0x7c2d12), + c950: Color::from_u32(0x431407), +}; + +#[rustfmt::skip] +///
+pub const AMBER: Palette = Palette { + c50: Color::from_u32(0xfffbeb), + c100: Color::from_u32(0xfef3c7), + c200: Color::from_u32(0xfde68a), + c300: Color::from_u32(0xfcd34d), + c400: Color::from_u32(0xfbbf24), + c500: Color::from_u32(0xf59e0b), + c600: Color::from_u32(0xd97706), + c700: Color::from_u32(0xb45309), + c800: Color::from_u32(0x92400e), + c900: Color::from_u32(0x78350f), + c950: Color::from_u32(0x451a03), +}; + +#[rustfmt::skip] +///
+pub const YELLOW: Palette = Palette { + c50: Color::from_u32(0xfefce8), + c100: Color::from_u32(0xfef9c3), + c200: Color::from_u32(0xfef08a), + c300: Color::from_u32(0xfde047), + c400: Color::from_u32(0xfacc15), + c500: Color::from_u32(0xeab308), + c600: Color::from_u32(0xca8a04), + c700: Color::from_u32(0xa16207), + c800: Color::from_u32(0x854d0e), + c900: Color::from_u32(0x713f12), + c950: Color::from_u32(0x422006), +}; + +#[rustfmt::skip] +///
+pub const LIME: Palette = Palette { + c50: Color::from_u32(0xf7fee7), + c100: Color::from_u32(0xecfccb), + c200: Color::from_u32(0xd9f99d), + c300: Color::from_u32(0xbef264), + c400: Color::from_u32(0xa3e635), + c500: Color::from_u32(0x84cc16), + c600: Color::from_u32(0x65a30d), + c700: Color::from_u32(0x4d7c0f), + c800: Color::from_u32(0x3f6212), + c900: Color::from_u32(0x365314), + c950: Color::from_u32(0x1a2e05), +}; + +#[rustfmt::skip] +///
+pub const GREEN: Palette = Palette { + c50: Color::from_u32(0xf0fdf4), + c100: Color::from_u32(0xdcfce7), + c200: Color::from_u32(0xbbf7d0), + c300: Color::from_u32(0x86efac), + c400: Color::from_u32(0x4ade80), + c500: Color::from_u32(0x22c55e), + c600: Color::from_u32(0x16a34a), + c700: Color::from_u32(0x15803d), + c800: Color::from_u32(0x166534), + c900: Color::from_u32(0x14532d), + c950: Color::from_u32(0x052e16), +}; + +#[rustfmt::skip] +///
+pub const EMERALD: Palette = Palette { + c50: Color::from_u32(0xecfdf5), + c100: Color::from_u32(0xd1fae5), + c200: Color::from_u32(0xa7f3d0), + c300: Color::from_u32(0x6ee7b7), + c400: Color::from_u32(0x34d399), + c500: Color::from_u32(0x10b981), + c600: Color::from_u32(0x059669), + c700: Color::from_u32(0x047857), + c800: Color::from_u32(0x065f46), + c900: Color::from_u32(0x064e3b), + c950: Color::from_u32(0x022c22), +}; + +#[rustfmt::skip] +///
+pub const TEAL: Palette = Palette { + c50: Color::from_u32(0xf0fdfa), + c100: Color::from_u32(0xccfbf1), + c200: Color::from_u32(0x99f6e4), + c300: Color::from_u32(0x5eead4), + c400: Color::from_u32(0x2dd4bf), + c500: Color::from_u32(0x14b8a6), + c600: Color::from_u32(0x0d9488), + c700: Color::from_u32(0x0f766e), + c800: Color::from_u32(0x115e59), + c900: Color::from_u32(0x134e4a), + c950: Color::from_u32(0x042f2e), +}; + +#[rustfmt::skip] +///
+pub const CYAN: Palette = Palette { + c50: Color::from_u32(0xecfeff), + c100: Color::from_u32(0xcffafe), + c200: Color::from_u32(0xa5f3fc), + c300: Color::from_u32(0x67e8f9), + c400: Color::from_u32(0x22d3ee), + c500: Color::from_u32(0x06b6d4), + c600: Color::from_u32(0x0891b2), + c700: Color::from_u32(0x0e7490), + c800: Color::from_u32(0x155e75), + c900: Color::from_u32(0x164e63), + c950: Color::from_u32(0x083344), +}; + +#[rustfmt::skip] +///
+pub const SKY: Palette = Palette { + c50: Color::from_u32(0xf0f9ff), + c100: Color::from_u32(0xe0f2fe), + c200: Color::from_u32(0xbae6fd), + c300: Color::from_u32(0x7dd3fc), + c400: Color::from_u32(0x38bdf8), + c500: Color::from_u32(0x0ea5e9), + c600: Color::from_u32(0x0284c7), + c700: Color::from_u32(0x0369a1), + c800: Color::from_u32(0x075985), + c900: Color::from_u32(0x0c4a6e), + c950: Color::from_u32(0x082f49), +}; + +#[rustfmt::skip] +///
+pub const BLUE: Palette = Palette { + c50: Color::from_u32(0xeff6ff), + c100: Color::from_u32(0xdbeafe), + c200: Color::from_u32(0xbfdbfe), + c300: Color::from_u32(0x93c5fd), + c400: Color::from_u32(0x60a5fa), + c500: Color::from_u32(0x3b82f6), + c600: Color::from_u32(0x2563eb), + c700: Color::from_u32(0x1d4ed8), + c800: Color::from_u32(0x1e40af), + c900: Color::from_u32(0x1e3a8a), + c950: Color::from_u32(0x172554), +}; + +#[rustfmt::skip] +///
+pub const INDIGO: Palette = Palette { + c50: Color::from_u32(0xeef2ff), + c100: Color::from_u32(0xe0e7ff), + c200: Color::from_u32(0xc7d2fe), + c300: Color::from_u32(0xa5b4fc), + c400: Color::from_u32(0x818cf8), + c500: Color::from_u32(0x6366f1), + c600: Color::from_u32(0x4f46e5), + c700: Color::from_u32(0x4338ca), + c800: Color::from_u32(0x3730a3), + c900: Color::from_u32(0x312e81), + c950: Color::from_u32(0x1e1b4b), +}; + +#[rustfmt::skip] +///
+pub const VIOLET: Palette = Palette { + c50: Color::from_u32(0xf5f3ff), + c100: Color::from_u32(0xede9fe), + c200: Color::from_u32(0xddd6fe), + c300: Color::from_u32(0xc4b5fd), + c400: Color::from_u32(0xa78bfa), + c500: Color::from_u32(0x8b5cf6), + c600: Color::from_u32(0x7c3aed), + c700: Color::from_u32(0x6d28d9), + c800: Color::from_u32(0x5b21b6), + c900: Color::from_u32(0x4c1d95), + c950: Color::from_u32(0x2e1065), +}; + +#[rustfmt::skip] +///
+pub const PURPLE: Palette = Palette { + c50: Color::from_u32(0xfaf5ff), + c100: Color::from_u32(0xf3e8ff), + c200: Color::from_u32(0xe9d5ff), + c300: Color::from_u32(0xd8b4fe), + c400: Color::from_u32(0xc084fc), + c500: Color::from_u32(0xa855f7), + c600: Color::from_u32(0x9333ea), + c700: Color::from_u32(0x7e22ce), + c800: Color::from_u32(0x6b21a8), + c900: Color::from_u32(0x581c87), + c950: Color::from_u32(0x3b0764), +}; + +#[rustfmt::skip] +///
+pub const FUCHSIA: Palette = Palette { + c50: Color::from_u32(0xfdf4ff), + c100: Color::from_u32(0xfae8ff), + c200: Color::from_u32(0xf5d0fe), + c300: Color::from_u32(0xf0abfc), + c400: Color::from_u32(0xe879f9), + c500: Color::from_u32(0xd946ef), + c600: Color::from_u32(0xc026d3), + c700: Color::from_u32(0xa21caf), + c800: Color::from_u32(0x86198f), + c900: Color::from_u32(0x701a75), + c950: Color::from_u32(0x4a044e), +}; + +#[rustfmt::skip] +///
+pub const PINK: Palette = Palette { + c50: Color::from_u32(0xfdf2f8), + c100: Color::from_u32(0xfce7f3), + c200: Color::from_u32(0xfbcfe8), + c300: Color::from_u32(0xf9a8d4), + c400: Color::from_u32(0xf472b6), + c500: Color::from_u32(0xec4899), + c600: Color::from_u32(0xdb2777), + c700: Color::from_u32(0xbe185d), + c800: Color::from_u32(0x9d174d), + c900: Color::from_u32(0x831843), + c950: Color::from_u32(0x500724), +}; + +#[rustfmt::skip] +///
+pub const ROSE: Palette = Palette { + c50: Color::from_u32(0xfff1f2), + c100: Color::from_u32(0xffe4e6), + c200: Color::from_u32(0xfecdd3), + c300: Color::from_u32(0xfda4af), + c400: Color::from_u32(0xfb7185), + c500: Color::from_u32(0xf43f5e), + c600: Color::from_u32(0xe11d48), + c700: Color::from_u32(0xbe123c), + c800: Color::from_u32(0x9f1239), + c900: Color::from_u32(0x881337), + c950: Color::from_u32(0x4c0519), +}; diff --git a/vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs b/vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs new file mode 100644 index 00000000000..5922b38ccb6 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/style/palette_conversion.rs @@ -0,0 +1,82 @@ +//! Conversions from colors in the `palette` crate to [`Color`]. + +use ::palette::LinSrgb; +use ::palette::bool_mask::LazySelect; +use ::palette::num::{Arithmetics, MulSub, PartialCmp, Powf, Real}; +use palette::Srgb; +use palette::stimulus::IntoStimulus; + +use crate::style::Color; + +/// Convert an [`palette::Srgb`] color to a [`Color`]. +/// +/// # Examples +/// +/// ``` +/// use palette::Srgb; +/// use ratatui_core::style::Color; +/// +/// let color = Color::from(Srgb::new(1.0f32, 0.0, 0.0)); +/// assert_eq!(color, Color::Rgb(255, 0, 0)); +/// ``` +impl> From> for Color { + fn from(color: Srgb) -> Self { + let (red, green, blue) = color.into_format().into_components(); + Self::Rgb(red, green, blue) + } +} + +/// Convert a [`palette::LinSrgb`] color to a [`Color`]. +/// +/// Note: this conversion only works for floating point linear sRGB colors. If you have a linear +/// sRGB color in another format, you need to convert it to floating point first. +/// +/// # Examples +/// +/// ``` +/// use palette::LinSrgb; +/// use ratatui_core::style::Color; +/// +/// let color = Color::from(LinSrgb::new(1.0f32, 0.0, 0.0)); +/// assert_eq!(color, Color::Rgb(255, 0, 0)); +/// ``` +impl> From> for Color +where + T: Real + Powf + MulSub + Arithmetics + PartialCmp + Clone, + T::Mask: LazySelect, +{ + fn from(color: LinSrgb) -> Self { + let srgb_color = Srgb::::from_linear(color); + Self::from(srgb_color) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_srgb() { + const RED: Color = Color::Rgb(255, 0, 0); + assert_eq!(Color::from(Srgb::new(255u8, 0, 0)), RED); + assert_eq!(Color::from(Srgb::new(65535u16, 0, 0)), RED); + assert_eq!(Color::from(Srgb::new(1.0f32, 0.0, 0.0)), RED); + + assert_eq!( + Color::from(Srgb::new(0.5f32, 0.5, 0.5)), + Color::Rgb(128, 128, 128) + ); + } + + #[test] + fn from_lin_srgb() { + const RED: Color = Color::Rgb(255, 0, 0); + assert_eq!(Color::from(LinSrgb::new(1.0f32, 0.0, 0.0)), RED); + assert_eq!(Color::from(LinSrgb::new(1.0f64, 0.0, 0.0)), RED); + + assert_eq!( + Color::from(LinSrgb::new(0.5f32, 0.5, 0.5)), + Color::Rgb(188, 188, 188) + ); + } +} diff --git a/vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs b/vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs new file mode 100644 index 00000000000..ddf476c4c62 --- /dev/null +++ b/vendor/ratatui-jcode/ratatui-core/src/style/stylize.rs @@ -0,0 +1,668 @@ +use alloc::borrow::Cow; +use alloc::string::{String, ToString}; +use core::fmt; + +use crate::style::{Color, Modifier, Style}; +use crate::text::Span; + +/// A trait for objects that have a `Style`. +/// +/// This trait enables generic code to be written that can interact with any object that has a +/// `Style`. This is used by the `Stylize` trait to allow generic code to be written that can +/// interact with any object that can be styled. +pub trait Styled { + type Item; + + /// Returns the style of the object. + fn style(&self) -> Style; + + /// Sets the style of the object. + /// + /// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or + /// your own type that implements [`Into