feat(context): implement SlidingWindowStrategy with configurable window size #9995

Open
opened 2026-04-16 11:23:30 +00:00 by HAL9000 · 1 comment
Owner

Background: The ContextStrategy protocol (see #8616) requires concrete implementations to be useful. A sliding window strategy is the most fundamental context management approach, limiting token usage by keeping only the most recent N messages or tokens in context. This is critical for long-running agent sessions that would otherwise exceed LLM context limits.

Acceptance criteria:

  • SlidingWindowStrategy class implements the ContextStrategy protocol
  • Window size is configurable (by message count and/or token count)
  • Strategy correctly truncates oldest messages when window is exceeded
  • Strategy preserves system prompt outside the sliding window
  • Unit tests achieve ≥ 97% coverage for the strategy module

Metadata

  • Commit Message: feat(context): implement SlidingWindowStrategy with configurable window size
  • Branch: feat/context-sliding-window-strategy

Subtasks

  • Define SlidingWindowStrategy class implementing ContextStrategy protocol
  • Add window_size (int) and window_mode (messages | tokens) config parameters
  • Implement apply(context: ContextWindow) -> ContextWindow method with truncation logic
  • Ensure system prompt is always preserved (not subject to sliding window)
  • Register strategy in the plugin registry under key "sliding_window"
  • Write unit tests covering edge cases (empty context, exact boundary, overflow)

Definition of Done

  • All acceptance criteria met
  • Tests written and passing (coverage ≥ 97%)
  • Code reviewed and approved
  • Documentation updated
  • No regressions introduced

Parent Epic

Child of and blocks #8505 — Epic: Advanced Context Strategies & LLM Provider Extensions (v3.6.0)


Automated by CleverAgents Bot
Supervisor: Epic Planning | Agent: epic-planning-pool-supervisor
Worker: [AUTO-EPIC-2]

**Background**: The `ContextStrategy` protocol (see #8616) requires concrete implementations to be useful. A sliding window strategy is the most fundamental context management approach, limiting token usage by keeping only the most recent N messages or tokens in context. This is critical for long-running agent sessions that would otherwise exceed LLM context limits. **Acceptance criteria**: - [ ] `SlidingWindowStrategy` class implements the `ContextStrategy` protocol - [ ] Window size is configurable (by message count and/or token count) - [ ] Strategy correctly truncates oldest messages when window is exceeded - [ ] Strategy preserves system prompt outside the sliding window - [ ] Unit tests achieve ≥ 97% coverage for the strategy module ## Metadata - **Commit Message**: `feat(context): implement SlidingWindowStrategy with configurable window size` - **Branch**: `feat/context-sliding-window-strategy` ## Subtasks - [x] Define `SlidingWindowStrategy` class implementing `ContextStrategy` protocol - [x] Add `window_size` (int) and `window_mode` (`messages` | `tokens`) config parameters - [x] Implement `apply(context: ContextWindow) -> ContextWindow` method with truncation logic - [x] Ensure system prompt is always preserved (not subject to sliding window) - [x] Register strategy in the plugin registry under key `"sliding_window"` - [x] Write unit tests covering edge cases (empty context, exact boundary, overflow) ## Definition of Done - [ ] All acceptance criteria met - [ ] Tests written and passing (coverage ≥ 97%) - [ ] Code reviewed and approved - [ ] Documentation updated - [ ] No regressions introduced ## Parent Epic Child of and blocks #8505 — Epic: Advanced Context Strategies & LLM Provider Extensions (v3.6.0) --- **Automated by CleverAgents Bot** Supervisor: Epic Planning | Agent: epic-planning-pool-supervisor Worker: [AUTO-EPIC-2]
Author
Owner

Implementation Attempt — Tier 1: Haiku — Success

Implemented the SlidingWindowStrategy class with configurable window size and mode.

What was done:

  • Created src/cleveragents/application/services/sliding_window_strategy.py implementing the ContextStrategy protocol
  • Supports window_size (int) and window_mode ("messages" | "tokens") configuration parameters
  • Messages mode: keeps the most recent N non-system fragments
  • Tokens mode: keeps the most recent fragments within the token budget
  • System prompt preservation: fragments with metadata["role"] == "system" are always preserved
  • Registered in the plugin registry under key "sliding_window" via ACMSPipeline.register_strategy()
  • Input validation: window_size must be positive, window_mode must be "messages" or "tokens"
  • Created features/sliding_window_strategy.feature with 22 BDD scenarios
  • Created features/steps/sliding_window_strategy_steps.py with step definitions

Quality gate status:

  • lint ✓ (ruff: all checks passed)
  • typecheck ✓ (pyright: 0 errors)
  • unit_tests ✓ (22/22 scenarios passed)

PR: #10776#10776


Automated by CleverAgents Bot
Supervisor: Implementation Pool | Agent: implementation-worker

**Implementation Attempt** — Tier 1: Haiku — Success Implemented the `SlidingWindowStrategy` class with configurable window size and mode. **What was done:** - Created `src/cleveragents/application/services/sliding_window_strategy.py` implementing the `ContextStrategy` protocol - Supports `window_size` (int) and `window_mode` (`"messages"` | `"tokens"`) configuration parameters - Messages mode: keeps the most recent N non-system fragments - Tokens mode: keeps the most recent fragments within the token budget - System prompt preservation: fragments with `metadata["role"] == "system"` are always preserved - Registered in the plugin registry under key `"sliding_window"` via `ACMSPipeline.register_strategy()` - Input validation: `window_size` must be positive, `window_mode` must be `"messages"` or `"tokens"` - Created `features/sliding_window_strategy.feature` with 22 BDD scenarios - Created `features/steps/sliding_window_strategy_steps.py` with step definitions **Quality gate status:** - lint ✓ (ruff: all checks passed) - typecheck ✓ (pyright: 0 errors) - unit_tests ✓ (22/22 scenarios passed) **PR:** #10776 — https://git.cleverthis.com/cleveragents/cleveragents-core/pulls/10776 --- **Automated by CleverAgents Bot** Supervisor: Implementation Pool | Agent: implementation-worker
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#9995
No description provided.