Skip to content

Conversation

@diegonix
Copy link
Contributor

Fix: Message Compaction System - Critical Bug Fixes

Problem

The message compaction system had two critical bugs:

  1. Missing method: PlanningAgent object has no attribute 'compact_messages' - The code called self.compact_messages() which doesn't exist; it should be self.summarize_messages()

  2. Global flag state leak: The _delayed_compaction_requested flag was a global variable that could leak state between sessions. Additionally, the flag was never reset when /compact was executed manually, causing spurious delayed compaction attempts on subsequent messages.

Solution

  1. Fixed method call in base_agent.py:1796 - Changed self.compact_messages() to self.summarize_messages()

  2. Refactored global flag to instance attribute in base_agent.py:93 - Moved _delayed_compaction_requested from module-level global to instance variable self._delayed_compaction_requested to prevent state contamination between sessions

  3. Added flag reset in /compact command in session_commands.py - Added agent._delayed_compaction_requested = False to reset the flag after manual compaction

  4. Added comprehensive test coverage - Created tests/agents/test_base_agent_compaction_flags.py with 9 unit tests covering flag lifecycle, idempotency, and instance isolation

Files Changed

  • code_puppy/agents/base_agent.py - Flag refactor + method fix
  • code_puppy/command_line/session_commands.py - Flag reset in /compact
  • tests/agents/test_base_agent_compaction_flags.py - New test file (9 tests)

Testing

  • All 30 compaction-related tests pass
  • 1957/1967 total tests pass (1 pre-existing unrelated failure)
  • No regressions detected

Backwards Compatibility

  • Fully backwards compatible - no schema changes, no migration required

…/compact

## Motivation
Previously the delayed compaction flag was implemented as a global module-level variable which caused cross-agent contamination and intermittent compaction behavior across sessions. This commit fixes that regression by making the flag instance-scoped and ensuring manual compaction commands properly clear the flag so delayed compaction doesn't trigger unexpectedly.

## Architecture / Approach
Remove the global _delayed_compaction_requested and add self._delayed_compaction_requested on BaseAgent instances during initialization. Update request_delayed_compaction() to set the instance flag and should_attempt_delayed_compaction() to consult and reset the instance flag only when it is safe to compact. Align the internal message-processing path to call summarize_messages(...) where logical summarization is expected. Update handle_compact_command (session_commands) to clear the instance flag when a manual /compact succeeds and emit explicit informational/success messages to surface the state change to users.

## Affected Components
- code_puppy/agents/base_agent.py: remove global flag; BaseAgent.__init__ adds instance flag; request_delayed_compaction and should_attempt_delayed_compaction now use the instance field; message processing uses summarize_messages call in the compaction flow.
- code_puppy/command_line/session_commands.py: handle_compact_command resets the agent._delayed_compaction_requested flag and emits token_context_status messages around manual compaction.
- tests/agents/test_base_agent_compaction_flags.py: new comprehensive regression tests for instance-level flag lifecycle and idempotency across agents.
- tests/test_command_handler.py: added tests to ensure /compact clears the delayed flag and remains idempotent when the flag is already clear.

## Performance / Security Impact
This change is behavioral and has negligible performance impact. It reduces the risk of unintended compaction operations across agent instances (a correctness/security concern for session isolation). The summarize_messages call substitution may centralize compaction behavior but does not change message retention policies beyond fixing the flag semantics.

## Compatibility / Testing
Adds unit tests covering initialization, multiple request cycles, pending-tool-call behavior, cross-instance isolation, and manual /compact interactions to prevent regressions. The change is backward compatible from a user perspective; manual /compact now explicitly clears any pending delayed compaction flag (preventing duplicate compactions). Developers should note the removed global flag if any external code referenced it.
@diegonix diegonix force-pushed the hotfix/compaction_sched branch from 611e2af to 16dd7a6 Compare December 30, 2025 19:15
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