mirror of
https://github.com/NCBM/plyngent.git
synced 2026-11-07 15:37:55 +08:00
core/agent: cap tool results in the request-prep pass
A tool result now stays whole in its message (up to a storage ceiling) and is trimmed on the request copy by the request-prep pass, where soft compact already runs. The trimmed copy names the document that holds the rest, so every surface (chat-completions, Responses, Messages) shares one rule. New [agent] hard_max_tool_result_chars bounds the stored body; the wire cap stays [agent] max_tool_result_chars. Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
This commit is contained in:
@@ -1,27 +1,49 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from plyngent.agent.budget import estimate_message_chars, truncate_tool_result
|
||||
from plyngent.agent.budget import (
|
||||
cap_hard_tool_text,
|
||||
cap_tool_message,
|
||||
estimate_message_chars,
|
||||
truncate_for_request,
|
||||
)
|
||||
from plyngent.lmproto.openai_compatible.model import ToolChatMessage, UserChatMessage
|
||||
|
||||
|
||||
def test_truncate_tool_result_short() -> None:
|
||||
assert truncate_tool_result("hello", 100) == "hello"
|
||||
def test_cap_hard_tool_text_short_untouched() -> None:
|
||||
assert cap_hard_tool_text("hello", 100) == "hello"
|
||||
|
||||
|
||||
def test_truncate_tool_result_long() -> None:
|
||||
text = "a" * 50
|
||||
out = truncate_tool_result(text, 20)
|
||||
assert "Truncated" in out
|
||||
assert "20" in out
|
||||
assert "30" in out
|
||||
# Generic tool results now embed a memory token too (resumable via get_truncated).
|
||||
assert "truncate_token=" in out
|
||||
assert out.startswith(("a" * 10, "a" * 20)) # marker is budgeted
|
||||
def test_cap_hard_tool_text_cuts_with_ceiling_marker() -> None:
|
||||
out = cap_hard_tool_text("a" * 50, 20)
|
||||
assert out.startswith("a" * 20)
|
||||
assert "[Stored at the 20-char ceiling: 30 chars beyond it]" in out
|
||||
|
||||
|
||||
def test_truncate_tool_result_fits_no_marker() -> None:
|
||||
assert truncate_tool_result("tiny", 100) == "tiny"
|
||||
assert "Truncated" not in truncate_tool_result("tiny", 100)
|
||||
def test_cap_tool_message_names_the_document() -> None:
|
||||
message = ToolChatMessage(content="a" * 50, tool_call_id="call-7")
|
||||
capped = cap_tool_message(message, 20)
|
||||
assert capped is not message
|
||||
assert capped.tool_call_id == "call-7"
|
||||
assert "document_id=call-7" in capped.content
|
||||
assert capped.content.startswith("a")
|
||||
# The stored message keeps its body whole.
|
||||
assert message.content == "a" * 50
|
||||
|
||||
|
||||
def test_cap_tool_message_short_untouched() -> None:
|
||||
message = ToolChatMessage(content="tiny", tool_call_id="call-1")
|
||||
assert cap_tool_message(message, 100) is message
|
||||
|
||||
|
||||
def test_truncate_for_request_caps_only_tool_messages() -> None:
|
||||
user = UserChatMessage(content="u" * 1000)
|
||||
tool = ToolChatMessage(content="t" * 1000, tool_call_id="call-1")
|
||||
out = truncate_for_request([user, tool], max_tool_result_chars=200)
|
||||
assert out[0] is user
|
||||
capped = out[1]
|
||||
assert isinstance(capped, ToolChatMessage)
|
||||
assert "document_id=call-1" in capped.content
|
||||
assert len(capped.content) <= 200
|
||||
|
||||
|
||||
def test_estimate_message_chars() -> None:
|
||||
|
||||
Reference in New Issue
Block a user