fix: incorrect message count on compacted message
Nicolas Leão committed
Mar 26, 2026 at 12:01 UTC
fdb5cf31c6681620233d5c6f2a66947f238f95c5
1 file changed
+7
-5
plugins/_chat_compaction/helpers/compactor.py
+7
-5
@@ -51,11 +51,13 @@ async def run_compaction(context, use_chat_model: bool = True) -> None:
51
# Leave some buffer for the prompt and response
52
max_input_tokens = int(ctx_length * 0.7)
53
54
- # Step 3: Create progress log item
54
+ # Step 3: Create progress log item (count user-visible messages only)
55
+ visible_types = {"user", "response"}
56
+ visible_count = sum(1 for item in context.log.logs if item.type in visible_types)
57
log_item = context.log.log(
58
type="info",
59
heading="Compacting chat history...",
58
- content=f"Analyzing {len(agent.history.current.messages)} messages (~{token_count} tokens)...",
60
+ content=f"Analyzing {visible_count} messages (~{token_count} tokens)...",
61
)
62
63
# Step 4: Handle large histories by chunking if necessary
@@ -76,7 +78,7 @@ async def run_compaction(context, use_chat_model: bool = True) -> None:
78
agent.history = History(agent=agent)
79
agent.history.add_message(
80
ai=True,
79
- content=f"# Chat Compacted\n\n{summary}"
81
+ content=f"## Context compacted\n\n{summary}"
82
)
83
84
# Clear subordinate chain
@@ -87,8 +89,8 @@ async def run_compaction(context, use_chat_model: bool = True) -> None:
89
context.log.reset()
90
context.log.log(
91
type="response",
90
- heading="Chat Compacted",
91
- content=summary,
92
+ heading="Context compacted",
93
+ content=f"## Context compacted\n\n{summary}",
94
update_progress="none",
95
)
96