Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(deepagents): keep media blocks out of tool result eviction
Image, audio and video blocks a tool returns no longer count toward toolTokenLimitBeforeEvict, and they stay on the replacement message when the tool result's text is evicted.

Fixes #947
  • Loading branch information
burrows99 committed Oct 12, 2026
commit 83f2ffbc66d56fc5b6d86c4e51c17edaae4c5f01
5 changes: 5 additions & 0 deletions .changeset/tool-eviction-keeps-media.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"deepagents": patch
---

Fix tool result eviction dropping media: image, audio and video blocks no longer count toward `toolTokenLimitBeforeEvict`, so a screenshot returned by a tool (for example over MCP) no longer forces eviction on its own, and when a tool result's text is evicted its media blocks stay on the replacement message, as they already do for evicted human messages.
70 changes: 70 additions & 0 deletions libs/deepagents/src/middleware/fs.eviction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,73 @@ describe("evicted tool result readback", () => {
expect(readResult[0].text).toContain("large plain text result");
});
});

describe("tool result eviction with media blocks", () => {
beforeEach(() => {
vi.clearAllMocks();
});

const screenshot = {
type: "image_url",
image_url: { url: `data:image/png;base64,${"A".repeat(200_000)}` },
};

async function wrapToolResult(content: ToolMessage["content"]) {
const state: { messages: unknown[]; files: Record<string, FileData> } = {
messages: [],
files: {},
};
vi.mocked(getCurrentTaskInput).mockReturnValue(state);
const middleware = createFilesystemMiddleware({
backend: () => new StateBackend({ state, store: undefined }),
toolTokenLimitBeforeEvict: 1000,
});
return (middleware as any).wrapToolCall(
{
toolCall: { id: "call_screenshot", name: "browser_take_screenshot" },
state,
runtime: {},
},
async () =>
new ToolMessage({
content,
tool_call_id: "call_screenshot",
name: "browser_take_screenshot",
}),
);
}

it("should not count image blocks toward the eviction threshold", async () => {
const result = await wrapToolResult([
{ type: "text", text: "Screenshot" },
screenshot,
]);

expect(isCommand(result)).toBe(false);
expect(result.content.map((b: any) => b.type)).toEqual([
"text",
"image_url",
]);
});

it("should keep image blocks when large text is evicted", async () => {
const largeText = "page text\n".repeat(1000);
const result = await wrapToolResult([
{ type: "text", text: largeText },
screenshot,
]);

expect(isCommand(result)).toBe(true);
const update = (result as any).update;
const evictedPath = "/large_tool_results/call_screenshot.txt";
expect(update.files[evictedPath]).toBeDefined();
expect(JSON.stringify(update.files[evictedPath]).includes("base64")).toBe(
false,
);
const content = update.messages[0].content;
expect(Array.isArray(content)).toBe(true);
expect(content.map((b: any) => b.type)).toEqual(["text", "image_url"]);
expect(content[0].text).toContain(evictedPath);
expect(content[1].image_url.url).toBe(screenshot.image_url.url);
});
});
29 changes: 28 additions & 1 deletion libs/deepagents/src/middleware/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,33 @@ function extractTextFromMessage(message: {
return String(message.content);
}

/**
* Image, audio and video blocks a tool returns (e.g. an MCP screenshot). They
* are kept on an evicted tool message and do not count toward its size, since
* their base64 payload is not text the model could page through.
*/
const TOOL_MEDIA_BLOCK_TYPES: ReadonlySet<unknown> = new Set([
"image",
"image_url",
"audio",
"video",
]);

function isToolMediaBlock(block: unknown): boolean {
return (
typeof block === "object" &&
block !== null &&
TOOL_MEDIA_BLOCK_TYPES.has((block as { type?: unknown }).type)
);
}

function stringifyToolContent(content: unknown): string {
if (typeof content === "string") {
return content;
}
if (Array.isArray(content)) {
return content
.filter((block) => !isToolMediaBlock(block))
.map((block) => {
if (
typeof block === "object" &&
Expand Down Expand Up @@ -2237,8 +2258,14 @@ export function createFilesystemMiddleware(
.replace("{file_path}", evictPath)
.replace("{content_sample}", contentSample);

const mediaBlocks = Array.isArray(msg.content)
? msg.content.filter(isToolMediaBlock)
: [];
const truncatedMessage = new ToolMessage({
content: replacementText,
content:
mediaBlocks.length > 0
? [{ type: "text", text: replacementText }, ...mediaBlocks]
: replacementText,
tool_call_id: msg.tool_call_id,
name: msg.name,
id: msg.id,
Expand Down
Loading