This repository was archived by the owner on Jun 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
feat: add context manager class #1115
Draft
lizradway
wants to merge
1
commit into
strands-agents:main
Choose a base branch
from
lizradway:context-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+908
−5
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
83 changes: 83 additions & 0 deletions
83
strands-ts/src/context-manager/compression/__tests__/protection.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { describe, it, expect } from 'vitest' | ||
| import { isProtected, pinMessage } from '../protection.js' | ||
| import { Message, TextBlock, ToolUseBlock, ToolResultBlock } from '../../../types/messages.js' | ||
|
|
||
| function userMsg(text: string): Message { | ||
| return new Message({ role: 'user', content: [new TextBlock(text)] }) | ||
| } | ||
|
|
||
| function assistantMsg(text: string): Message { | ||
| return new Message({ role: 'assistant', content: [new TextBlock(text)] }) | ||
| } | ||
|
|
||
| function toolUseMsg(toolUseId: string): Message { | ||
| return new Message({ role: 'assistant', content: [new ToolUseBlock({ toolUseId, name: 'test', input: {} })] }) | ||
| } | ||
|
|
||
| function toolResultMsg(toolUseId: string): Message { | ||
| return new Message({ | ||
| role: 'user', | ||
| content: [new ToolResultBlock({ toolUseId, content: [new TextBlock('result')], status: 'success' })], | ||
| }) | ||
| } | ||
|
|
||
| describe('isProtected', () => { | ||
| describe('no range, no pin', () => { | ||
| it('returns false for unprotected message', () => { | ||
| const messages = [userMsg('a'), assistantMsg('b')] | ||
| expect(isProtected(messages, 0)).toBe(false) | ||
| expect(isProtected(messages, 1)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('positive range (protect first N)', () => { | ||
| it('protects messages within range', () => { | ||
| const messages = [userMsg('a'), assistantMsg('b'), userMsg('c')] | ||
| expect(isProtected(messages, 0, 2)).toBe(true) | ||
| expect(isProtected(messages, 1, 2)).toBe(true) | ||
| expect(isProtected(messages, 2, 2)).toBe(false) | ||
| }) | ||
|
|
||
| it('protects toolUse outside range if its toolResult is inside range', () => { | ||
| const messages = [userMsg('task'), toolUseMsg('t1'), toolResultMsg('t1'), userMsg('next')] | ||
| // range=2 protects [0] and [1]. [2] is toolResult — check if toolUse at [1] being in range protects [2] | ||
| // Actually [2] is outside range. But [1] (toolUse) is in range, so [2] (toolResult, partner) should be protected. | ||
| expect(isProtected(messages, 2, 2)).toBe(true) | ||
| }) | ||
|
|
||
| it('protects toolResult outside range if its toolUse is inside range', () => { | ||
| const messages = [toolUseMsg('t1'), toolResultMsg('t1'), userMsg('a'), assistantMsg('b')] | ||
| // range=1 protects [0] (toolUse). [1] (toolResult) is outside but partner is protected. | ||
| expect(isProtected(messages, 1, 1)).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('negative range (protect last N)', () => { | ||
| it('protects messages within range', () => { | ||
| const messages = [userMsg('a'), assistantMsg('b'), userMsg('c'), assistantMsg('d'), userMsg('e')] | ||
| expect(isProtected(messages, 0, -2)).toBe(false) | ||
| expect(isProtected(messages, 2, -2)).toBe(false) | ||
| expect(isProtected(messages, 3, -2)).toBe(true) | ||
| expect(isProtected(messages, 4, -2)).toBe(true) | ||
| }) | ||
|
|
||
| it('protects toolUse outside range if its toolResult is inside range', () => { | ||
| const messages = [userMsg('a'), toolUseMsg('t1'), toolResultMsg('t1'), userMsg('b'), assistantMsg('c')] | ||
| // range=-3: protects [2], [3], [4]. toolUse at [1] is outside, but [2] (its toolResult) is in range. | ||
| expect(isProtected(messages, 1, -3)).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('pinned messages', () => { | ||
| it('protects pinned message regardless of range', () => { | ||
| const messages = [userMsg('a'), pinMessage(assistantMsg('pinned')), userMsg('c')] | ||
| expect(isProtected(messages, 1)).toBe(true) | ||
| expect(isProtected(messages, 1, 0)).toBe(true) | ||
| }) | ||
|
|
||
| it('protects tool-pair partner of pinned message', () => { | ||
| const messages = [pinMessage(toolUseMsg('t1')), toolResultMsg('t1'), userMsg('a')] | ||
| expect(isProtected(messages, 1)).toBe(true) // toolResult partner of pinned toolUse | ||
| }) | ||
| }) | ||
| }) |
185 changes: 185 additions & 0 deletions
185
strands-ts/src/context-manager/compression/context-compression.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import type { Plugin } from '../../plugins/plugin.js' | ||
| import type { LocalAgent } from '../../types/agent.js' | ||
| import type { Tool } from '../../tools/tool.js' | ||
| import type { Message } from '../../types/messages.js' | ||
| import type { Model } from '../../models/model.js' | ||
| import { AfterInvocationEvent, AfterModelCallEvent, BeforeModelCallEvent } from '../../hooks/events.js' | ||
| import { ContextWindowOverflowError } from '../../errors.js' | ||
| import { truncate } from './strategies/truncate.js' | ||
| import { summarize, type SummarizeOptions } from './strategies/summarize.js' | ||
| import { estimateInputTokens } from '../token-estimation/token-estimation.js' | ||
| import { logger } from '../../logging/logger.js' | ||
| import { warnOnce } from '../../logging/warn-once.js' | ||
|
|
||
| const DEFAULT_CONTEXT_WINDOW_LIMIT = 200_000 | ||
| const DEFAULT_PROACTIVE_THRESHOLD = 0.7 | ||
| const DEFAULT_WINDOW_SIZE = 40 | ||
|
|
||
| export type CompressionMethod = 'truncate' | 'summarize' | ||
|
|
||
| type SharedCompressionOptions = { | ||
| /** | ||
| * Proactive compression before the model call. | ||
| * - `true`: compress when 70% of the context window is used (default threshold). | ||
| * - `{ threshold: number }`: compress at the specified ratio (0, 1]. | ||
| * - `false`: disable proactive compression; only reactive overflow recovery is used. | ||
| * - Omitted: defaults to `true`. | ||
| */ | ||
| proactive?: boolean | { threshold: number } | ||
| /** | ||
| * Protect messages from eviction during reduction. | ||
| * Positive values protect the first N messages; negative values protect the last N. | ||
| * | ||
| * For agent-controlled pinning, use the `pinMessageTool` (agentic mode). | ||
| */ | ||
| protectedMessageRange?: number | ||
| } | ||
|
|
||
| export type TruncateCompressionConfig = SharedCompressionOptions & { | ||
| method?: 'truncate' | ||
| /** Maximum messages to keep after trimming. Defaults to 40. */ | ||
| windowSize?: number | ||
| } | ||
|
|
||
| export type SummarizeCompressionConfig = SharedCompressionOptions & { | ||
| method: 'summarize' | ||
| /** Ratio of messages to summarize (0.1–0.8). Defaults to 0.3. */ | ||
| summaryRatio?: number | ||
| /** Minimum recent messages to preserve during summarization. Defaults to 10. */ | ||
| preserveRecentMessages?: number | ||
| } | ||
|
|
||
| /** | ||
| * Compression configuration (discriminated union on `method`). | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * contextManager: { compression: true } // defaults (truncate) | ||
| * contextManager: { compression: 'summarize' } // strategy shorthand | ||
| * contextManager: { compression: { method: 'truncate', windowSize: 30 } } // full config | ||
| * contextManager: { compression: { method: 'summarize', summaryRatio: 0.5 } } // full config | ||
| * ``` | ||
| */ | ||
| export type CompressionOptions = TruncateCompressionConfig | SummarizeCompressionConfig | ||
|
|
||
| /** | ||
| * Plugin that handles context compression — both proactive (before model call when | ||
| * threshold is exceeded) and reactive (after model call on overflow error). | ||
| * | ||
| * Delegates reduction to strategy functions (truncate or summarize). | ||
| */ | ||
| export class ContextCompression implements Plugin { | ||
| readonly name = 'strands:context-compression' | ||
|
|
||
| private readonly _proactiveThreshold: number | undefined | ||
| private readonly _method: CompressionMethod | ||
| private readonly _windowSize: number | ||
| private readonly _protectedMessageRange: number | undefined | ||
| private readonly _summarizeOptions: SummarizeOptions | undefined | ||
|
|
||
| constructor(config?: CompressionOptions) { | ||
| const proactive = config?.proactive ?? true | ||
| if (proactive === false) { | ||
| this._proactiveThreshold = undefined | ||
| } else if (proactive === true) { | ||
| this._proactiveThreshold = DEFAULT_PROACTIVE_THRESHOLD | ||
| } else { | ||
| if (proactive.threshold <= 0 || proactive.threshold > 1) { | ||
| throw new Error( | ||
| `proactive compression threshold must be between 0 (exclusive) and 1 (inclusive), got ${proactive.threshold}` | ||
| ) | ||
| } | ||
| this._proactiveThreshold = proactive.threshold | ||
| } | ||
|
|
||
| this._method = config?.method ?? 'truncate' | ||
| this._protectedMessageRange = config?.protectedMessageRange | ||
|
|
||
| if (config?.method === 'summarize') { | ||
| this._windowSize = DEFAULT_WINDOW_SIZE | ||
| this._summarizeOptions = { | ||
| ...(config.summaryRatio !== undefined && { summaryRatio: config.summaryRatio }), | ||
| ...(config.preserveRecentMessages !== undefined && { preserveRecentMessages: config.preserveRecentMessages }), | ||
| } | ||
| } else { | ||
| this._windowSize = (config as TruncateCompressionConfig | undefined)?.windowSize ?? DEFAULT_WINDOW_SIZE | ||
| this._summarizeOptions = undefined | ||
| } | ||
| } | ||
|
|
||
| getTools(): Tool[] { | ||
| return [] | ||
| } | ||
|
|
||
| initAgent(agent: LocalAgent): void { | ||
| // Reactive overflow recovery | ||
| agent.addHook(AfterModelCallEvent, async (event) => { | ||
| if (event.error instanceof ContextWindowOverflowError) { | ||
| if (await this._reduce(event.agent.messages, event.model)) { | ||
| event.retry = true | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| // Proactive compression | ||
| agent.addHook(BeforeModelCallEvent, async (event) => { | ||
| if (this._proactiveThreshold === undefined) { | ||
| return | ||
| } | ||
|
|
||
| let contextWindowLimit = event.model.getConfig().contextWindowLimit | ||
| if (contextWindowLimit === undefined) { | ||
| contextWindowLimit = DEFAULT_CONTEXT_WINDOW_LIMIT | ||
| warnOnce( | ||
| logger, | ||
| `context_compression | contextWindowLimit is not set on the model, using default of ${DEFAULT_CONTEXT_WINDOW_LIMIT} | set contextWindowLimit in your model config for accurate proactive compression` | ||
| ) | ||
| } | ||
|
|
||
| const projectedInputTokens = | ||
|
lizradway marked this conversation as resolved.
|
||
| event.projectedInputTokens ?? (await estimateInputTokens(event.agent.messages, event.model)) | ||
|
|
||
| if (projectedInputTokens === undefined) { | ||
| return | ||
| } | ||
|
|
||
| const ratio = projectedInputTokens / contextWindowLimit | ||
| if (ratio >= this._proactiveThreshold) { | ||
| logger.debug( | ||
| `projected_tokens=<${projectedInputTokens}>, limit=<${contextWindowLimit}>, ratio=<${ratio.toFixed(2)}>, threshold=<${this._proactiveThreshold}> | compression threshold exceeded, reducing context` | ||
| ) | ||
| try { | ||
| await this._reduce(event.agent.messages, event.model) | ||
| } catch (e) { | ||
| logger.warn(`context_compression | proactive compression failed, continuing | error=<${e}>`) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| // Sliding window enforcement after each invocation (truncate method only) | ||
| if (this._method === 'truncate') { | ||
| agent.addHook(AfterInvocationEvent, (event) => { | ||
| if (event.agent.messages.length > this._windowSize) { | ||
| truncate(event.agent.messages, this._windowSize, { | ||
| ...(this._protectedMessageRange !== undefined && { protectedMessageRange: this._protectedMessageRange }), | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| private async _reduce(messages: Message[], model: Model): Promise<boolean> { | ||
| switch (this._method) { | ||
| case 'summarize': | ||
| return summarize(messages, model, { | ||
| ...this._summarizeOptions, | ||
| ...(this._protectedMessageRange !== undefined && { protectedMessageRange: this._protectedMessageRange }), | ||
| }) | ||
| case 'truncate': | ||
| default: | ||
| return truncate(messages, this._windowSize, { | ||
| ...(this._protectedMessageRange !== undefined && { protectedMessageRange: this._protectedMessageRange }), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.