feat: add context manager class#1115
Conversation
| ...messagesToSummarize, | ||
| new Message({ role: 'user', content: [new TextBlock('Please summarize this conversation.')] }), | ||
| ] | ||
|
|
There was a problem hiding this comment.
Issue: The summarize function generates a summary with role: 'user' (line 97). This means the model will see an injected "user" message containing a conversation summary, which may confuse the model into thinking the user sent that summary. This is especially concerning since the summary format includes headings like "## Conversation Summary" and "## Tools Executed".
Suggestion: Consider using role: 'assistant' for the summary message, or wrapping it with clear delimiters indicating it's a system-generated summary. Many summarization implementations in conversation managers use a user message with an explicit framing like [Previous conversation summary: ...] to make the origin clear.
|
Assessment: Request Changes This PR introduces a well-architected Review Categories
The architecture and composability model are solid — the plugin-composes-sub-plugins pattern is clean and extensible. |
bcd6f96 to
5be1deb
Compare
5be1deb to
9f44605
Compare
Description
Implements the v2
contextManagerclass as designed in strands-agents/docs#831.Adds a
contextManagerparameter toAgentConfigthat pre-composes the SDK's context management primitives into a single configuration surface. TheContextManagerclass is a plugin that owns shared infrastructure (storage, token estimation, budget tracking) and composes sub-plugins (ContextCompression,ContextOffloader) that handle the actual behavior.Architecture
Sub-plugins work independently when used standalone (without
ContextManager). User-provided plugins with matching names take precedence over managed sub-plugins. WhencontextManageris set,ContextCompressiontakes priority overconversationManager.What ships
ContextManagerclass (Plugin) — owns token estimation, budget tracking, composes sub-pluginsContextCompressionplugin — proactive/reactive compression with own reduction logic (truncate or summarize strategies via discriminated union config)ContextStrategyconst —{ Auto: 'auto' }for strategy discoverabilityCompressionStrategyconst —{ Truncate: 'truncate', Summarize: 'summarize' }estimateInputTokens()utility — shared token estimation extracted tosrc/context-manager/token-estimation.tsprotectedMessageRange— positive protects first N, negative protects last N messages from evictionconversationManagermarked as pending deprecation — still works, JSDoc-taggedPublic API Surface
New exports:
ContextManager(class, Plugin)ContextStrategy(const)ContextCompression(class, Plugin)CompressionStrategy(const)ContextManagerParam,ContextManagerConfig,ContextStrategyValue,ToolResultCacheConfig,CompressionConfig,ContextCompressionConfig,CompressionStrategyValue,TruncateCompressionConfig,SummarizeCompressionConfigNew on
AgentConfig:contextManager?: ContextManagerParamDefaults (benchmark-validated)
toolResultCache.thresholdtoolResultCache.previewTokenscompression.proactivecompression.strategycompression.windowSizestoragev2 Deprecation Plan
The following will be deprecated in v2 and removed in v3:
AgentConfig.conversationManager→contextManager.compressionAgent._estimateInputTokens()→ContextManagerhook + shared utilityBeforeModelCallEvent.projectedInputTokens→cm.budgetConversationManager,SlidingWindowConversationManager,SummarizingConversationManager,NullConversationManager→ContextCompressionpluginvended-plugins/context-offloader/module →context-manager/tool-result-cache/Breaking Changes
None. All changes are additive. Existing behavior is unchanged when
contextManageris not set.Related Issues
Documentation PR
strands-agents/docs#831
Type of Change
New feature
Testing
How have you tested the change?
npm run checknpm run type-check)npm run lint)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.