Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: persist compaction results back to TUI session history

Akarshan Biswas5f9ca9adbd77
diff --git a/src-tauri/src/core/agent/events.rs b/src-tauri/src/core/agent/events.rs
index 566999e04..b0aeaff74 100644
--- a/src-tauri/src/core/agent/events.rs
+++ b/src-tauri/src/core/agent/events.rs
@@ -45,6 +45,12 @@ pub enum StreamEvent {
name: String,
event: Box<StreamEvent>,
},
+ /// The loop's compaction reduced the conversation while retrying a
+ /// context overflow. The client should replace its session history with
+ /// `messages` for subsequent turns.
+ MessagesUpdated {
+ messages: Vec<serde_json::Value>,
+ },
/// Terminal success: the model returned a final (tool-free) completion.
Done {
stop_reason: String,
diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rs
index 61dc65da6..2f7f0ce40 100644
--- a/src-tauri/src/core/agent/loop.rs
+++ b/src-tauri/src/core/agent/loop.rs
@@ -1003,6 +1003,10 @@ async fn run_turn_cycle(
// mid-task by a fixed turn cap.
let unlimited = max_turns == 0;
let mut turn: usize = 0;
+ // Set on any turn where compaction ran, so the final return can emit
+ // `MessagesUpdated` even when compaction happened on a prior (tool-call)
+ // turn and the final turn itself didn't need retry.
+ let mut did_compact = false;
while unlimited || turn < max_turns {
let _ = events.send(StreamEvent::Step {
index: (turn as u32) + 1,
@@ -1041,6 +1045,7 @@ async fn run_turn_cycle(
attempts + 1
);
conversation_messages = compacted;
+ did_compact = true;
keep_recent = (keep_recent / 2).max(2);
attempts += 1;
}
@@ -1054,6 +1059,11 @@ async fn run_turn_cycle(
let tool_calls = extract_tool_calls(&completion);
if tool_calls.is_empty() {
+ if did_compact {
+ let _ = events.send(StreamEvent::MessagesUpdated {
+ messages: conversation_messages.clone(),
+ });
+ }
return Ok(completion);
}
diff --git a/src-tauri/src/core/agent/subagent.rs b/src-tauri/src/core/agent/subagent.rs
index 07aaf9ed7..4e4cb95a9 100644
--- a/src-tauri/src/core/agent/subagent.rs
+++ b/src-tauri/src/core/agent/subagent.rs
@@ -355,7 +355,12 @@ fn resolve_dispatch(
/// instead, bracketed by `SubagentStart`/`SubagentEnd`.
fn forward_to_parent(ev: &crate::core::agent::events::StreamEvent) -> bool {
use crate::core::agent::events::StreamEvent;
- !matches!(ev, StreamEvent::Done { .. } | StreamEvent::Error { .. })
+ !matches!(
+ ev,
+ StreamEvent::Done { .. }
+ | StreamEvent::Error { .. }
+ | StreamEvent::MessagesUpdated { .. }
+ )
}
/// Final assistant text of a completion, or empty when the model returned none.
diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rs
index 34b164378..d2b3c271c 100644
--- a/src-tauri/src/core/cli/mod.rs
+++ b/src-tauri/src/core/cli/mod.rs
@@ -1280,6 +1280,9 @@ async fn print_event(ev: StreamEvent, registry: &PermissionRegistry) {
StreamEvent::Error { code, message } => {
eprintln!("\n\x1b[31m[error] {code}: {message}\x1b[0m")
}
+ // The non-interactive CLI doesn't persist session state, so
+ // MessagesUpdated is a no-op here.
+ StreamEvent::MessagesUpdated { .. } => {}
StreamEvent::PermissionRequest {
request_id,
tool_name,
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 59e1455a5..69d0db84c 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -1170,6 +1170,10 @@ impl App {
event,
} => self.apply_subagent_event(&run_id, &name, *event),
StreamEvent::Done { .. } | StreamEvent::Error { .. } => {}
+ StreamEvent::MessagesUpdated { messages } => {
+ self.history = messages;
+ self.persist();
+ }
}
}