Skip to content

ai: context compression with AI-generated summaries#45

Merged
zevorn merged 1 commit intomainfrom
feat/ai-context-compression
Mar 16, 2026
Merged

ai: context compression with AI-generated summaries#45
zevorn merged 1 commit intomainfrom
feat/ai-context-compression

Conversation

@zevorn
Copy link
Copy Markdown
Owner

@zevorn zevorn commented Mar 16, 2026

Summary

  • When channel memory approaches capacity (within 4 of max), compress oldest half into an AI-generated summary before adding new messages
  • Calls do_api_call() directly on the worker thread (no queue deadlock)
  • Summary replaces N messages with 1 tagged [Earlier conversation summary], then re-adds newer half
  • Falls back to existing drop_oldest_pair_for() on API failure
  • Compression prompt: 2-3 sentences, same language, max_tokens=256

Before: 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.
Trade-off: 1 extra API call per compression event (
$0.001).

Test plan

  • make test-unit — 25/25 passed
  • scripts/check-patch.sh --staged — 0 errors, 0 warnings
  • vexpress-a9 QEMU boot passes

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>
@zevorn zevorn merged commit 428d7dd into main Mar 16, 2026
9 checks passed
@zevorn zevorn deleted the feat/ai-context-compression branch March 16, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant