diff --git a/crates/goose/src/agents/agent.rs b/crates/goose/src/agents/agent.rs index acfc5f576..e803e6532 100644 --- a/crates/goose/src/agents/agent.rs +++ b/crates/goose/src/agents/agent.rs @@ -35,7 +35,6 @@ use crate::agents::AgentEvent; use crate::config::extensions::name_to_key; use crate::config::permission::PermissionManager; use crate::config::{Config, GooseMode}; -use crate::context_mgmt::DEFAULT_COMPACTION_THRESHOLD; use crate::conversation::message::{ActionRequiredData, Message, MessageContent}; use crate::permission::permission_inspector::PermissionInspector; use crate::permission::{Permission, PermissionConfirmation}; @@ -885,9 +884,7 @@ impl Agent { let stop_hook_block_cap = Config::global() .get_param::("GOOSE_STOP_HOOK_BLOCK_CAP") .unwrap_or(DEFAULT_STOP_HOOK_BLOCK_CAP); - let compaction_threshold = Config::global() - .get_param::("GOOSE_AUTO_COMPACT_THRESHOLD") - .unwrap_or(DEFAULT_COMPACTION_THRESHOLD); + let compaction_threshold = crate::context_mgmt::auto_compact_threshold(context_limit); let tool_call_cutoff = Config::global() .get_param::("GOOSE_TOOL_CALL_CUTOFF") .unwrap_or_else(|_| { diff --git a/crates/goose/src/agents/state_machine/ops_compaction.rs b/crates/goose/src/agents/state_machine/ops_compaction.rs index 68a48a7bd..4b6b5bea1 100644 --- a/crates/goose/src/agents/state_machine/ops_compaction.rs +++ b/crates/goose/src/agents/state_machine/ops_compaction.rs @@ -289,11 +289,11 @@ impl Operation for CompactionOperation { conversation }; - let threshold_percentage = (self.threshold * 100.0) as u32; + let threshold_k_tokens = (self.context_limit as f64 * self.threshold / 1000.0) as usize; emit.message(Message::assistant().with_system_notification( SystemNotificationType::InlineMessage, format!( - "Exceeded auto-compact threshold of {threshold_percentage}%. \ + "Exceeded auto-compact threshold of {threshold_k_tokens}k tokens. \ Performing auto-compaction..." ), )) diff --git a/crates/goose/src/context_mgmt/mod.rs b/crates/goose/src/context_mgmt/mod.rs index a4fa3d8e2..d500dd374 100644 --- a/crates/goose/src/context_mgmt/mod.rs +++ b/crates/goose/src/context_mgmt/mod.rs @@ -22,8 +22,27 @@ use tracing::log::warn; pub use goose_context_management::DEFAULT_COMPACTION_THRESHOLD; +/// Starts compaction with headroom below OpenAI's long-context price tier +/// (above 272k input tokens). This is not a maximum request size. +pub const DEFAULT_AUTO_COMPACT_TOKEN_LIMIT: usize = 225_000; + pub(crate) const TOOLCALL_SUMMARIZATION_BATCH_SIZE: usize = 10; +/// The configured threshold, lowered so compaction starts by the token limit. +pub fn auto_compact_threshold(context_limit: usize) -> f64 { + let config = Config::global(); + let threshold = config + .get_param::("GOOSE_AUTO_COMPACT_THRESHOLD") + .unwrap_or(DEFAULT_COMPACTION_THRESHOLD); + if threshold <= 0.0 || threshold >= 1.0 || context_limit == 0 { + return threshold; + } + let token_limit = config + .get_param::("GOOSE_AUTO_COMPACT_TOKEN_LIMIT") + .unwrap_or(DEFAULT_AUTO_COMPACT_TOKEN_LIMIT); + threshold.min(token_limit as f64 / context_limit as f64) +} + pub(crate) fn tool_pair_summarization_enabled() -> bool { Config::global() .get_param::("GOOSE_TOOL_PAIR_SUMMARIZATION") diff --git a/crates/goose/tests/auto_compact_token_limit.rs b/crates/goose/tests/auto_compact_token_limit.rs new file mode 100644 index 000000000..a06ef9ffc --- /dev/null +++ b/crates/goose/tests/auto_compact_token_limit.rs @@ -0,0 +1,176 @@ +use anyhow::Result; +use async_trait::async_trait; +use futures::StreamExt; +use goose::agents::{Agent, SessionConfig}; +use goose::config::{Config, GooseMode}; +use goose::context_mgmt::auto_compact_threshold; +use goose::conversation::message::Message; +use goose::conversation::Conversation; +use goose::providers::base::{stream_from_single_message, MessageStream, Provider}; +use goose::session::SessionType; +use goose_providers::conversation::token_usage::{ProviderUsage, Usage}; +use goose_providers::errors::ProviderError; +use goose_providers::model::ModelConfig; +use rmcp::model::Tool; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + +#[derive(Default)] +struct CompactionProvider { + compacted: AtomicBool, +} + +#[async_trait] +impl Provider for CompactionProvider { + fn get_name(&self) -> &str { + "compaction-test" + } + + async fn get_context_limit(&self, model: &str, override_limit: Option) -> usize { + override_limit.unwrap_or_else(|| model.parse().unwrap()) + } + + async fn stream( + &self, + model_config: &ModelConfig, + _system: &str, + messages: &[Message], + _tools: &[Tool], + ) -> Result { + let is_compaction = messages.iter().any(|message| { + message + .as_concat_text() + .to_lowercase() + .contains("summarize") + }); + if is_compaction { + self.compacted.store(true, Ordering::SeqCst); + } + Ok(stream_from_single_message( + Message::assistant().with_text(if is_compaction { + "summary" + } else { + "continued" + }), + ProviderUsage::new( + model_config.model_name.clone(), + Usage::new(Some(100), Some(10), Some(110)), + ), + )) + } +} + +#[tokio::test] +async fn effective_trigger_and_agent_execution_follow_the_current_model() -> Result<()> { + let temp_dir = tempfile::tempdir()?; + let root = temp_dir.path().to_str().unwrap(); + let _env = env_lock::lock_env([ + ("GOOSE_PATH_ROOT", Some(root)), + ("GOOSE_DISABLE_KEYRING", Some("1")), + ("GOOSE_AUTO_COMPACT_THRESHOLD", None), + ("GOOSE_AUTO_COMPACT_TOKEN_LIMIT", None), + ("GOOSE_CONTEXT_LIMIT", None), + ("GOOSE_TOOL_PAIR_SUMMARIZATION", Some("false")), + ("GOOSE_DISABLE_SESSION_NAMING", Some("true")), + ]); + let config = Config::global(); + assert_eq!(auto_compact_threshold(1_000_000), 0.225); + assert_eq!(auto_compact_threshold(200_000), 0.8); + assert_eq!(auto_compact_threshold(0), 0.8); + + let agent = Agent::new(); + let session = agent + .config + .session_manager + .create_session( + temp_dir.path().to_path_buf(), + "compaction threshold".to_string(), + SessionType::Hidden, + GooseMode::default(), + ) + .await?; + + for (percentage, cap, context, trigger) in [ + (0.8_f64, 225_000, 1_000_000, 225_000), + (0.8, 225_000, 200_000, 160_000), + (0.8, 225_000, 1_000_000, 225_000), + (0.2, 225_000, 1_000_000, 200_000), + (0.8, 900_000, 1_000_000, 800_000), + ] { + config.set_param("GOOSE_AUTO_COMPACT_THRESHOLD", percentage)?; + config.set_param("GOOSE_AUTO_COMPACT_TOKEN_LIMIT", cap)?; + assert_eq!( + auto_compact_threshold(context), + percentage.min(cap as f64 / context as f64) + ); + for (tokens, expected) in [(trigger - 1, false), (trigger, false), (trigger + 1, true)] { + verify_reply(&agent, &session.id, context, tokens, expected).await?; + } + } + + for disabled in [0.0, -0.1, 1.0] { + config.set_param("GOOSE_AUTO_COMPACT_THRESHOLD", disabled)?; + config.set_param("GOOSE_AUTO_COMPACT_TOKEN_LIMIT", 225_000)?; + for context in [1_000_000, 200_000] { + assert_eq!(auto_compact_threshold(context), disabled); + verify_reply(&agent, &session.id, context, 900_000, false).await?; + } + } + Ok(()) +} + +async fn verify_reply( + agent: &Agent, + session_id: &str, + context: usize, + tokens: i32, + expected_compaction: bool, +) -> Result<()> { + let provider = Arc::new(CompactionProvider::default()); + agent + .update_provider( + provider.clone(), + ModelConfig::new(context.to_string()), + session_id, + ) + .await?; + agent + .config + .session_manager + .replace_conversation( + session_id, + &Conversation::new_unvalidated([ + Message::user().with_text("previous work"), + Message::assistant().with_text("previous response"), + ]), + ) + .await?; + agent + .config + .session_manager + .update(session_id) + .usage(Usage::new(Some(tokens), Some(0), Some(tokens))) + .apply() + .await?; + let stream = agent + .reply( + Message::user().with_text("continue"), + SessionConfig { + id: session_id.to_string(), + schedule_id: None, + max_turns: Some(2), + }, + None, + ) + .await?; + tokio::pin!(stream); + while let Some(event) = stream.next().await { + event?; + } + assert_eq!( + provider.compacted.load(Ordering::SeqCst), + expected_compaction, + "context={context}, tokens={tokens}" + ); + Ok(()) +} diff --git a/documentation/docs/guides/config-files.md b/documentation/docs/guides/config-files.md index 7dceacb19..6a845f26a 100644 --- a/documentation/docs/guides/config-files.md +++ b/documentation/docs/guides/config-files.md @@ -64,6 +64,7 @@ The following settings can be configured at the root level of your config.yaml f | `GOOSE_DOCS_ROOT` | Documentation root used by `goose-doc-guide` (e.g. for offline/air-gapped docs) | Local path or HTTP(S) URL containing `goose-docs-map.md` and `docs/` | `https://goose-docs.ai` | No | | `GOOSE_RECIPE_GITHUB_REPO` | GitHub repository for recipes | Format: "org/repo" | None | No | | `GOOSE_AUTO_COMPACT_THRESHOLD` | Set the percentage threshold at which goose [automatically compacts your session](/docs/guides/sessions/smart-context-management#automatic-compaction). | Float between 0.0 and 1.0 (disabled at 0.0)| 0.8 | No | +| `GOOSE_AUTO_COMPACT_TOKEN_LIMIT` | Absolute auto-compaction trigger; the earlier of this and the percentage threshold applies while auto-compaction is enabled. Controls when compaction starts, not a guaranteed maximum request size. | Positive integer | 225000 | No | | `SECURITY_PROMPT_ENABLED` | Enable [prompt injection detection](/docs/guides/security/prompt-injection-detection) to identify potentially harmful commands | true/false | false | No | | `SECURITY_PROMPT_THRESHOLD` | Sensitivity threshold for prompt injection detection (higher = stricter) | Float between 0.01 and 1.0 | 0.8 | No | | `SECURITY_PROMPT_CLASSIFIER_ENABLED` | Enable ML-based prompt injection detection for advanced threat identification | true/false | false | No | diff --git a/documentation/docs/guides/environment-variables.md b/documentation/docs/guides/environment-variables.md index 50c923a06..b3aa66f0f 100644 --- a/documentation/docs/guides/environment-variables.md +++ b/documentation/docs/guides/environment-variables.md @@ -152,6 +152,7 @@ These variables control how goose manages conversation sessions and context. | `GOOSE_TRUNCATED_SHOW_LINES` | Number of lines shown before the "... (N more lines)" message when a code block is truncated | Positive integer | 20 | | `GOOSE_NO_CODE_TRUNCATION` | Disable code block truncation entirely — all code blocks are shown in full | "1", "true" (case-insensitive) to enable | false | | `GOOSE_AUTO_COMPACT_THRESHOLD` | Set the percentage threshold at which goose [automatically compacts your session](/docs/guides/sessions/smart-context-management#automatic-compaction). | Float between 0.0 and 1.0 (disabled at 0.0) | 0.8 | +| `GOOSE_AUTO_COMPACT_TOKEN_LIMIT` | Absolute auto-compaction trigger; the earlier of this and the percentage threshold applies while auto-compaction is enabled. Controls when compaction starts, not a guaranteed maximum request size. | Positive integer | 225000 | | `GOOSE_TOOL_CALL_CUTOFF` | Number of tool calls to keep in full detail before summarizing older tool outputs to help maintain efficient context usage | Integer (e.g., 5, 10, 20) | Computed from the model context limit and auto-compaction threshold | | `GOOSE_MOIM_MESSAGE_TEXT` | Injects persistent text into goose's [working memory](/docs/guides/context-engineering/using-persistent-instructions) every turn. Useful for behavioral guardrails or persistent reminders. | Any text string | Not set | | `GOOSE_MOIM_MESSAGE_FILE` | Path to a file whose contents are injected into goose's [working memory](/docs/guides/context-engineering/using-persistent-instructions) every turn. Supports `~/`. Max 64 KB per file. | File path | Not set | diff --git a/documentation/docs/guides/sessions/smart-context-management.md b/documentation/docs/guides/sessions/smart-context-management.md index d6bc4834f..ce4623d1a 100644 --- a/documentation/docs/guides/sessions/smart-context-management.md +++ b/documentation/docs/guides/sessions/smart-context-management.md @@ -26,10 +26,13 @@ This layered approach lets goose handle token and context limits gracefully. ## Automatic Compaction goose automatically compacts older parts of your conversation into a summary when approaching token limits, allowing you to maintain long-running sessions without manual intervention. -Auto-compaction is triggered by default when you reach 80% of the token limit in goose Desktop and the goose CLI. +Auto-compaction is triggered by default when you reach 80% of the token limit or 225,000 tokens, whichever comes first, in goose Desktop and the goose CLI. +The effective trigger is `min(GOOSE_AUTO_COMPACT_THRESHOLD × model context window, GOOSE_AUTO_COMPACT_TOKEN_LIMIT)`. It is recalculated for the current model's context window. Change the token cap with `GOOSE_AUTO_COMPACT_TOKEN_LIMIT`. + +The default cap leaves headroom below OpenAI's long-context price tier (above 272,000 input tokens). It controls when compaction starts, not a guaranteed maximum request size: new messages, tool output, and the compaction request itself can exceed the trigger between checks. Control the auto-compaction behavior with the `GOOSE_AUTO_COMPACT_THRESHOLD` [environment variable](/docs/guides/environment-variables.md#session-management). -Disable this feature by setting the value to `0.0`. +Disable this feature by setting the value to `0.0`; the token cap does not re-enable disabled auto-compaction. ``` # Automatically compact sessions when 60% of available tokens are used diff --git a/goose-self-test.yaml b/goose-self-test.yaml index e9cd70cf8..3b06efbce 100644 --- a/goose-self-test.yaml +++ b/goose-self-test.yaml @@ -18,6 +18,7 @@ activities: - Test Z.AI Coding Plan streamed tool calls and interruption recovery - Validate ACP thinking-effort discovery, forwarding, and provider switching - Validate GDK observability hook lifecycle events and payload opt-in + - Validate capped and uncapped auto-compaction thresholds and Desktop editing - Test error boundaries including nested delegation prevention - Generate comprehensive test report @@ -26,7 +27,7 @@ parameters: input_type: string requirement: optional default: "all" - description: "Which test phases to run: all, basic, extensions, delegation, reasoning, zai-coding-plan, acp-effort, advanced" + description: "Which test phases to run: all, basic, extensions, delegation, reasoning, zai-coding-plan, acp-effort, compaction, advanced" - key: test_depth input_type: string @@ -194,6 +195,29 @@ prompt: | Log results to: {{ workspace_dir }}/phase1_basic_tools.md {% endif %} + {% if test_phases == "all" or "compaction" in test_phases %} + ## Auto-Compaction Threshold Validation + + **Prerequisites**: Run this phase from a goose source checkout with the Rust toolchain + and installed Desktop dependencies. Record unavailable checks as SKIPPED. + + 1. Run `cargo test -p goose --test auto_compact_token_limit`. + 2. Verify the effective trigger is the minimum of the percentage budget and token cap: + defaults produce 225,000 tokens for a 1M-token context and 160,000 for a 200,000-token + context. Verify actual agent replies compact only after crossing the trigger. + 3. Verify changing model context size in the same session recalculates the trigger and + that disabled auto-compaction remains disabled regardless of the cap. + 4. Run `cd ui/desktop && pnpm exec vitest run src/components/alerts/__tests__/AlertBox.test.tsx`. + 5. Verify the inline editor saves the displayed token cap in capped contexts and the + percentage in uncapped contexts, including cancellation and model changes during editing. + 6. Confirm the smart context management guide describes when compaction starts, rather + than promising a maximum request size. New messages, tool output, and compaction + requests can exceed the trigger between checks. + + Log command results and failures to: {{ workspace_dir }}/compaction.md + Do not modify source files or configuration to make these checks pass. + {% endif %} + {% if test_phases == "all" or "extensions" in test_phases %} ## 🔧 PHASE 2: Extension System Testing diff --git a/ui/desktop/src/components/alerts/AlertBox.tsx b/ui/desktop/src/components/alerts/AlertBox.tsx index eea78cee4..a9f5126a2 100644 --- a/ui/desktop/src/components/alerts/AlertBox.tsx +++ b/ui/desktop/src/components/alerts/AlertBox.tsx @@ -24,6 +24,14 @@ const i18n = defineMessages({ id: 'alertBox.autoCompactAt', defaultMessage: 'Auto compact at', }, + tokenLimitLabel: { + id: 'settings.agentLoop.operations.tokenLimit.label', + defaultMessage: 'Token limit', + }, + thresholdLabel: { + id: 'settings.agentLoop.operations.threshold.label', + defaultMessage: 'Threshold %', + }, compactNow: { id: 'alertBox.compactNow', defaultMessage: 'Compact now', @@ -45,16 +53,23 @@ export const AlertBox = ({ alert, className }: AlertBoxProps) => { const { read, upsert } = useConfig(); const [isEditingThreshold, setIsEditingThreshold] = useState(false); const [loadedThreshold, setLoadedThreshold] = useState(0.8); - const [thresholdValue, setThresholdValue] = useState(80); + const [editValue, setEditValue] = useState(80); + const [editingTokenLimit, setEditingTokenLimit] = useState(false); + const [tokenLimit, setTokenLimit] = useState(225000); const [isSaving, setIsSaving] = useState(false); useEffect(() => { const loadThreshold = async () => { try { - const threshold = await read('GOOSE_AUTO_COMPACT_THRESHOLD', false); + const [threshold, limit] = await Promise.all([ + read('GOOSE_AUTO_COMPACT_THRESHOLD', false), + read('GOOSE_AUTO_COMPACT_TOKEN_LIMIT', false), + ]); if (threshold !== undefined && threshold !== null && typeof threshold === 'number') { setLoadedThreshold(threshold); - setThresholdValue(Math.max(1, Math.min(99, Math.round(threshold * 100)))); + } + if (typeof limit === 'number') { + setTokenLimit(limit); } } catch (err) { console.error('Error fetching auto-compact threshold:', err); @@ -65,31 +80,39 @@ export const AlertBox = ({ alert, className }: AlertBoxProps) => { }, [read]); const currentThreshold = loadedThreshold; + const contextLimit = alert.progress?.total ?? 0; + const tokenLimitApplies = + currentThreshold > 0 && currentThreshold < 1 && tokenLimit < currentThreshold * contextLimit; + + const editMaximum = editingTokenLimit ? Number.MAX_SAFE_INTEGER : 99; + const editLabel = intl.formatMessage( + editingTokenLimit ? i18n.tokenLimitLabel : i18n.thresholdLabel + ); const handleSaveThreshold = async () => { if (isSaving) return; // Prevent double-clicks - let validThreshold = Math.max(1, Math.min(99, thresholdValue)); - if (validThreshold !== thresholdValue) { - setThresholdValue(validThreshold); - } - + const validValue = Math.max(1, Math.min(editMaximum, editValue)); + setEditValue(validValue); setIsSaving(true); try { - const newThreshold = validThreshold / 100; // Convert percentage to decimal - - await upsert('GOOSE_AUTO_COMPACT_THRESHOLD', newThreshold, false); - - setIsEditingThreshold(false); - setLoadedThreshold(newThreshold); - - // Notify parent component of the threshold change - if (alert.onThresholdChange) { - alert.onThresholdChange(newThreshold); + if (editingTokenLimit) { + await upsert('GOOSE_AUTO_COMPACT_TOKEN_LIMIT', validValue, false); + setTokenLimit(validValue); + } else { + const newThreshold = validValue / 100; + await upsert('GOOSE_AUTO_COMPACT_THRESHOLD', newThreshold, false); + setLoadedThreshold(newThreshold); + alert.onThresholdChange?.(newThreshold); } + setIsEditingThreshold(false); } catch (error) { console.error('Error saving threshold:', error); - window.alert(intl.formatMessage(i18n.failedToSaveThreshold, { error: errorMessage(error, 'Unknown error') })); + window.alert( + intl.formatMessage(i18n.failedToSaveThreshold, { + error: errorMessage(error, 'Unknown error'), + }) + ); } finally { setIsSaving(false); } @@ -111,27 +134,28 @@ export const AlertBox = ({ alert, className }: AlertBoxProps) => {
{isEditingThreshold ? ( <> - {intl.formatMessage(i18n.autoCompactAt)} + {editLabel} { const val = parseInt(e.target.value, 10); if (e.target.value === '') { - setThresholdValue(1); + setEditValue(1); } else if (!isNaN(val)) { - setThresholdValue(Math.max(1, Math.min(99, val))); + setEditValue(Math.max(1, Math.min(editMaximum, val))); } }} onBlur={(e) => { const val = parseInt(e.target.value, 10); if (isNaN(val) || val < 1) { - setThresholdValue(1); - } else if (val > 99) { - setThresholdValue(99); + setEditValue(1); + } else if (val > editMaximum) { + setEditValue(editMaximum); } }} onKeyDown={(e) => { @@ -139,8 +163,6 @@ export const AlertBox = ({ alert, className }: AlertBoxProps) => { handleSaveThreshold(); } else if (e.key === 'Escape') { setIsEditingThreshold(false); - const resetValue = Math.round(currentThreshold * 100); - setThresholdValue(Math.max(1, Math.min(99, resetValue))); } }} onFocus={(e) => { @@ -149,11 +171,10 @@ export const AlertBox = ({ alert, className }: AlertBoxProps) => { onClick={(e) => { e.stopPropagation(); }} - className="w-12 px-1 text-[10px] bg-white/10 border border-current/30 rounded outline-none text-center focus:bg-white/20 focus:border-current/50 transition-colors" + className="w-20 px-1 text-[10px] bg-white/10 border border-current/30 rounded outline-none text-center focus:bg-white/20 focus:border-current/50 transition-colors" disabled={isSaving} autoFocus /> - %