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(cli): honor manual /compact on short threads

Akarshan Biswasfb93307d9130
diff --git a/src-tauri/src/core/agent/compaction.rs b/src-tauri/src/core/agent/compaction.rs
index 29e21cb7f..434bd0bc2 100644
--- a/src-tauri/src/core/agent/compaction.rs
+++ b/src-tauri/src/core/agent/compaction.rs
@@ -16,6 +16,10 @@ use crate::core::agent::upstream::extract_choice_message;
/// Default number of most-recent non-system messages kept verbatim.
pub(crate) const DEFAULT_KEEP_RECENT: usize = 8;
+/// Recent tail kept when the user explicitly runs `/compact`. Smaller than the
+/// automatic threshold so a deliberate compaction is honoured on short threads.
+pub(crate) const MANUAL_KEEP_RECENT: usize = 2;
+
const SUMMARY_SYSTEM_PROMPT: &str = "You are compacting an AI agent conversation that grew too long for \
the model's context window. Summarize the messages below into a dense, factual brief that preserves \
everything needed to continue the task: the user's goals and constraints, decisions made, files and \
diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rs
index 2f7f0ce40..79a3e2b6b 100644
--- a/src-tauri/src/core/agent/loop.rs
+++ b/src-tauri/src/core/agent/loop.rs
@@ -939,6 +939,7 @@ pub(crate) async fn compact_history(
args: &OrchestrationArgs,
model_id: &str,
messages: &[serde_json::Value],
+ keep_recent: usize,
) -> Result<Vec<serde_json::Value>, String> {
let (upstream_url, api_keys) = resolve_upstream_for_model(
model_id,
@@ -956,7 +957,7 @@ pub(crate) async fn compact_history(
messages,
model_id,
&model,
- crate::core::agent::compaction::DEFAULT_KEEP_RECENT,
+ keep_recent,
)
.await)
}
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 21c96955e..ef6a72783 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -2952,7 +2952,14 @@ async fn compact_command(app: &mut App) {
return;
};
let before = app.history.len();
- match crate::core::agent::r#loop::compact_history(&args, &app.model, &app.history).await {
+ match crate::core::agent::r#loop::compact_history(
+ &args,
+ &app.model,
+ &app.history,
+ crate::core::agent::compaction::MANUAL_KEEP_RECENT,
+ )
+ .await
+ {
Ok(compacted) if compacted.len() < before => {
app.history = compacted;
app.persist();