Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): keep pixel-rejected images off the history byte budget

xpeng5278-webbfc69c73ac27
diff --git a/docs/spec/03-runtime/01-ipc-protocol.md b/docs/spec/03-runtime/01-ipc-protocol.md
index 1f0ff282f..0cb013d5d 100644
--- a/docs/spec/03-runtime/01-ipc-protocol.md
+++ b/docs/spec/03-runtime/01-ipc-protocol.md
@@ -228,7 +228,9 @@ For a vision-capable model, images within the 10 MB per-image bound become
transient pi-ai image blocks. Restored history also has a 30 MB aggregate raw
image-byte budget: the sidecar considers persisted attachments newest-first and
preserves every eligible image when the total fits. If the budget is exceeded,
-older images use the existing safe `@path` fallback. Unknown/custom models
+older images use the existing safe `@path` fallback. An image rejected for a
+longest edge above 8000 px does not consume that budget, so an older image
+that still fits stays inlined. Unknown/custom models
without an explicit image override, non-vision models, oversized images, and
unavailable refs also use the safe fallback. Main uses streamed hashing and file
copying for oversized images. The durable user message stores `content` plus
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index 76cabcdba..4db241e31 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -886,7 +886,8 @@ safety bound, and restored history has a 30 MB aggregate raw-byte budget.
The newest refs are considered first; all eligible images remain image blocks
when the history fits, while over-budget or oversized images become safe
`@path` fallbacks; so do images whose header reports a longest edge above
-8000 px (D675). Every provider request also replaces any image block past that
+8000 px (D675). A pixel-limit rejection does not consume the aggregate byte
+budget, so an older image that still fits is inlined. Every provider request also replaces any image block past that
pixel limit, in user prompts and tool results alike, with a text notice naming
its size, so a session that already holds one recovers. An image whose message names it inline (`inlinePath`) is
spliced back into the prompt at that position, so a restored turn keeps the
diff --git a/packages/agent-runtime/src/attachment-history.test.ts b/packages/agent-runtime/src/attachment-history.test.ts
index c764edbc7..a6c9982d8 100644
--- a/packages/agent-runtime/src/attachment-history.test.ts
+++ b/packages/agent-runtime/src/attachment-history.test.ts
@@ -55,6 +55,52 @@ describe("hydrateAttachmentHistory", () => {
});
});
+ it("does not let a pixel-limit rejection consume the history byte budget", async () => {
+ await withTempDir(async (tmp) => {
+ const olderBytes = Buffer.from("fake-png-bytes");
+ const olderPath = resolve(tmp, "older.png");
+ await writeFile(olderPath, olderBytes);
+ const newerPath = resolve(tmp, "page.png");
+ const header = Buffer.alloc(33);
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13]).copy(header);
+ header.write("IHDR", 12, "latin1");
+ header.writeUInt32BE(780, 16);
+ header.writeUInt32BE(14182, 20);
+ await writeFile(newerPath, header);
+ const history = [
+ userMessage("older", [
+ {
+ name: "older.png",
+ ref: olderPath,
+ kind: "image",
+ mimeType: "image/png",
+ size: olderBytes.length,
+ },
+ ]),
+ userMessage("newer", [
+ {
+ name: "page.png",
+ ref: newerPath,
+ kind: "image",
+ mimeType: "image/png",
+ size: header.length,
+ },
+ ]),
+ ];
+
+ const hydrated = await hydrateAttachmentHistory(history, {
+ projectPath: tmp,
+ supportsVision: true,
+ // Fits either image alone, not both. The newer one is over 8000 px.
+ maxInlinedImageBytes: 40,
+ });
+
+ expect(hydrated[1]?.attachments?.[0]?.data).toBeUndefined();
+ expect(hydrated[1]?.content).toContain("page.png");
+ expect(hydrated[0]?.attachments?.[0]?.data).toBe(olderBytes.toString("base64"));
+ });
+ });
+
it("keeps the path fallback for an image past the pixel limit (#1913)", async () => {
await withTempDir(async (tmp) => {
const imgPath = resolve(tmp, "page.png");
diff --git a/packages/agent-runtime/src/attachment-history.ts b/packages/agent-runtime/src/attachment-history.ts
index d7a332bc4..822220552 100644
--- a/packages/agent-runtime/src/attachment-history.ts
+++ b/packages/agent-runtime/src/attachment-history.ts
@@ -179,46 +179,38 @@ export async function hydrateAttachmentHistory(
}),
);
- // Allocate the byte budget in reverse transcript order, then reverse attachment
- // order within a message. This preserves every image when the history fits while
- // keeping the most recent visual context when the aggregate exceeds the cap.
+ // Newest transcript first, then newest attachment within a message. Charge
+ // the byte budget only after the pixel probe accepts the file. A tall
+ // screenshot keeps the path fallback (#1913) and must not starve an older
+ // image that still fits. Images past the remaining budget are not read.
let remainingBytes = maxInlinedImageBytes;
- const selectedForInlining: ResolvedAttachment[] = [];
+ const inlineCandidates: ResolvedAttachment[] = [];
for (let messageIndex = resolvedHistory.length - 1; messageIndex >= 0; messageIndex--) {
const resolved = resolvedHistory[messageIndex];
if (!resolved) continue;
for (let attachmentIndex = resolved.length - 1; attachmentIndex >= 0; attachmentIndex--) {
const item = resolved[attachmentIndex];
- if (
- !item?.canonicalPath ||
- item.size === undefined ||
- item.size > remainingBytes
- ) {
- continue;
- }
- remainingBytes -= item.size;
- selectedForInlining.push(item);
+ if (!item?.canonicalPath || item.size === undefined) continue;
+ inlineCandidates.push(item);
}
}
- await Promise.all(
- selectedForInlining.map(async (item) => {
- try {
- const bytes = await readFileAtRecordedSize(item.canonicalPath!, item.size!);
- if (!bytes) return;
- // An image past the provider's pixel limit keeps the path fallback, as
- // a fresh prompt does, instead of failing every replay (#1913).
- if (imageExceedsInlineDimensions(probeImageDimensions(bytes))) return;
- item.attachment = {
- ...item.attachment,
- data: bytes.toString("base64"),
- };
- item.inlined = true;
- } catch {
- // A failed transient read should not prevent the remaining history restoring.
- }
- }),
- );
+ for (const item of inlineCandidates) {
+ if (item.size === undefined || item.size > remainingBytes) continue;
+ try {
+ const bytes = await readFileAtRecordedSize(item.canonicalPath!, item.size);
+ if (!bytes) continue;
+ if (imageExceedsInlineDimensions(probeImageDimensions(bytes))) continue;
+ remainingBytes -= item.size;
+ item.attachment = {
+ ...item.attachment,
+ data: bytes.toString("base64"),
+ };
+ item.inlined = true;
+ } catch {
+ // A failed transient read should not prevent the remaining history restoring.
+ }
+ }
const fallbackTasks: Promise<void>[] = [];
for (const resolved of resolvedHistory) {