feat(compact): add forge_compact crate with lossless sliding-window conversation compaction#3081
Open
tusharmath wants to merge 4 commits intomainfrom
Open
feat(compact): add forge_compact crate with lossless sliding-window conversation compaction#3081tusharmath wants to merge 4 commits intomainfrom
tusharmath wants to merge 4 commits intomainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a new
forge_compactcrate implementing a generic, lossless conversation compaction algorithm using a sliding window approach, and clean up the existing compaction code by removing thetoken_threshold_percentagefeature and over-defensive error handling.Context
Conversation compaction is critical for long-running agent sessions: as context grows, it must be summarised before exceeding model limits. The existing compaction logic in
forge_appwas tightly coupled to domain types and contained defensive fallbacks that masked bugs. This PR extracts the core algorithm into a standalone, fully-tested crate (forge_compact) and trims the feature surface ofCompactconfig to what is actually needed.Changes
forge_compactcrate: genericCompaction<Item>struct with acompact_conversationsliding-window algorithm that works with any type implementingContextMessageSummarytype: wraps original messages alongside their synthesised summary, enabling lossless reconstruction of compacted historycompact_conversationalgorithm: grows a window from size 1 up to the full length, triggers compaction when the caller-supplied threshold is exceeded, and restarts after each successful compaction; respectsretainto keep the most-recent messages untouchedfind_compact_range: correctly handles tool-call / tool-result pairs so they are never split across a compaction boundarytoken_threshold_percentagefromCompactconfig — the percentage-based capping logic was removed in favour of simpler absolute thresholdsCompactorinforge_app: replaced.get(start..=end)defensive fallbacks with direct slice indexing (panics are now loud, not silent bugs)token_threshold_percentagefeatureKey Implementation Details
Compaction<Item>is fully generic over anyItem: ContextMessage + Clone. The caller supplies two closures:summarize(produces a single summary item from a slice) andthreshold(decides whether the current window needs compacting). Aretaincount pins the most-recent messages outside the compaction window, preventing recent context from being summarised away.The sliding window algorithm is designed to be lossless — each
Message::Summarycarries asourcefield with the original messages, so callers can reconstruct the full history if needed.Testing
The new crate ships with a comprehensive unit test suite covering:
retainwindow pinning the tail of the conversationLinks
forge_appcompaction pipeline