Skip to content

feat(compact): auto-compact context window when history exceeds 75% capacity#25

Open
anfibiacreativa wants to merge 1 commit intomainfrom
feat/compact-context
Open

feat(compact): auto-compact context window when history exceeds 75% capacity#25
anfibiacreativa wants to merge 1 commit intomainfrom
feat/compact-context

Conversation

@anfibiacreativa
Copy link
Copy Markdown
Collaborator

  • 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

@anfibiacreativa anfibiacreativa requested a review from mhaack April 29, 2026 11:42
…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
Comment thread src/compact-skill.ts
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this duplicated to the .md skill file, why twice?

Comment thread src/compact-skill.ts

## Site context
- org / site / current page path (if known)
- Purpose of the site (if known)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the reference to the project memory.md? This should have Purpose of the site, main URLs/areas, templates etc.

Comment thread src/server.ts
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;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put these at the top, next to the other constants

Comment thread src/server.ts
? envThreshold
: COMPACT_THRESHOLD;
const shouldAutoCompact =
estimateTokens(JSON.stringify(modelMessages)) >
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This includes the chat message only but excludes the system prompt, skills etc. which can add a lot of tokens.

Comment thread src/server.ts
? 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.`
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be reached ${Math.round(effectiveThreshold * 100)}% instead of using COMPACT_THRESHOLD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants