Skip to content

test: add unit tests for TokenAwareConversationManager#2039

Closed
FlorentLa wants to merge 2 commits intostrands-agents:mainfrom
FlorentLa:feat/token-aware-conversation-manager-tests
Closed

test: add unit tests for TokenAwareConversationManager#2039
FlorentLa wants to merge 2 commits intostrands-agents:mainfrom
FlorentLa:feat/token-aware-conversation-manager-tests

Conversation

@FlorentLa
Copy link
Copy Markdown

Motivation

Adds comprehensive test coverage for the TokenAwareConversationManager introduced in #2038. This PR is the test-only companion to keep each PR under the size limit.

Depends on: #2038

Public API Changes

No public API changes.

Testing

35 unit tests covering:

  • Initialization with default and custom parameters
  • Hook registration and BeforeModelCallEvent callback (token capture from cycles[-2])
  • ANSI sanitization of tool results
  • Tool result truncation pass
  • Tool pair adjustment logic (avoiding orphaned toolUse/toolResult)
  • Full compact flow: truncation → summarization → trim fallback
  • LLM summarization via model.stream() (no agent re-entry)
  • State persistence (get_state / restore_from_session)
  • Role alternation after compaction (summary as assistant role)
  • removed_message_count tracking across multiple compactions

Florent Lacroute added 2 commits April 2, 2026 16:21
Token-based context management that uses actual inputTokens from model
responses to decide when to compact, instead of counting messages.

Four-pass compaction strategy:
1. Sanitize — strip ANSI escape codes, collapse repeated lines
2. Truncate — replace oversized tool results with placeholders
3. Summarize — use model.stream() to summarize older messages
4. Trim — remove oldest messages as last resort

The first user message is always preserved so the agent never loses
sight of its original task. Summarization calls model.stream() directly,
avoiding re-entrant agent invocation and deadlocks on _invocation_lock.
35 tests covering all four compaction passes, hook callbacks,
state persistence, role alternation after summarization, and
edge cases (too few messages, summarization failure fallback).
@FlorentLa
Copy link
Copy Markdown
Author

Superseded by a standalone tests PR based on main (to pass label-size check).

@FlorentLa FlorentLa closed this Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant