Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/agents/test_token_estimation_consistency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Tests for token estimation consistency across modules.

Ensures file_operations._read_file and BaseAgent.estimate_token_count
use the same chars-per-token heuristic to prevent unexpected early
compaction triggered by estimation mismatch.
"""

import math

from code_puppy.agents.agent_code_puppy import CodePuppyAgent


class TestTokenEstimationConsistency:
"""Token estimation should be consistent between file_operations and BaseAgent."""

def test_estimate_token_count_matches_file_operations_heuristic(self):
"""
BaseAgent.estimate_token_count and file_operations._read_file
must use the same 2.5 chars/token heuristic.
"""
agent = CodePuppyAgent()
content = "x" * 1000

base_agent_estimate = agent.estimate_token_count(content)
file_ops_estimate = math.floor(len(content) / 2.5)

assert base_agent_estimate == file_ops_estimate

def test_estimation_consistent_across_content_sizes(self):
"""
Consistency holds across small, medium, and large content sizes.
"""
agent = CodePuppyAgent()

for size in [100, 1000, 10000, 25000]:
content = "x" * size
base_agent_estimate = agent.estimate_token_count(content)
file_ops_estimate = math.floor(len(content) / 2.5)
assert base_agent_estimate == file_ops_estimate, (
f"Mismatch at size {size}: "
f"base_agent={base_agent_estimate}, "
f"file_ops={file_ops_estimate}"
)

def test_minimum_token_count_is_one(self):
"""
Both estimators enforce a minimum of 1 token even for empty content.
"""
agent = CodePuppyAgent()

result = agent.estimate_token_count("")
assert result >= 1
Comment on lines +45 to +52
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Docstring claims to test "Both estimators" but only tests one.

The docstring states "Both estimators enforce a minimum of 1 token" but the test only verifies BaseAgent.estimate_token_count. Additionally, the assertion >= 1 is weaker than necessary—the implementation (per context snippet) returns exactly 1 for empty input via max(1, ...).

🔧 Suggested fix
     def test_minimum_token_count_is_one(self):
         """
-        Both estimators enforce a minimum of 1 token even for empty content.
+        estimate_token_count enforces a minimum of 1 token even for empty content.
         """
         agent = CodePuppyAgent()
 
         result = agent.estimate_token_count("")
-        assert result >= 1
+        assert result == 1

Or better, actually test both estimators as the original docstring intended.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_minimum_token_count_is_one(self):
"""
Both estimators enforce a minimum of 1 token even for empty content.
"""
agent = CodePuppyAgent()
result = agent.estimate_token_count("")
assert result >= 1
def test_minimum_token_count_is_one(self):
"""
estimate_token_count enforces a minimum of 1 token even for empty content.
"""
agent = CodePuppyAgent()
result = agent.estimate_token_count("")
assert result == 1
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/agents/test_token_estimation_consistency.py` around lines 45 - 52,
Docstring and assertion are inconsistent: update the test for CodePuppyAgent.
Change the docstring to state that CodePuppyAgent enforces a minimum of 1 token,
and tighten the assertion to expect exactly 1 by replacing "assert result >= 1"
with "assert result == 1" when calling CodePuppyAgent().estimate_token_count("")
(referencing CodePuppyAgent and estimate_token_count).