Files
hermes-agent/tests/agent/test_native_compaction_nontext_retention.py
huanshan5195 40721bffe5 fix(agent): stamp type=message on every Responses input message item
The Responses input converter emitted bare {"role", "content"} items for user messages and for
the empty assistant "following item" that must trail a replayed reasoning block. OpenAI tolerates
a typeless input item; llama.cpp's server-chat.cpp /v1/responses parser does not — it rejects the
whole request, so a self-hosted Responses endpoint returns an empty "" answer as soon as a turn
replays multi-turn history.

Stamping the type is not enough on its own, because preflight dispatches on it. A stamped user
message stops reaching the untyped handler and lands in _preflight_message, which accepted
role='assistant' with a content *list* only — it would have rejected exactly the items the
converter produces (plain-text messages carry string content, and the reasoning-following item is
""). So the typed handler now takes both roles, accepts string content, types each part for its
role (input_text for user, output_text for assistant), and still stamps `status` only on assistant
items, since status is an assistant *output* field.

Both remaining emitters — the untyped preflight handler and the auxiliary client's empty-input
fallback, which never passes through preflight at all — build their items through the same
_role_message_item() constructor, so the invariant is structural rather than remembered. The
provider-specific `typed_text_only` wrapping main added since is unchanged: the item carries the
type, wire_content still shapes the content.

(cherry picked from commit 19c845ba6060771f8dc140a925a27d8b21ee496d)
2026-10-01 16:18:23 +05:30

96 lines
3.0 KiB
Python

"""Regression coverage for image-only user content across native compaction."""
from agent.codex_responses_adapter import _chat_messages_to_responses_input
from agent.native_compaction import prune_pre_checkpoint_items
_IMAGE_URL = "data:image/png;base64,AAAA"
_CHAT_IMAGE_PART = {"type": "image_url", "image_url": {"url": _IMAGE_URL}}
_RESPONSES_IMAGE_PART = {"type": "input_image", "image_url": _IMAGE_URL}
_CHECKPOINT = {"type": "compaction", "encrypted_content": "blob_cp"}
def test_image_only_user_message_survives_pre_checkpoint_pruning_verbatim():
image_only = {"role": "user", "content": [_RESPONSES_IMAGE_PART]}
items = [
image_only,
_CHECKPOINT,
{"role": "user", "content": "after checkpoint"},
]
pruned = prune_pre_checkpoint_items(items, retained_user_token_budget=1)
assert pruned == [
_CHECKPOINT,
image_only,
{"role": "user", "content": "after checkpoint"},
]
assert pruned[1] is image_only
def test_image_only_user_message_still_obeys_retention_budget():
image_only = {"role": "user", "content": [_RESPONSES_IMAGE_PART]}
assert prune_pre_checkpoint_items(
[image_only, _CHECKPOINT],
retained_user_token_budget=0,
) == [_CHECKPOINT]
def test_malformed_or_unknown_multipart_is_not_retained():
invalid_contents = [
[{}],
[{"type": "input_image", "image_url": ""}],
[{"type": "unknown", "value": "not an attachment"}],
]
for content in invalid_contents:
malformed = {"role": "user", "content": content}
assert prune_pre_checkpoint_items(
[malformed, _CHECKPOINT],
retained_user_token_budget=1,
) == [_CHECKPOINT]
def test_adapter_preserves_image_only_user_message_across_checkpoint():
messages = [
{"role": "user", "content": [_CHAT_IMAGE_PART]},
{
"role": "assistant",
"content": "checkpoint turn",
"codex_reasoning_items": [_CHECKPOINT],
},
{"role": "user", "content": "after checkpoint"},
]
converted = _chat_messages_to_responses_input(
messages,
native_compaction_eligible=True,
)
assert converted == [
_CHECKPOINT,
{"type": "message", "role": "user", "content": [_RESPONSES_IMAGE_PART]},
{"type": "message", "role": "assistant", "content": "checkpoint turn"},
{"type": "message", "role": "user", "content": "after checkpoint"},
]
def test_image_only_user_message_is_retained_with_interleaved_assistant():
image_user = {
"role": "user",
"content": [_RESPONSES_IMAGE_PART],
}
items = [
image_user,
{"role": "assistant", "content": "I see the screenshot."},
_CHECKPOINT,
{"role": "user", "content": "what was in that screenshot?"},
]
out = prune_pre_checkpoint_items(items)
assert out[0] == _CHECKPOINT
assert out[1] == image_user
assert out[2] == {"role": "user", "content": "what was in that screenshot?"}