feat(compaction): support custom /compact instructions and ratio-based auto-compaction#1300
feat(compaction): support custom /compact instructions and ratio-based auto-compaction#1300
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the compaction system by allowing /compact to accept user-provided focus instructions and by adding a ratio-based auto-compaction trigger configurable via loop_control.compaction_trigger_ratio.
Changes:
- Add
loop_control.compaction_trigger_ratioconfig (default0.85) and use it in auto-compaction trigger logic. - Extend
/compactto pass optional custom instructions into the compaction prompt. - Update tests/snapshots to cover new config defaults and the updated slash command description.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/kimi_cli/soul/compaction.py |
Adds should_auto_compact and threads custom_instruction through the compaction interface and prompt construction. |
src/kimi_cli/soul/kimisoul.py |
Switches auto-compaction trigger to should_auto_compact and allows compact_context(custom_instruction=...). |
src/kimi_cli/soul/slash.py |
Updates /compact to accept and forward custom focus text. |
src/kimi_cli/config.py |
Introduces compaction_trigger_ratio in loop control config. |
tests/core/test_simple_compaction.py |
Adds tests for custom compaction instruction prompt behavior and should_auto_compact. |
tests/core/test_config.py |
Adds tests for compaction_trigger_ratio default/validation and updates default config dump snapshot. |
tests_e2e/test_wire_protocol.py |
Updates wire protocol snapshots for the new /compact command description. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return ( | ||
| token_count >= max_context_size * trigger_ratio | ||
| or token_count + reserved_context_size >= max_context_size | ||
| ) |
There was a problem hiding this comment.
should_auto_compact compares token_count to max_context_size * trigger_ratio using floating-point math, which can produce thresholds like 170000.00000000003 and cause an unexpected false negative at the boundary. To make the trigger deterministic, compute an integer threshold (e.g., math.ceil(max_context_size * trigger_ratio) or equivalent integer arithmetic) before comparing.
src/kimi_cli/config.py
Outdated
| context_tokens + reserved_context_size >= max_context_size. Default is 50000.""" | ||
| compaction_trigger_ratio: float = Field(default=0.85, ge=0.5, le=0.99) | ||
| """Context usage ratio that triggers auto-compaction. Default is 0.85 (85%). | ||
| Auto-compaction triggers when context_tokens >= max_context_size * compaction_trigger_ratio.""" | ||
|
|
||
|
|
There was a problem hiding this comment.
The reserved_context_size field docstring now states auto-compaction triggers when context_tokens + reserved_context_size >= max_context_size, but the logic also includes the new ratio-based trigger. Please update this docstring (or reword both docstrings) so the config documentation matches actual behavior.
| context_tokens + reserved_context_size >= max_context_size. Default is 50000.""" | |
| compaction_trigger_ratio: float = Field(default=0.85, ge=0.5, le=0.99) | |
| """Context usage ratio that triggers auto-compaction. Default is 0.85 (85%). | |
| Auto-compaction triggers when context_tokens >= max_context_size * compaction_trigger_ratio.""" | |
| either context_tokens + reserved_context_size >= max_context_size or | |
| context_tokens >= max_context_size * compaction_trigger_ratio. Default is 50000.""" | |
| compaction_trigger_ratio: float = Field(default=0.85, ge=0.5, le=0.99) | |
| """Context usage ratio threshold for auto-compaction. Default is 0.85 (85%). | |
| Auto-compaction triggers when context_tokens >= max_context_size * compaction_trigger_ratio | |
| or when context_tokens + reserved_context_size >= max_context_size.""" |
| def test_load_config_compaction_trigger_ratio(): | ||
| config = load_config_from_string('{"loop_control": {"compaction_trigger_ratio": 0.8}}') | ||
| assert config.loop_control.compaction_trigger_ratio == 0.8 | ||
|
|
||
|
|
There was a problem hiding this comment.
Config validation tests cover an in-range value and out-of-range values, but they don't exercise the documented boundaries (0.5 and 0.99). Add tests asserting that compaction_trigger_ratio=0.5 and =0.99 are accepted, and that values just outside the bounds are rejected, to prevent future regressions in the ge/le constraints.
| def test_1m_model_triggers_by_ratio(self): | ||
| """1M model with default config: ratio (85%) fires first at 850K.""" | ||
| # At 850K tokens: ratio check = 850K >= 850K (True) | ||
| assert should_auto_compact( | ||
| 850_000, 1_000_000, trigger_ratio=0.85, reserved_context_size=50_000 | ||
| ) |
There was a problem hiding this comment.
The should_auto_compact tests only cover ratios where max_context_size * trigger_ratio lands on an exact integer. Add a case with a non-integer threshold (e.g., max_context_size=999, trigger_ratio=0.85) to lock in the intended rounding behavior and catch float precision edge cases.
| logger.info("Running `/compact`") | ||
| await soul.compact_context() | ||
| await soul.compact_context(custom_instruction=args.strip()) | ||
| wire_send(TextPart(text="The context has been compacted.")) |
There was a problem hiding this comment.
/compact forwards the raw argument string into the compaction prompt. Because there’s no length/size guard, a very long custom instruction can push the compaction request over the model’s context limit and cause a hard failure (non-retryable 4xx). Consider truncating to a reasonable max length (and/or warning the user) before passing it into compact_context.
Signed-off-by: Kai <me@kaiyi.cool>
Summary
loop_control.compaction_trigger_ratio(default0.85, range0.5-0.99) to control when auto-compaction starts.token_count + reserved_context_size >= max_context_size./compactto accept optional custom instructions (for example:/compact keep db discussions) and pass them into compaction prompts.Tests
compaction_trigger_ratiodefault, valid values, and boundary validation.should_auto_compactacross different context sizes and ratios./compactcommand description.Checklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.