Module: RubynCode::Context::AutoCompact

Defined in:
lib/rubyn_code/context/auto_compact.rb

Overview

LLM-driven summarization triggered automatically when the context window grows too large. Serializes the conversation tail, asks the LLM to produce a continuity summary, and returns a fresh single-message conversation.

Constant Summary collapse

SUMMARY_INSTRUCTION =
"You are a context compaction assistant. Summarize the following conversation transcript for continuity. Cover exactly three areas:\n\n1) **What was accomplished** - completed tasks, files changed, problems solved\n2) **Current state** - what the user/agent is working on right now, any pending actions\n3) **Key decisions made** - architectural choices, user preferences, constraints established\n\nBe concise but preserve all details needed to continue the work seamlessly. Use bullet points.\n"
MAX_TRANSCRIPT_CHARS =
80_000

Class Method Summary collapse

Class Method Details

.call(messages, llm_client:, transcript_dir: nil) ⇒ Array<Hash>

Compacts the conversation by summarizing it through the LLM.

Parameters:

  • messages (Array<Hash>) —

    current conversation messages

  • llm_client (#chat) —

    an LLM client that responds to #chat

  • transcript_dir (String, nil) (defaults to: nil) —

    directory to save full transcript before compaction

Returns:

  • (Array<Hash>) —

    new messages array containing only the summary



30
31
32
33
34
35
36
37
# File 'lib/rubyn_code/context/auto_compact.rb', line 30

def self.call(messages, llm_client:, transcript_dir: nil)
  save_transcript(messages, transcript_dir) if transcript_dir

  transcript_text = serialize_tail(messages, MAX_TRANSCRIPT_CHARS)
  summary = request_summary(transcript_text, llm_client)

  [{ role: 'user', content: "[Context compacted]\n\n#{summary}" }]
end