feat(context): implement PriorityContextStrategy with configurable priority scoring #9997

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

Background: Different messages in an agent's context have varying importance — tool results, error messages, and user instructions are typically more critical than intermediate reasoning steps. A priority-based context strategy allows developers to assign and configure scoring rules so that high-priority messages are always retained when the context window must be trimmed, improving agent reliability in constrained environments.

Acceptance criteria:

  • PriorityContextStrategy class implements the ContextStrategy protocol
  • Priority scoring is configurable via a scoring function or rule-based config
  • Built-in priority rules cover: message role (system > tool > user > assistant), recency, and explicit priority tags
  • Strategy retains highest-scoring messages within the token budget
  • Strategy is registered in the plugin registry under key "priority_context"
  • Unit tests achieve ≥ 97% coverage

Metadata

  • Commit Message: feat(context): implement PriorityContextStrategy with configurable priority scoring
  • Branch: feat/context-priority-strategy

Subtasks

  • Define PriorityContextStrategy class implementing ContextStrategy protocol
  • Define PriorityRule dataclass with field, matcher, and score attributes
  • Implement default scoring rules (role-based, recency decay, explicit tag boost)
  • Allow custom scoring function injection via score_fn: Callable[[Message], float]
  • Implement greedy selection of highest-scoring messages within token budget
  • Register strategy in plugin registry under key "priority_context"
  • Write unit tests covering all default rules and custom scoring function injection

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**: Different messages in an agent's context have varying importance — tool results, error messages, and user instructions are typically more critical than intermediate reasoning steps. A priority-based context strategy allows developers to assign and configure scoring rules so that high-priority messages are always retained when the context window must be trimmed, improving agent reliability in constrained environments. **Acceptance criteria**: - [ ] `PriorityContextStrategy` class implements the `ContextStrategy` protocol - [ ] Priority scoring is configurable via a scoring function or rule-based config - [ ] Built-in priority rules cover: message role (system > tool > user > assistant), recency, and explicit priority tags - [ ] Strategy retains highest-scoring messages within the token budget - [ ] Strategy is registered in the plugin registry under key `"priority_context"` - [ ] Unit tests achieve ≥ 97% coverage ## Metadata - **Commit Message**: `feat(context): implement PriorityContextStrategy with configurable priority scoring` - **Branch**: `feat/context-priority-strategy` ## Subtasks - [ ] Define `PriorityContextStrategy` class implementing `ContextStrategy` protocol - [ ] Define `PriorityRule` dataclass with `field`, `matcher`, and `score` attributes - [ ] Implement default scoring rules (role-based, recency decay, explicit tag boost) - [ ] Allow custom scoring function injection via `score_fn: Callable[[Message], float]` - [ ] Implement greedy selection of highest-scoring messages within token budget - [ ] Register strategy in plugin registry under key `"priority_context"` - [ ] Write unit tests covering all default rules and custom scoring function injection ## 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 PriorityContextStrategy with configurable priority scoring for issue #9997.

What was done:

  • Created src/cleveragents/application/services/priority_context_strategy.py with:
    • PriorityRule dataclass with field, matcher, and score attributes
    • PriorityContextStrategy class implementing the ContextStrategy protocol
    • Default role-based priority rules: system (1.0) > tool (0.75) > user (0.5) > assistant (0.25)
    • Recency decay scoring using exponential half-life (7 days)
    • Explicit priority tag boost (0.6) via metadata["priority_tag"]
    • Custom scoring function injection via score_fn parameter
    • Custom PriorityRule list injection via rules parameter
    • Greedy selection of highest-scoring fragments within token budget
    • Registered in ACMS pipeline under key "priority_context"
  • Created features/priority_context_strategy.feature with 18 BDD scenarios
  • Created features/steps/priority_context_strategy_steps.py with step definitions

Quality Gates:

  • lint: All checks passed
  • typecheck: 0 errors
  • unit_tests: 18 scenarios passed, 0 failed

PR:

PR #10772 created: #10772


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

**Implementation Attempt** — Tier 1: Haiku — Success Implemented `PriorityContextStrategy` with configurable priority scoring for issue #9997. ## What was done: - Created `src/cleveragents/application/services/priority_context_strategy.py` with: - `PriorityRule` dataclass with `field`, `matcher`, and `score` attributes - `PriorityContextStrategy` class implementing the `ContextStrategy` protocol - Default role-based priority rules: system (1.0) > tool (0.75) > user (0.5) > assistant (0.25) - Recency decay scoring using exponential half-life (7 days) - Explicit priority tag boost (0.6) via `metadata["priority_tag"]` - Custom scoring function injection via `score_fn` parameter - Custom `PriorityRule` list injection via `rules` parameter - Greedy selection of highest-scoring fragments within token budget - Registered in ACMS pipeline under key `"priority_context"` - Created `features/priority_context_strategy.feature` with 18 BDD scenarios - Created `features/steps/priority_context_strategy_steps.py` with step definitions ## Quality Gates: - ✅ lint: All checks passed - ✅ typecheck: 0 errors - ✅ unit_tests: 18 scenarios passed, 0 failed ## PR: PR #10772 created: https://git.cleverthis.com/cleveragents/cleveragents-core/pulls/10772 --- **Automated by CleverAgents Bot** Supervisor: Implementation Pool | Agent: implementation-worker
HAL9000 added this to the v3.6.0 milestone 2026-05-02 23:21:07 +00:00
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#9997
No description provided.