Compaction/Safeguard: keep multimodal hints in preserved tail

This commit is contained in:
Rodrigo Uroz 2026-02-27 19:11:27 +00:00 committed by Josh Lehman
parent 04f2e405aa
commit b656de6de7
No known key found for this signature in database
GPG Key ID: D141B425AC7F876B
2 changed files with 21 additions and 1 deletions

View File

@ -530,6 +530,22 @@ describe("compaction-safeguard recent-turn preservation", () => {
expect(section).toContain("- Assistant: [non-text content: toolCall]");
});
it("keeps non-text placeholders for mixed-content preserved messages", () => {
const section = formatPreservedTurnsSection([
{
role: "user",
content: [
{ type: "text", text: "caption text" },
{ type: "image", data: "abc", mimeType: "image/png" },
],
timestamp: 1,
} as unknown as AgentMessage,
]);
expect(section).toContain("- User: caption text");
expect(section).toContain("[non-text content: image]");
});
it("clamps preserve count into a safe range", () => {
expect(resolveRecentTurnsPreserve(undefined)).toBe(3);
expect(resolveRecentTurnsPreserve(-1)).toBe(0);

View File

@ -201,6 +201,9 @@ function formatNonTextPlaceholder(content: unknown): string | null {
if (content === null || content === undefined) {
return null;
}
if (typeof content === "string") {
return null;
}
if (!Array.isArray(content)) {
return "[non-text content]";
}
@ -355,7 +358,8 @@ function formatPreservedTurnsSection(messages: AgentMessage[]): string {
const nonTextPlaceholder = formatNonTextPlaceholder(
(message as { content?: unknown }).content,
);
const rendered = text || nonTextPlaceholder;
const rendered =
text && nonTextPlaceholder ? `${text}\n${nonTextPlaceholder}` : text || nonTextPlaceholder;
if (!rendered) {
return null;
}