From 9068b870ba473f830b273bc16f59b26033fa3db2 Mon Sep 17 00:00:00 2001 From: Ryan Wyler Date: Tue, 25 Aug 2026 18:08:30 -0700 Subject: [PATCH] feat(core): proportional compaction with context-restoration prompt Improves compaction so long sessions stay coherent after compacting, and fixes a small-session edge case in the existing absolute-token approach. Three related changes, all opt-in (unset falls back to current behavior): - Context-restoration prompt: replaces the terse summary template with a 6-section restoration document, and feeds the newest slice of the conversation to the summarizer as a relevance signal () so the summary is weighted toward the session's current direction. - Proportional selection (extract_ratio / recent_ratio): sizes the verbatim tail as (1 - extract_ratio) of the current scoped tokens instead of an absolute budget. This fixes the case where a session smaller than preserve_recent_tokens summarizes the ENTIRE conversation and keeps nothing verbatim (select() returns keep.start === 0). Ratios scale with session size, so a small session is never fully summarized. recent_ratio sizes the relevance signal proportionally. - Proportional trigger (trigger_ratio): compacts proactively at trigger_ratio x context (a percentage of the window) instead of the fixed reserved-headroom threshold, leaving room for a rich summary at high fill. All three are token-based and computed on demand from the scoped conversation (history minus hidden prior-compaction turns). When the new keys are unset, selection and trigger behavior are unchanged. --- packages/core/src/session/compaction.ts | 91 ++++++++++----------- packages/core/src/v1/config/config.ts | 18 ++++ packages/opencode/src/session/compaction.ts | 58 ++++++++++++- packages/opencode/src/session/overflow.ts | 4 + 4 files changed, 119 insertions(+), 52 deletions(-) diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts index f714633295a5..262c33e744fe 100644 --- a/packages/core/src/session/compaction.ts +++ b/packages/core/src/session/compaction.ts @@ -13,46 +13,29 @@ const DEFAULT_BUFFER = 20_000 const DEFAULT_KEEP_TOKENS = 8_000 const TOOL_OUTPUT_MAX_CHARS = 2_000 const SUMMARY_OUTPUT_TOKENS = 4_096 -const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside