Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public class AutoContextConfig {
/** Compression ratio (0.0-1.0) for current round messages. Default is 0.3 (30%). */
double currentRoundCompressionRatio = 0.3;

/**
* Minimum token count required for compression to proceed.
* If the original token count is less than this value, compression will be skipped.
* Default is 5000 tokens.
*/
int minCompressionTokenThreshold = 5000;

/**
* Optional custom prompt configuration.
* If null, default prompts from {@link Prompts} will be used.
Expand Down Expand Up @@ -95,6 +102,10 @@ public double getCurrentRoundCompressionRatio() {
return currentRoundCompressionRatio;
}

public int getMinCompressionTokenThreshold() {
return minCompressionTokenThreshold;
}

/**
* Gets the custom prompt configuration.
*
Expand Down Expand Up @@ -137,6 +148,7 @@ public static class Builder {
private int lastKeep = 50;
private int minConsecutiveToolMessages = 6;
private double currentRoundCompressionRatio = 0.3;
private int minCompressionTokenThreshold = 5000;
private PromptConfig customPrompt;

/**
Expand Down Expand Up @@ -229,6 +241,19 @@ public Builder currentRoundCompressionRatio(double currentRoundCompressionRatio)
return this;
}

/**
* Sets the minimum token count required for compression to proceed.
* If the original token count is less than this value, compression will be skipped.
* Default is 5000 tokens.
*
* @param minCompressionTokenThreshold the minimum token count threshold
* @return this builder instance for method chaining
*/
public Builder minCompressionTokenThreshold(int minCompressionTokenThreshold) {
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

Missing validation for the minCompressionTokenThreshold parameter. The method should validate that the threshold is non-negative (>= 0) to prevent invalid configurations. Negative values would cause unexpected behavior in the compression logic.

Suggested change
public Builder minCompressionTokenThreshold(int minCompressionTokenThreshold) {
public Builder minCompressionTokenThreshold(int minCompressionTokenThreshold) {
if (minCompressionTokenThreshold < 0) {
throw new IllegalArgumentException("minCompressionTokenThreshold must be greater than or equal to 0");
}

Copilot uses AI. Check for mistakes.
this.minCompressionTokenThreshold = minCompressionTokenThreshold;
return this;
}

/**
* Sets custom prompt configuration.
*
Expand Down Expand Up @@ -258,6 +283,7 @@ public AutoContextConfig build() {
config.lastKeep = this.lastKeep;
config.minConsecutiveToolMessages = this.minConsecutiveToolMessages;
config.currentRoundCompressionRatio = this.currentRoundCompressionRatio;
config.minCompressionTokenThreshold = this.minCompressionTokenThreshold;
config.customPrompt = this.customPrompt;
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.agentscope.core.memory.Memory;
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.TextBlock;
import io.agentscope.core.plan.PlanNotebook;
import io.agentscope.core.tool.Toolkit;
import java.util.ArrayList;
Expand Down Expand Up @@ -225,20 +226,60 @@ private Mono<PreReasoningEvent> handlePreReasoning(PreReasoningEvent event) {
}

// Trigger compression if needed (this modifies workingMemoryStorage in place)
boolean compressed = autoContextMemory.compressIfNeeded();

if (compressed) {
// Preserve system prompt and replace memory messages with compressed ones
List<Msg> originalInputMessages = event.getInputMessages();
List<Msg> newInputMessages = new ArrayList<>();
if (!originalInputMessages.isEmpty()
&& originalInputMessages.get(0).getRole() == MsgRole.SYSTEM) {
newInputMessages.add(originalInputMessages.get(0));
}
newInputMessages.addAll(autoContextMemory.getMessages());
event.setInputMessages(newInputMessages);
autoContextMemory.compressIfNeeded();

// Always append system prompt instruction about compressed messages
List<Msg> originalInputMessages = event.getInputMessages();
List<Msg> newInputMessages = new ArrayList<>();

if (!originalInputMessages.isEmpty()
&& originalInputMessages.get(0).getRole() == MsgRole.SYSTEM) {
// Append instruction to existing system prompt
Msg originalSystemMsg = originalInputMessages.get(0);
String originalSystemText = originalSystemMsg.getTextContent();
String appendedInstruction =
"\n\n"
+ "You may see compressed messages containing <!-- CONTEXT_OFFLOAD"
+ " uuid=... -->.\n"
+ "- Use the UUID to call context_reload if you need full details.\n"
+ "- NEVER mention, quote, or refer to UUIDs, offload tags, or internal"
+ " metadata in your response.";

String newSystemText =
originalSystemText != null
? originalSystemText + appendedInstruction
: appendedInstruction.trim();

Msg updatedSystemMsg =
Msg.builder()
.role(MsgRole.SYSTEM)
.name(originalSystemMsg.getName())
.content(TextBlock.builder().text(newSystemText).build())
.metadata(originalSystemMsg.getMetadata())
.build();

newInputMessages.add(updatedSystemMsg);
} else {
// No system message exists, create a new one with the instruction
String instruction =
"You may see compressed messages containing <!-- CONTEXT_OFFLOAD uuid=..."
+ " -->.\n"
+ "- Use the UUID to call context_reload if you need full details.\n"
+ "- NEVER mention, quote, or refer to UUIDs, offload tags, or internal"
+ " metadata in your response.";
Comment on lines +240 to +269
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

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

The system prompt instruction text contains duplicate wording. Lines 244-246 say "NEVER mention, quote, or refer to UUIDs, offload tags, or internal metadata" and lines 268-269 repeat "NEVER mention, quote, or refer to UUIDs, offload tags, or internal metadata". Consider extracting this as a constant to avoid duplication and ensure consistency.

Copilot uses AI. Check for mistakes.

newInputMessages.add(
Msg.builder()
.role(MsgRole.SYSTEM)
.name("system")
.content(TextBlock.builder().text(instruction).build())
.build());
}

// Add memory messages (compressed or not)
newInputMessages.addAll(autoContextMemory.getMessages());
event.setInputMessages(newInputMessages);

return Mono.just(event);
}
}
Loading
Loading