feat(compact): auto-compact context window when history exceeds 75% capacity#25
Open
anfibiacreativa wants to merge 1 commit intomainfrom
Open
feat(compact): auto-compact context window when history exceeds 75% capacity#25anfibiacreativa wants to merge 1 commit intomainfrom
anfibiacreativa wants to merge 1 commit intomainfrom
Conversation
Collaborator
anfibiacreativa
commented
Apr 29, 2026
- Add compact_context tool, registered only when shouldAutoCompact is true, so the model is never prompted to compact on short conversations
- Inject compact.md skill and a MUST-compact system-prompt notice when the serialised message history exceeds 75% of the 1M-token context window
- Inline estimateTokens (3.5 chars/token heuristic) in server.ts
- Add COMPACT_THRESHOLD_OVERRIDE env var to allow dev overrides without redeployment; add src/text-modules.d.ts to type .md imports
…apacity - Add compact_context tool, registered only when shouldAutoCompact is true, so the model is never prompted to compact on short conversations - Inject compact.md skill and a MUST-compact system-prompt notice when the serialised message history exceeds 75% of the 1M-token context window - Inline estimateTokens (3.5 chars/token heuristic) in server.ts - Add COMPACT_THRESHOLD_OVERRIDE env var to allow dev overrides without redeployment; add src/text-modules.d.ts to type .md imports Made-with: Cursor
de8aa78 to
4971738
Compare
mhaack
reviewed
Apr 29, 2026
Collaborator
There was a problem hiding this comment.
Is this duplicated to the .md skill file, why twice?
|
|
||
| ## Site context | ||
| - org / site / current page path (if known) | ||
| - Purpose of the site (if known) |
Collaborator
There was a problem hiding this comment.
Should the reference to the project memory.md? This should have Purpose of the site, main URLs/areas, templates etc.
Comment on lines
+540
to
+545
| /** Claude Sonnet 4 context-window size in tokens. */ | ||
| const MODEL_CONTEXT_WINDOW = 1_000_000; | ||
| /** Auto-compact threshold: compact skill is injected above this fraction. */ | ||
| const COMPACT_THRESHOLD = 0.75; | ||
| /** Approximate characters per token for English + code. */ | ||
| const CHARS_PER_TOKEN = 3.5; |
Collaborator
There was a problem hiding this comment.
Should we put these at the top, next to the other constants
mhaack
reviewed
Apr 29, 2026
| ? envThreshold | ||
| : COMPACT_THRESHOLD; | ||
| const shouldAutoCompact = | ||
| estimateTokens(JSON.stringify(modelMessages)) > |
Collaborator
There was a problem hiding this comment.
This includes the chat message only but excludes the system prompt, skills etc. which can add a lot of tokens.
mhaack
reviewed
Apr 29, 2026
| ? buildGeneratedToolsPromptSection(generatedToolsIndex) | ||
| : ''; | ||
| const autoCompactSection = autoCompact | ||
| ? `\n\n## [AUTO-COMPACT TRIGGERED]\nThe conversation has reached ${Math.round(COMPACT_THRESHOLD * 100)}% of the model context-window capacity (${MODEL_CONTEXT_WINDOW / 1_000}K tokens). You MUST call the \`compact_context\` tool immediately, before responding to anything else, using the instructions in the **Compact Context** skill pre-loaded above.` |
Collaborator
There was a problem hiding this comment.
Should this be reached ${Math.round(effectiveThreshold * 100)}% instead of using COMPACT_THRESHOLD
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.