fix(compact): flatten prior highlights to prevent nested compaction growth

Each compaction round was re-nesting the previous summary under
'- Previously compacted context:', causing the summary to grow by
~depth * overhead per compaction cycle. After 20+ compactions the
summary was 30+ levels deep and 500KB+.

The fix flattens prior compaction highlights directly into the top-level
list (prefixed with '-'), while keeping the current round's highlights
under '- Newly compacted context:'. This means compaction summary size
is now O(depth) rather than O(depth^2).
This commit is contained in:
Josh Bowyer 2026-05-21 09:12:36 -05:00
parent f8e1bb7262
commit 5b1375b840
1 changed files with 4 additions and 6 deletions

View File

@ -291,13 +291,11 @@ fn merge_compact_summaries(existing_summary: Option<&str>, new_summary: &str) ->
let mut lines = vec!["<summary>".to_string(), "Conversation summary:".to_string()];
// Flatten prior highlights directly — do NOT re-nest them under
// "- Previously compacted context:" or the nesting compounds with each
// compaction cycle, inflating the summary by ~depth * overhead per turn.
if !previous_highlights.is_empty() {
lines.push("- Previously compacted context:".to_string());
lines.extend(
previous_highlights
.into_iter()
.map(|line| format!(" {line}")),
);
lines.extend(previous_highlights.into_iter().map(|line| format!("- {line}")));
}
if !new_highlights.is_empty() {