Skip to content

feat(compaction): support custom /compact instructions and ratio-based auto-compaction#1300

Merged
RealKai42 merged 4 commits intomainfrom
kaiyi/compact-with-args
Mar 2, 2026
Merged

feat(compaction): support custom /compact instructions and ratio-based auto-compaction#1300
RealKai42 merged 4 commits intomainfrom
kaiyi/compact-with-args

Conversation

@RealKai42
Copy link
Collaborator

@RealKai42 RealKai42 commented Mar 2, 2026

Summary

  • Add loop_control.compaction_trigger_ratio (default 0.85, range 0.5-0.99) to control when auto-compaction starts.
  • Update auto-compaction trigger logic to compact when either:
    • context usage reaches the configured ratio, or
    • token_count + reserved_context_size >= max_context_size.
  • Extend /compact to accept optional custom instructions (for example: /compact keep db discussions) and pass them into compaction prompts.

Tests

  • Add config tests for compaction_trigger_ratio default, valid values, and boundary validation.
  • Add compaction tests for custom instruction prompt behavior.
  • Add trigger logic tests for should_auto_compact across different context sizes and ratios.
  • Update wire protocol e2e snapshots for the new /compact command description.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

Open with Devin

Copilot AI review requested due to automatic review settings March 2, 2026 07:35
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_ratio config (default 0.85) and use it in auto-compaction trigger logic.
  • Extend /compact to 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.

Comment on lines +69 to +72
return (
token_count >= max_context_size * trigger_ratio
or token_count + reserved_context_size >= max_context_size
)
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines 83 to 88
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."""


Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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."""

Copilot uses AI. Check for mistakes.
Comment on lines +98 to +102
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


Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +190 to +195
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
)
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 61
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."))
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

/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.

Copilot uses AI. Check for mistakes.
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