fix(agent-runtime): never wipe chat history on compaction - #1176
Open
nordicnode wants to merge 1 commit into
Open
fix(agent-runtime): never wipe chat history on compaction#1176nordicnode wants to merge 1 commit into
nordicnode wants to merge 1 commit into
Conversation
Two verified paths replaced the model's entire context with nothing: 1. /compact built its replacement summary from fullResponse without checking it was non-empty. A silent empty stop (finish part with a real reason, no content) yields no recovery chunk, so the whole history was replaced by a single message whose summary carried nothing — every earlier turn gone. Guard the replacement on fullResponse.trim() and keep the history (the forced next step retries the summary) when the model returned nothing. 2. Compaction identified its own summary by content: a user message containing <conversation_summary> and the header sentence. A user message quoting those markers (asking about this very mechanism, pasting a summary back) matched, findLast picked it over the real summary, and the real summary was then neither re-parsed nor kept — every earlier turn vanished at the next compaction. The inlined copy in the context-pruner matched the bare tag alone, so a message merely mentioning the tag wiped memory there too. Stamp summaries with a CONVERSATION_SUMMARY tag and recognize them by provenance; keep a legacy fallback requiring the full envelope (open+close tag, header, <historical_memory>) so pre-tag summaries still fold in. The pruner's tag-alone match — the parity test's deliberate divergence — closes: a bare tag mention no longer qualifies in either implementation. Refs CodebuffAI#1166
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
Two verified paths could replace the model's entire context with nothing (issue #1166, from the "Model losing all chat history instead of compacting" reports):
/compactwith an empty model response — the replacement summary was built fromfullResponsewithout an emptiness check, and a silent empty stop yields no recovery chunk, so the whole history was replaced by a single message carrying nothing. Now the replacement only happens when the model actually produced a summary; otherwise the history is kept (the forced next step retries) and a warning is logged. On the new main this guard is already present (coupled trigger + empty/think-only guards) — this branch keeps it and adds the remaining mechanism below.isConversationSummary, sofindLastpicked the quote over the real summary and the real summary was neither re-parsed nor kept: every earlier turn vanished at the next compaction. Summaries are now stamped with aCONVERSATION_SUMMARYtag and recognized by provenance; a legacy fallback requires the full envelope (open+close tag, header,<historical_memory>) so pre-tag summaries still fold in. The context-pruner's inlined copy — which matched the bare tag alone, wiping memory on any message that merely mentioned the tag — gets the same treatment, closing the parity test's second deliberate divergence.Regression tests for both mechanisms, including the exact steal shape (tag + header quote with no memory block) and the legacy-envelope case. The parity test now asserts BOTH implementations keep a tag-mentioning user message.
Validation
Re-run on the new main after the history rewrite:
bun test packages/agent-runtime/src/__tests__/compact-history.test.ts packages/agent-runtime/src/__tests__/context-pruner-parity.test.ts packages/agent-runtime/src/__tests__/main-prompt.test.ts— 55 pass, 0 failFixes #1166
Replaces #1167 — auto-closed when the repo history was force-pushed (the original commits no longer exist in the new history). Rebased onto the new main per the maintainer's request; picking the review up here.