Stop runaway emergency compaction when compacted_count drifts past history#193
Open
cooleryu wants to merge 1 commit into
Open
Stop runaway emergency compaction when compacted_count drifts past history#193cooleryu wants to merge 1 commit into
cooleryu wants to merge 1 commit into
Conversation
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
Fixes #175 by making stale compaction state recover instead of re-entering the emergency compaction loop.
This PR:
active_messages()withmin(compacted_count, messages.len()), so a stale compacted count no longer falls back to replaying the full transcriptcompacted_count > messages.len()before API message assembly, background compaction application, and hard compactionmessages.len()when caller history is availablecargo fmt --allformatting needed by the repository CIMotivation
The issue report shows a long-running coding-agent session getting wedged after emergency compaction. Once
compacted_countdrifted past the actual message history length,active_messages()returned the full transcript again. That made the next API payload include both the summary and old messages, kept the context above the threshold, and allowed hard compaction to append repeated[Emergency compaction]markers while increasingcompacted_counteven further.The important invariant is: if the caller provides the full message list, a compacted count beyond that list cannot mean "all messages are active again". It means the manager has stale state and should recover to an empty active tail.
Changes
compacted_countas a recoverable state and clamp it tomessages.len().check_and_apply_compaction().compacted_countbeyond caller history.active_messages()clamping stale state to an empty active tailTest Plan
cargo test test_bug_175_ -- --nocapturecargo test compaction::tests:: -- --nocapturecargo fmt --all -- --checkcargo check --all-targets --all-featuresI also installed and tried local clippy. On my macOS machine it stops on pre-existing macOS-only lints in
crates/jcode-core/src/stdin_detect.rs; I did not mix that unrelated cleanup into this PR.Risk
Low to moderate. This changes recovery behavior only when
compacted_countis already inconsistent with the caller-provided history. Normal compaction paths should continue to use the same active suffix. The no-history compatibility path is preserved so existing callers ofcheck_and_apply_compaction()still apply completed background compactions.Maintainer Context
The issue already included a useful local hotfix note. This PR turns that direction into a tested upstream fix and keeps the scope limited to the compaction invariant, recovery points, and regression coverage. It intentionally does not add a session-file migration script; that can remain a separate operational recovery step if maintainers want it.
Need help on this PR? Tag
@codesmithwith what you need.