chore(context): refactor for consistency, optimize formatting, and simplify logic

- Cleaned up unnecessary comments in `__all__` definitions for better readability.
- Adjusted indentation and formatting across modules for improved clarity (e.g., long lines, logical grouping).
- Simplified conditional expressions and inline comments for context scoring and ranking.
- Replaced some hard-coded values with type-safe annotations (e.g., `ClassVar`).
- Removed unused imports and ensured consistent usage across test files.
- Updated `test_score_not_cached_on_context` to clarify caching behavior.
- Improved truncation strategy logic and marker handling.
This commit is contained in:
2026-01-04 15:23:14 +01:00
parent 9e54f16e56
commit 2bea057fb1
26 changed files with 226 additions and 273 deletions

View File

@@ -2,16 +2,15 @@
import pytest
from app.services.context.budget import BudgetAllocator
from app.services.context.compression import (
ContextCompressor,
TruncationResult,
TruncationStrategy,
)
from app.services.context.budget import BudgetAllocator, TokenBudget
from app.services.context.types import (
ContextType,
KnowledgeContext,
SystemContext,
TaskContext,
)
@@ -113,7 +112,7 @@ class TestTruncationStrategy:
assert result.truncated is True
assert len(result.content) < len(content)
assert strategy.TRUNCATION_MARKER in result.content
assert strategy.truncation_marker in result.content
@pytest.mark.asyncio
async def test_truncate_middle_strategy(self) -> None:
@@ -126,7 +125,7 @@ class TestTruncationStrategy:
)
assert result.truncated is True
assert strategy.TRUNCATION_MARKER in result.content
assert strategy.truncation_marker in result.content
@pytest.mark.asyncio
async def test_truncate_sentence_strategy(self) -> None:
@@ -140,7 +139,9 @@ class TestTruncationStrategy:
assert result.truncated is True
# Should cut at sentence boundary
assert result.content.endswith(".") or strategy.TRUNCATION_MARKER in result.content
assert (
result.content.endswith(".") or strategy.truncation_marker in result.content
)
class TestContextCompressor:
@@ -235,10 +236,12 @@ class TestTruncationEdgeCases:
content = "Some content to truncate"
# max_tokens less than marker tokens should return just marker
result = await strategy.truncate_to_tokens(content, max_tokens=1, strategy="end")
result = await strategy.truncate_to_tokens(
content, max_tokens=1, strategy="end"
)
# Should handle gracefully without crashing
assert strategy.TRUNCATION_MARKER in result.content or result.content == content
assert strategy.truncation_marker in result.content or result.content == content
@pytest.mark.asyncio
async def test_truncate_with_content_that_has_zero_tokens(self) -> None:
@@ -249,7 +252,7 @@ class TestTruncationEdgeCases:
result = await strategy.truncate_to_tokens("a", max_tokens=100)
# Should not raise ZeroDivisionError
assert result.content in ("a", "a" + strategy.TRUNCATION_MARKER)
assert result.content in ("a", "a" + strategy.truncation_marker)
@pytest.mark.asyncio
async def test_get_content_for_tokens_zero_target(self) -> None: