history-compression

Explains and tests stagewise's agent history compression pipeline including boundary selection, chained compressions, and SQLite replay harness.

6.8k|507|Updated Apr 26, 2025
One-click install
npx skills add https://github.com/stagewise-io/stagewise --skill history-compression
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: history-compression
Source: https://github.com/stagewise-io/stagewise/tree/main/.agents/skills/history-compression
Command: npx skills add https://github.com/stagewise-io/stagewise --skill history-compression

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Long agent chat histories overflow the model's context window, and debugging or tuning stagewise's history compression requires deep knowledge of its trigger logic, boundary selection, serialization format, and model cascade that is scattered across the codebase.

Core Features & Use Cases

  • Pipeline Internals Reference: Documents the trigger formula, boundary selection walk, token estimation quirks, chained compression behavior, and the three-model fallback cascade with all tuning knobs.
  • SQLite Test Harness: Replays real compression events from local stagewise SQLite databases into playground-ready bundles (system prompt, user message, serialized history, actual output) for LLM playground comparison.
  • Use Case: When a user reports context-window overflow or lossy compression, use this Skill to check the trigger formula, adjust compactionThreshold or COMPRESSION_TARGET_CHARS, and replay the failing compression against alternative models to diff output quality.

Quick Start

Ask the agent to explain why history compression did not trigger for a chat and to extract the real compression test data from the local stagewise SQLite database for replay in an LLM playground.

Frequently Asked Questions about history-compression

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How does stagewise history compression decide when to trigger?

After every agent step, compression triggers when used tokens exceed min(compactionThreshold times context window, 200k). The default threshold is 0.65, with the chat agent overriding to 0.5, and a hard cap of 200k tokens applies regardless of model context size.

How do I replay real compression events in an LLM playground?

Run npx tsx scripts/experiments/extract-compression-test-data.ts with a channel flag to read the local stagewise SQLite database. It writes per-compression bundles containing the system prompt, user message, serialized history, and actual output for direct playground comparison.

Why didn't history compression trigger for my chat?

Check whether usedTokens exceeded the trigger formula min(compactionThreshold times contextWindow, 200k), verify compactionThreshold is at least 0, and confirm the _isCompressingHistory flag is not stuck from a prior run blocking new compressions.

Which models does the compression cascade use?

The cascade tries gemini-3.1-flash-lite, then gpt-5.4-nano, then claude-haiku-4.5, each with a 30-second timeout and temperature 0.1. If all fail, it falls back to the active chat model, and if that fails the agent continues uncompressed.

How do chained compressions avoid losing earlier context?

The serializer stops at the first prior compressedHistory it finds and inlines it as a previous-chat-history block, so older raw messages are never re-serialized. The prompt then injects a ratio-based budget hint telling the model how aggressively to condense the prior briefing.

Why is my compression output too lossy or too aggressive?

Raise COMPRESSION_TARGET_CHARS above the 30k default to allow longer briefings, or lower compactionThreshold so compression triggers earlier on smaller inputs. Keep the kept budget below the trigger threshold or compression never runs.