diff --git a/navi/workers/compressor.py b/navi/workers/compressor.py index 2ae0cc5..2ac49ec 100644 --- a/navi/workers/compressor.py +++ b/navi/workers/compressor.py @@ -3,7 +3,7 @@ import structlog from navi.config import settings -from navi.core.compressor import compress_context, should_compress +from navi.core.compressor import compress_context, should_compress, ContextCompressor from navi.core.events import ContextCompressed from navi.llm.base import Message @@ -45,13 +45,30 @@ return WorkerResult() new_context, summary_text = result - session.context = new_context - session.context_token_count = 0 # reset so next turn doesn't re-compress pre-call + + # Mark messages that are no longer part of the LLM context + new_context_ids = {id(m) for m in new_context} + for msg in session.messages: + if id(msg) not in new_context_ids and msg.role != "system": + msg.is_context = False + + # The summary returned by the compressor must also live in messages so + # save() writes it to the normalized table, but it is not displayed. + summary_msg = next((m for m in new_context if m.is_summary), None) + if summary_msg and summary_msg not in session.messages: + summary_msg.is_display = False + session.messages.append(summary_msg) + + # UI marker showing that compression happened session.messages.append(Message( role="system", is_compression=True, + is_context=False, content=summary_text, )) + + session.context = new_context + session.context_token_count = ContextCompressor.estimate_context_tokens(new_context) await ctx.session_store.save(session) log.info(