fix(compression): keep estimate seam positional-compatible for monkeypatched estimators
Test seams and plugin engines monkeypatch estimate_messages_tokens_rough with (messages)-only signatures; route callers only pass the charge_stale_thinking kwarg on the False path.
This commit is contained in:
@@ -2679,10 +2679,12 @@ def run_conversation(
|
||||
# disagreement that dead-looped compaction.
|
||||
from agent.turn_context import _agent_stale_thinking_on_wire
|
||||
|
||||
approx_tokens = estimate_messages_tokens_rough(
|
||||
api_messages,
|
||||
charge_stale_thinking=_agent_stale_thinking_on_wire(agent),
|
||||
)
|
||||
if _agent_stale_thinking_on_wire(agent):
|
||||
approx_tokens = estimate_messages_tokens_rough(api_messages)
|
||||
else:
|
||||
approx_tokens = estimate_messages_tokens_rough(
|
||||
api_messages, charge_stale_thinking=False
|
||||
)
|
||||
# Route-aware pressure: when the upcoming request is eligible for
|
||||
# native Responses compaction the transport will checkpoint-prune
|
||||
# the payload before sending — the generic durable-history figure
|
||||
|
||||
@@ -3836,9 +3836,15 @@ def estimate_request_tokens_rough(
|
||||
if system_prompt:
|
||||
total += estimate_tokens_rough(system_prompt)
|
||||
if messages:
|
||||
total += estimate_messages_tokens_rough(
|
||||
messages, charge_stale_thinking=charge_stale_thinking
|
||||
)
|
||||
if charge_stale_thinking:
|
||||
# Positional-compatible call: test seams and plugin engines
|
||||
# monkeypatch estimate_messages_tokens_rough with (messages)-only
|
||||
# signatures; only the route-aware False path needs the kwarg.
|
||||
total += estimate_messages_tokens_rough(messages)
|
||||
else:
|
||||
total += estimate_messages_tokens_rough(
|
||||
messages, charge_stale_thinking=False
|
||||
)
|
||||
if tools:
|
||||
total += _estimate_tools_tokens_rough(tools)
|
||||
return total
|
||||
|
||||
@@ -95,11 +95,17 @@ def _preflight_request_tokens(
|
||||
"using generic transcript estimate",
|
||||
exc_info=True,
|
||||
)
|
||||
if _agent_stale_thinking_on_wire(agent):
|
||||
return estimate_request_tokens_rough(
|
||||
messages,
|
||||
system_prompt=system_prompt or "",
|
||||
tools=tools,
|
||||
)
|
||||
return estimate_request_tokens_rough(
|
||||
messages,
|
||||
system_prompt=system_prompt or "",
|
||||
tools=tools,
|
||||
charge_stale_thinking=_agent_stale_thinking_on_wire(agent),
|
||||
charge_stale_thinking=False,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user