-
Notifications
You must be signed in to change notification settings - Fork 694
feat(compaction): support custom /compact instructions and ratio-based auto-compaction #1300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b9c2a49
a8c0cc7
254b8fe
f1741dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,13 +51,13 @@ async def init(soul: KimiSoul, args: str): | |
|
|
||
| @registry.command | ||
| async def compact(soul: KimiSoul, args: str): | ||
| """Compact the context""" | ||
| """Compact the context (optionally with a custom focus, e.g. /compact keep db discussions)""" | ||
| if soul.context.n_checkpoints == 0: | ||
| wire_send(TextPart(text="The context is empty.")) | ||
| return | ||
|
|
||
| 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.")) | ||
|
Comment on lines
59
to
61
|
||
| wire_send(StatusUpdate(context_usage=soul.status.context_usage)) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ def test_default_config_dump(): | |
| "max_retries_per_step": 3, | ||
| "max_ralph_iterations": 0, | ||
| "reserved_context_size": 50000, | ||
| "compaction_trigger_ratio": 0.85, | ||
| }, | ||
| "services": {"moonshot_search": None, "moonshot_fetch": None}, | ||
| "mcp": {"client": {"tool_call_timeout_ms": 60000}}, | ||
|
|
@@ -92,3 +93,23 @@ def test_load_config_max_steps_per_run(): | |
| def test_load_config_reserved_context_size_too_low(): | ||
| with pytest.raises(ConfigError, match="reserved_context_size"): | ||
| load_config_from_string('{"loop_control": {"reserved_context_size": 500}}') | ||
|
|
||
|
|
||
| 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 | ||
|
|
||
|
|
||
|
Comment on lines
+98
to
+102
|
||
| def test_load_config_compaction_trigger_ratio_default(): | ||
| config = load_config_from_string("{}") | ||
| assert config.loop_control.compaction_trigger_ratio == 0.85 | ||
|
|
||
|
|
||
| def test_load_config_compaction_trigger_ratio_too_low(): | ||
| with pytest.raises(ConfigError, match="compaction_trigger_ratio"): | ||
| load_config_from_string('{"loop_control": {"compaction_trigger_ratio": 0.3}}') | ||
|
|
||
|
|
||
| def test_load_config_compaction_trigger_ratio_too_high(): | ||
| with pytest.raises(ConfigError, match="compaction_trigger_ratio"): | ||
| load_config_from_string('{"loop_control": {"compaction_trigger_ratio": 1.0}}') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should_auto_compactcomparestoken_counttomax_context_size * trigger_ratiousing floating-point math, which can produce thresholds like170000.00000000003and 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.