Module: TurnKit::Compaction
- Defined in:
- lib/turnkit/compaction.rb
Constant Summary collapse
- DEFAULTS =
{ "enabled" => true, "threshold" => 0.75, "context_limit" => 128_000, "reserved_tokens" => 20_000, "head_messages" => 0, "tail_messages" => 12, "tail_tokens" => 8_000, "summary_ratio" => 0.20, "min_summary_tokens" => 1_000, "max_summary_tokens" => 12_000, "tool_output_max_chars" => 2_000, "model" => nil, "client" => nil }.freeze
- KNOWN_KEYS =
DEFAULTS.keys.freeze
- COMPACTION_SYSTEM_PROMPT =
<<~TEXT.strip You are an anchored context summarization assistant for TurnKit conversations. Summarize only the conversation history you are given. Recent turns may be kept verbatim outside your summary, so focus on older context that still matters for continuing the work. If a previous summary is provided, update it by preserving still-true details, removing stale details, and merging in new facts. Produce only the requested Markdown summary. Do not answer the conversation itself. Do not mention that you are summarizing, compacting, or merging context. Write in the same language the user was using. Never include API keys, tokens, passwords, secrets, credentials, or connection strings. Replace secret values with [REDACTED]. TEXT
- SUMMARY_TEMPLATE =
<<~TEXT.strip Use this exact structure: ## Active Task - [latest unfulfilled user request, preferably verbatim] ## Goal - [what the user is trying to accomplish overall] ## Constraints & Preferences - [user/developer preferences, specs, constraints, important choices] ## Completed Actions - [completed work and outcomes] ## Active State - [current state, records/files touched, test status, running tool/turn state] ## In Progress - [work underway, or "(none)"] ## Blocked - [blockers, exact errors, missing information, or "(none)"] ## Key Decisions - [important decisions and why] ## Resolved Questions - [questions already answered] ## Pending User Asks - [unanswered or unfulfilled asks] ## Relevant Files - [file/path/resource and why it matters, or "(none)"] ## Tool Results To Remember - [important tool output summaries, or "(none)"] ## Remaining Work - [likely next work, framed as context, not instructions] ## Critical Context - [specific values, IDs, commands, errors, constraints; redact secrets] Rules: - Keep every section. - Use terse bullets. - Preserve exact file paths, commands, error strings, IDs, and important values. - Do not invent facts. - Do not include secrets. - Do not include a greeting or preamble. TEXT
Class Method Summary collapse
- .active_summaries(messages) ⇒ Object
- .append_summary(conversation, turn:, summary:, selected:, policy:, focus:, auto:, input_tokens:) ⇒ Object
- .build_prompt(previous_summary:, focus:, target_tokens:) ⇒ Object
- .compact!(conversation, agent:, turn: nil, focus: nil, auto: false, overrides: {}, force: true) ⇒ Object
- .enabled_for?(agent, overrides = {}) ⇒ Boolean
- .estimate_messages_tokens(messages) ⇒ Object
- .estimate_text_tokens(text) ⇒ Object
- .expand_tail_start_for_tool_pairs(messages, tail_start) ⇒ Object
- .generate_summary(agent:, policy:, messages:, previous_summary:, focus:, target_tokens:, fallback_model:, conversation_id:, turn_id:, turn: nil) ⇒ Object
- .maybe_compact!(turn, force: nil, focus: nil) ⇒ Object
- .normalize_config(value) ⇒ Object
- .over_threshold?(messages, policy) ⇒ Boolean
- .policy_for(agent, overrides = {}) ⇒ Object
- .project(messages) ⇒ Object
- .range_for(summary) ⇒ Object
- .sanitize_message(message, policy) ⇒ Object
- .select_messages(messages, policy) ⇒ Object
- .summary_budget(input_tokens, policy) ⇒ Object
- .tail_start_index(messages, policy) ⇒ Object
Class Method Details
.active_summaries(messages) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/turnkit/compaction.rb', line 311 def active_summaries() summaries = Array().select(&:context_summary?).sort_by { |summary| summary.sequence.to_i } active = [] summaries.reverse_each do |summary| next if active.any? { |newer| (range_for(newer)&.cover?(summary.sequence.to_i)) } active << summary end active.reverse end |
.append_summary(conversation, turn:, summary:, selected:, policy:, focus:, auto:, input_tokens:) ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/turnkit/compaction.rb', line 393 def append_summary(conversation, turn:, summary:, selected:, policy:, focus:, auto:, input_tokens:) model = policy["model"] || turn&.model || conversation.model || conversation.agent.effective_model conversation.( role: "assistant", kind: "context_summary", text: summary, turn_id: turn&.id, metadata: { "compaction" => { "auto" => auto, "focus" => focus, "replaces_from_sequence" => selected.fetch("replaces_from_sequence"), "replaces_through_sequence" => selected.fetch("replaces_through_sequence"), "tail_start_sequence" => selected["tail_start_sequence"], "summary_model" => model, "input_tokens" => input_tokens, "summary_tokens" => estimate_text_tokens(summary), "created_for_turn_id" => turn&.id, "created_at" => Clock.now.iso8601 }.compact } ) end |
.build_prompt(previous_summary:, focus:, target_tokens:) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/turnkit/compaction.rb', line 252 def build_prompt(previous_summary:, focus:, target_tokens:) parts = [] if previous_summary && !previous_summary.empty? parts << <<~TEXT.strip Update the anchored summary below using the conversation history above. Preserve still-true details, remove stale details, and merge in new facts. Remove stale details that are no longer relevant or have been superseded. <previous-summary> #{previous_summary} </previous-summary> TEXT else parts << <<~TEXT.strip Create a structured context checkpoint for the conversation history above. This summary will replace older TurnKit messages in future model prompts while the original messages remain stored durably. TEXT end if focus && !focus.to_s.strip.empty? parts << <<~TEXT.strip Focus topic: "#{focus}" Preserve extra detail related to this focus topic. Summarize unrelated context more aggressively, but do not omit constraints or active blockers that affect the current task. TEXT end parts << "Target length: approximately #{target_tokens} tokens." parts << SUMMARY_TEMPLATE parts.join("\n\n") end |
.compact!(conversation, agent:, turn: nil, focus: nil, auto: false, overrides: {}, force: true) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/turnkit/compaction.rb', line 127 def compact!(conversation, agent:, turn: nil, focus: nil, auto: false, overrides: {}, force: true) policy = policy_for(agent, overrides) raise CompactionError, "compaction is disabled" unless policy["enabled"] = turn ? conversation.(turn) : conversation. projected = project() selected = (projected, policy) return nil if selected.nil? && auto raise CompactionError, "not enough messages to compact" unless selected selected_tokens = (selected.fetch("middle")) return nil if auto && !force && !over_threshold?(projected, policy) summary = generate_summary( agent: agent, policy: policy, messages: selected.fetch("middle"), previous_summary: selected["previous_summary"]&.text, focus: focus, target_tokens: summary_budget(selected_tokens, policy), fallback_model: turn&.model || conversation.model || agent.effective_model, conversation_id: conversation.id, turn_id: turn&.id, turn: turn ) append_summary(conversation, turn: turn, summary: summary, selected: selected, policy: policy, focus: focus, auto: auto, input_tokens: selected_tokens) rescue CompactionError raise rescue BudgetError raise rescue StandardError => error raise CompactionError, "#{error.class}: #{error.}" end |
.enabled_for?(agent, overrides = {}) ⇒ Boolean
93 94 95 |
# File 'lib/turnkit/compaction.rb', line 93 def enabled_for?(agent, overrides = {}) policy_for(agent, overrides)["enabled"] end |
.estimate_messages_tokens(messages) ⇒ Object
202 203 204 |
# File 'lib/turnkit/compaction.rb', line 202 def () Array().sum { || estimate_text_tokens(.text) + 8 } end |
.estimate_text_tokens(text) ⇒ Object
206 207 208 |
# File 'lib/turnkit/compaction.rb', line 206 def estimate_text_tokens(text) (text.to_s.length / 4.0).ceil end |
.expand_tail_start_for_tool_pairs(messages, tail_start) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/turnkit/compaction.rb', line 344 def (, tail_start) index = tail_start while index.positive? && [index]&.tool_result? call_id = [index].["tool_call_id"] call_index = (index - 1).downto(0).find do |i| [i].tool_call? && Array([i].["tool_calls"]).any? { |call| call["id"] == call_id || call[:id] == call_id } end break unless call_index index = call_index end index end |
.generate_summary(agent:, policy:, messages:, previous_summary:, focus:, target_tokens:, fallback_model:, conversation_id:, turn_id:, turn: nil) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/turnkit/compaction.rb', line 358 def generate_summary(agent:, policy:, messages:, previous_summary:, focus:, target_tokens:, fallback_model:, conversation_id:, turn_id:, turn: nil) client = policy["client"] || agent.effective_client model = policy["model"] || fallback_model = .map { || (, policy) } prompt = build_prompt(previous_summary: previous_summary, focus: focus, target_tokens: target_tokens) attrs = { model: model, messages: MessageProjection.for() + [ { role: :user, content: prompt } ], tools: [], instructions: COMPACTION_SYSTEM_PROMPT, metadata: { compaction: true, conversation_id: conversation_id, turn_id: turn_id } } result = if turn turn.internal_model_call(**attrs, purpose: "compaction", client: policy["client"]) else client.validate!(model: model) client.chat(**attrs) end text = result.text.to_s.strip raise CompactionError, "compaction model returned an empty summary" if text.empty? text end |
.maybe_compact!(turn, force: nil, focus: nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/turnkit/compaction.rb', line 109 def maybe_compact!(turn, force: nil, focus: nil) return if turn.compact == false force = turn.compact == true if force.nil? policy = policy_for(turn.agent) return unless policy["enabled"] = project(turn.conversation.(turn)) return unless force || over_threshold?(, policy) compact!(turn.conversation, agent: turn.agent, turn: turn, focus: focus, auto: true, overrides: policy, force: true) rescue BudgetError raise rescue StandardError => error TurnKit.logger&.warn("TurnKit compaction failed: #{error.class}: #{error.}") nil end |
.normalize_config(value) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/turnkit/compaction.rb', line 285 def normalize_config(value) case value when nil, true nil when false false when Hash attrs = value.transform_keys(&:to_s) unknown = attrs.keys - KNOWN_KEYS raise ConfigError, "unknown compaction options: #{unknown.join(", ")}" if unknown.any? attrs else raise ConfigError, "compaction must be true, false, nil, or a Hash" end end |
.over_threshold?(messages, policy) ⇒ Boolean
216 217 218 219 |
# File 'lib/turnkit/compaction.rb', line 216 def over_threshold?(, policy) usable = [ policy["context_limit"].to_i - policy["reserved_tokens"].to_i, 1 ].max () >= (usable * policy["threshold"].to_f) end |
.policy_for(agent, overrides = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/turnkit/compaction.rb', line 97 def policy_for(agent, overrides = {}) global = normalize_config(TurnKit.compaction) local = normalize_config(agent.compaction) override = normalize_config(overrides) return DEFAULTS.merge("enabled" => false) if global == false return DEFAULTS.merge("enabled" => false) if local == false return DEFAULTS.merge("enabled" => false) if override == false DEFAULTS.merge(global || {}).merge(local || {}).merge(override || {}) end |
.project(messages) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/turnkit/compaction.rb', line 162 def project() rows = Array().sort_by { || [ .sequence.to_i, .id ] } summaries = active_summaries(rows) ranges = summaries.filter_map { |summary| range_for(summary) } summaries_by_id = summaries.to_h { |summary| [ summary.id, summary ] } inserted = {} projected = [] rows.each do || summaries.each do |summary| range = range_for(summary) next unless range next if inserted[summary.id] next unless range.begin <= .sequence.to_i projected << summary inserted[summary.id] = true end if .context_summary? projected << if summaries_by_id[.id] && !inserted[.id] && !range_for() inserted[.id] = true if summaries_by_id[.id] next end next if ranges.any? { |range| range.cover?(.sequence.to_i) } projected << end summaries.each do |summary| next if inserted[summary.id] projected << summary inserted[summary.id] = true end projected end |
.range_for(summary) ⇒ Object
302 303 304 305 306 307 308 309 |
# File 'lib/turnkit/compaction.rb', line 302 def range_for(summary) = summary. from = ["replaces_from_sequence"] through = ["replaces_through_sequence"] return nil unless from && through (from.to_i..through.to_i) end |
.sanitize_message(message, policy) ⇒ Object
382 383 384 385 386 387 388 389 390 391 |
# File 'lib/turnkit/compaction.rb', line 382 def (, policy) return unless .tool_result? max = policy["tool_output_max_chars"].to_i return if max <= 0 || .text.length <= max attrs = .to_h text = "#{.text[0, max]}\n\n[Tool result truncated for compaction]" Message.new(attrs.merge("text" => text, "content" => [ { "type" => "text", "text" => text } ])) end |
.select_messages(messages, policy) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/turnkit/compaction.rb', line 221 def (, policy) rows = Array() return nil if rows.length <= policy["head_messages"].to_i + 1 previous_summary = rows.reverse.find(&:context_summary?) candidates = rows.reject(&:context_summary?) return nil if candidates.length <= policy["head_messages"].to_i + 1 head_count = policy["head_messages"].to_i tail_start = tail_start_index(candidates, policy) tail_start = [ tail_start, head_count ].max tail_start = (candidates, tail_start) middle = candidates[head_count...tail_start] return nil if middle.nil? || middle.empty? from_sequence = middle.first.sequence.to_i through_sequence = middle.last.sequence.to_i if previous_summary from_sequence = [ from_sequence, previous_summary.sequence.to_i ].min through_sequence = [ through_sequence, previous_summary.sequence.to_i ].max end { "middle" => middle, "previous_summary" => previous_summary, "replaces_from_sequence" => from_sequence, "replaces_through_sequence" => through_sequence, "tail_start_sequence" => candidates[tail_start]&.sequence } end |
.summary_budget(input_tokens, policy) ⇒ Object
210 211 212 213 214 |
# File 'lib/turnkit/compaction.rb', line 210 def summary_budget(input_tokens, policy) budget = (input_tokens.to_i * policy["summary_ratio"].to_f).ceil budget = [ budget, policy["min_summary_tokens"].to_i ].max [ budget, policy["max_summary_tokens"].to_i ].min end |
.tail_start_index(messages, policy) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/turnkit/compaction.rb', line 324 def tail_start_index(, policy) = policy["tail_messages"].to_i max_tokens = policy["tail_tokens"].to_i count = 0 tokens = 0 index = .length (.length - 1).downto(0) do |i| = estimate_text_tokens([i].text) + 8 break if count >= break if count.positive? && tokens + > max_tokens count += 1 tokens += index = i end index end |