fix(complexity_router): count structured tool results in context escalation

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Devin AI 2026-09-21 02:23:10 +00:00
parent aeaae23dfc
commit e78c6be041
3 changed files with 22 additions and 2 deletions

View file

@ -460,8 +460,22 @@ def _message_text(content: object) -> str:
return content if isinstance(content, str) else ""
def _estimated_content_characters(content: object) -> int:
if isinstance(content, str):
return len(content)
if isinstance(content, list):
return sum(_estimated_content_characters(part) for part in content)
if isinstance(content, Mapping):
content_type: Final = content.get("type")
if content_type == "text":
return _estimated_content_characters(content.get("text"))
if content_type == "tool_result":
return _estimated_content_characters(content.get("content"))
return 0
def _estimated_conversation_tokens(messages: Sequence[Mapping[str, object]] | None) -> int:
return sum(len(_message_text(message.get("content"))) // 4 for message in messages or ())
return sum(_estimated_content_characters(message.get("content")) // 4 for message in messages or ())
def _reminder_block_spans(lowered: str, open_marker: str, close_marker: str) -> Iterator[tuple[int, int]]:

View file

@ -13538,9 +13538,13 @@ class TestHeuristicFirst:
],
4,
),
(
[{"role": "user", "content": [{"type": "tool_result", "content": "abcdefghijklmnop"}]}],
4,
),
],
)
def test_estimated_conversation_tokens_counts_text_parts(self, messages, expected):
def test_estimated_conversation_tokens_counts_text_and_tool_result_parts(self, messages, expected):
assert _estimated_conversation_tokens(messages) == expected
@pytest.mark.asyncio

View file

@ -278,7 +278,9 @@ const ClassificationMethodConfig: React.FC<ClassificationMethodConfigProps> = ({
const parsed: number = Number(raw);
if (Number.isFinite(parsed)) {
onChange({ ...value, heuristic_first_max_context_tokens: Math.max(1, Math.round(parsed)) });
return;
}
onChange({ ...value, heuristic_first_max_context_tokens: undefined });
};
const handleHybridBoundaryMarginChange = (raw: string) => {