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.

feat(agent): read the compaction ratio per provider route

thinhlpg53cffeef2963
diff --git a/src-tauri/src/core/agent/global_config.rs b/src-tauri/src/core/agent/global_config.rs
index 8f8b0d16b..30460b5e7 100644
--- a/src-tauri/src/core/agent/global_config.rs
+++ b/src-tauri/src/core/agent/global_config.rs
@@ -229,6 +229,10 @@ pub(crate) fn load_global_config() -> Result<HashMap<String, ProviderConfig>, St
custom_headers: Vec::new(),
models: entry.models,
api_type: entry.api_type,
+ // `~/.jan/config.toml` describes the desktop app's
+ // providers, which size compaction from the project's
+ // `[provider]`/`[agent]` sections instead.
+ compaction_ratio: None,
},
)
})
diff --git a/src-tauri/src/core/agent/project.rs b/src-tauri/src/core/agent/project.rs
index 9edc339e6..d14d37fb9 100644
--- a/src-tauri/src/core/agent/project.rs
+++ b/src-tauri/src/core/agent/project.rs
@@ -54,6 +54,12 @@ pub(crate) struct ProviderSection {
pub models: Vec<String>,
#[serde(default)]
pub api_type: Option<String>,
+ /// Share of `context_window` a prompt may fill before a run compacts ahead
+ /// of dispatching, for a request served by this provider. Overrides
+ /// `[agent].compaction_ratio`, so a route with a small window can be tuned
+ /// without loosening the setting for every other route.
+ #[serde(default)]
+ pub compaction_ratio: Option<f64>,
}
/// `[skills]` — which project skills are advertised to the model. An empty
@@ -89,11 +95,20 @@ pub(crate) struct AgentSection {
#[serde(default)]
pub context_window: Option<u64>,
/// Tokens to hold back from the context window when deciding whether to
- /// compact (defaults to 16K if unset). Compaction triggers at
- /// `context_window - compaction_reserve_tokens`. This is a compaction
- /// heuristic only — it is NOT sent to the API as `max_tokens`.
+ /// compact. Compaction triggers at `context_window - compaction_reserve_tokens`,
+ /// and setting this wins over `compaction_ratio`, so an explicit absolute
+ /// headroom is never silently relaxed. This is a compaction heuristic only
+ /// - it is NOT sent to the API as `max_tokens`.
#[serde(default)]
pub compaction_reserve_tokens: Option<u64>,
+ /// Share of `context_window` a prompt may fill before a run compacts ahead
+ /// of dispatching (defaults to 0.8 if unset). Expressed as a ratio rather
+ /// than a fixed reserve so the headroom scales with the window: 16K is 12%
+ /// of a 128K window and 1.6% of a 1M one, which is far too late on the
+ /// large window. Setting `compaction_reserve_tokens` instead pins absolute
+ /// headroom and wins over this.
+ #[serde(default)]
+ pub compaction_ratio: Option<f64>,
/// Per-request output cap forwarded to the model as the OpenAI-compatible
/// `max_tokens` field. Limits how many tokens the model may generate in a
/// single response. Omitted from the request when unset (model default).
@@ -176,7 +191,9 @@ pub(crate) struct ToolsSection {
const AGENT_TOML_TEMPLATE: &str = r#"[agent]
# model = "Jan-V4"
# context_window = 128000 # tokens; defaults to 128K if unset
-# compaction_reserve_tokens = 16384 # headroom before auto-compaction; defaults to 16K
+# compaction_ratio = 0.8 # share of context_window a prompt may fill before the run
+# # compacts ahead of the request; defaults to 0.8
+# compaction_reserve_tokens = 16384 # absolute headroom instead, in tokens; wins over compaction_ratio
# max_tokens = 4096 # cap on tokens the model generates per response (OpenAI max_tokens); omitted if unset
# max_parallel_subagents = 10 # max concurrently-running subagents per run; extra dispatches queue FIFO
# show_reasoning = false # expand reasoning in the transcript (Ctrl-O still toggles)
diff --git a/src-tauri/src/core/cli/providers.rs b/src-tauri/src/core/cli/providers.rs
index 2bd8931ea..de78a9c69 100644
--- a/src-tauri/src/core/cli/providers.rs
+++ b/src-tauri/src/core/cli/providers.rs
@@ -746,6 +746,7 @@ fn provider_config_from_section(section: ProviderSection) -> ProviderConfig {
custom_headers: Vec::new(),
models: section.models,
api_type: section.api_type,
+ compaction_ratio: section.compaction_ratio,
}
}
@@ -879,6 +880,7 @@ fn provider_from_json(p: &serde_json::Value) -> Option<ProviderConfig> {
custom_headers: Vec::new(),
models,
api_type,
+ compaction_ratio: None,
})
}
@@ -901,6 +903,7 @@ fn apply_overrides(configs: &mut HashMap<String, ProviderConfig>, overrides: &Pr
custom_headers: Vec::new(),
models: Vec::new(),
api_type: None,
+ compaction_ratio: None,
});
set_key(cfg, api_key);
}
diff --git a/src-tauri/src/core/server/remote_provider_commands.rs b/src-tauri/src/core/server/remote_provider_commands.rs
index 8f69e1061..c1d671952 100644
--- a/src-tauri/src/core/server/remote_provider_commands.rs
+++ b/src-tauri/src/core/server/remote_provider_commands.rs
@@ -73,6 +73,9 @@ pub async fn register_provider_config(
.collect(),
models: request.models, // Models will be added when they are configured
api_type: request.api_type,
+ // Registration carries no ratio: this path has no field for one, and
+ // `None` inherits `[agent].compaction_ratio` for the route.
+ compaction_ratio: None,
};
// Persist the key chain to the OS keyring so it survives webview storage
@@ -207,6 +210,7 @@ mod tests {
custom_headers: vec![],
models: vec![],
api_type: None,
+ compaction_ratio: None,
}
}
diff --git a/src-tauri/src/core/state.rs b/src-tauri/src/core/state.rs
index 17ec68925..134d7b6ac 100644
--- a/src-tauri/src/core/state.rs
+++ b/src-tauri/src/core/state.rs
@@ -42,6 +42,12 @@ pub struct ProviderConfig {
/// the provider's native API.
#[serde(default)]
pub api_type: Option<String>,
+ /// Share of the context window a prompt may fill before a run routed
+ /// through this provider compacts ahead of dispatching. Overrides
+ /// `[agent].compaction_ratio`; `None` inherits it. Sized per provider
+ /// because windows differ by an order of magnitude across them.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub compaction_ratio: Option<f64>,
}
impl ProviderConfig {