-
Notifications
You must be signed in to change notification settings - Fork 159
fix: standardize token estimation heuristic between file_operations and base_agent #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
RyanLHicks
wants to merge
1
commit into
mpfaffenberger:main
from
RyanLHicks:fix/standardize-token-estimation-heuristic
Closed
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
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,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 | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>= 1is weaker than necessary—the implementation (per context snippet) returns exactly1for empty input viamax(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 == 1Or better, actually test both estimators as the original docstring intended.
📝 Committable suggestion
🤖 Prompt for AI Agents