ai: context compression with AI-generated summaries#45
Merged
Conversation
When a channel's conversation memory approaches capacity (within 4 of max), generate an AI summary of the oldest half of messages before adding the new one. This preserves key context that would otherwise be lost by the existing FIFO drop_oldest_pair_for(). Flow (runs on AI worker thread, no queue deadlock): 1. Build text from oldest half of channel messages 2. Call do_api_call() directly with a summarization prompt 3. Clear channel, prepend "[Earlier conversation summary] ..." 4. Re-add the newer half of messages 5. On API failure, fall back to existing FIFO drop behavior The compression prompt asks the LLM to summarize in 2-3 sentences in the same language as the conversation, with max_tokens=256. Memory cost: ~200 bytes (prompt template constant). Net memory is neutral or positive since N messages are replaced by 1 summary. Trade-off: one extra API call per compression event. Signed-off-by: Chao Liu <chao.liu.zevorn@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
do_api_call()directly on the worker thread (no queue deadlock)[Earlier conversation summary], then re-adds newer halfdrop_oldest_pair_for()on API failureBefore: oldest messages silently deleted, key context lost.
After: AI generates a concise summary preserving facts and decisions.
Memory:
200B (prompt constant). Net neutral — N messages replaced by 1.$0.001).Trade-off: 1 extra API call per compression event (
Test plan
make test-unit— 25/25 passedscripts/check-patch.sh --staged— 0 errors, 0 warnings