Skip to main content

Module compact

Module compact 

Source
Expand description

Context compaction module.

Handles automatic context compaction when the conversation gets too long. This includes token threshold detection, summary generation, and message management.

Re-exports§

pub use crate::services::token_estimation::rough_token_count_estimation;
pub use crate::services::token_estimation::rough_token_count_estimation_for_content;
pub use crate::services::token_estimation::rough_token_count_estimation_for_message;

Modules§

compact_errors
Compact command error messages

Structs§

CompactCommand
Compact command configuration
CompactionResult
Compact result containing the new messages after compaction
FileReadState
Post-compact restore state — tracks recently accessed files for restoration
PostCompactRestore
Post-compact file restore result
TokenWarningState
Calculate token warning state Translated from: calculateTokenWarningState in autoCompact.ts

Constants§

AUTOCOMPACT_BUFFER_TOKENS
Buffer tokens for auto-compact trigger
DEFAULT_CONTEXT_WINDOW
Default context window sizes by model (in tokens)
ERROR_THRESHOLD_BUFFER_TOKENS
Buffer tokens for error threshold
MANUAL_COMPACT_BUFFER_TOKENS
Manual compact uses smaller buffer (more aggressive)
MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES
Maximum consecutive auto-compact failures before giving up
MAX_OUTPUT_TOKENS_FOR_SUMMARY
Reserve tokens for output during compaction Based on p99.99 of compact summary output
POST_COMPACT_MAX_FILES_TO_RESTORE
Post-compaction: max files to restore
POST_COMPACT_MAX_TOKENS_PER_FILE
Post-compaction: max tokens per file
POST_COMPACT_MAX_TOKENS_PER_SKILL
Post-compaction: max tokens per skill
POST_COMPACT_SKILLS_TOKEN_BUDGET
Post-compaction: skills token budget
POST_COMPACT_TOKEN_BUDGET
Post-compaction: token budget for restored files
SKILL_TRUNCATION_MARKER
SKILL_TRUNCATION_MARKER appended when a skill is truncated for post-compact restore.
WARNING_THRESHOLD_BUFFER_TOKENS
Buffer tokens for warning threshold

Functions§

calculate_token_warning_state
collect_read_tool_file_paths
Collect file paths from Read tool results in preserved messages. Returns paths that are already visible and don’t need restoration.
create_post_compact_file_attachments
Create post-compact file restore attachments.
create_post_compact_skill_attachments
Create post-compact skill restore attachments.
estimate_token_count
Estimate token count for messages (rough estimation) Uses 4 chars per token for regular text (matching original TypeScript) Uses 2 chars per token for tool results (JSON is more token-efficient) Takes optional max_output_tokens to ensure we leave room for the response
get_auto_compact_threshold
Get the auto-compact threshold (when to trigger compaction)
get_blocking_limit
Get the blocking limit (when to block further input)
get_compact_command
Get the compact command
get_compact_prompt
Get the prompt for generating conversation summary Translated from: getCompactPrompt in prompt.ts
get_context_window_for_model
Get context window size for a model
get_default_context_window
Get default context window from environment or use default
get_effective_context_window_size
Get effective context window size (total - output reserve) TS: autoCompact.ts getEffectiveContextWindowSize
should_compact
Check if conversation should be compacted
strip_images_from_messages
Strip images from messages before sending for compaction Images are replaced with [image] text markers, documents with [document] markers to prevent compaction API from hitting prompt-too-long
strip_reinjected_attachments
Strip reinjected attachments (skill_discovery/skill_listing) that will be re-injected post-compaction anyway
truncate_messages_for_summary
Truncate messages to fit within a safe token limit for summarization This is used when the conversation is too large to fit in context Skips ALL system messages (they contain huge compaction summaries) Returns (truncated_messages, estimated_tokens)
truncate_to_tokens
Truncate content to roughly max_tokens, keeping the head. rough_token_count_estimation uses ~4 chars/token, so char budget = max_tokens * 4.