The Open Framework for Building Agents
test(eve): reproduce overwritten staged attachments reaching the model (#4288)
e0-gh-vercel-connect[bot]f234b513067f
diff --git a/.changeset/staged-attachment-integrity.md b/.changeset/staged-attachment-integrity.mdnew file mode 100644index 000000000..4c00bb8f5--- /dev/null+++ b/.changeset/staged-attachment-integrity.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Staged attachments are now checked against their content address before each model call. If code in the sandbox overwrote a staged file, the model gets a `FileNotFound` note instead of the replaced bytes posing as the original upload.diff --git a/docs/sandbox/index.mdx b/docs/sandbox/index.mdxindex 62e8595ff..377859c6b 100644--- a/docs/sandbox/index.mdx+++ b/docs/sandbox/index.mdx@@ -158,6 +158,8 @@ Before every model call, eve reads the referenced files back from the sandbox:- An image up to 3 MiB or a PDF up to 20 MiB attached to a message reaches the model as a file. Larger files and other types reach the model as a text reference, such as `Attached file /workspace/.eve/attachments/<hash>/report.csv (text/csv)`, that the agent can open with `read_file` or `bash`.- A file returned by a tool always reaches the model as a file.+If a referenced file is missing, or its bytes no longer match what eve staged because something in the sandbox overwrote it, the model receives a `FileNotFound` note in its place.+Each call sends the same bytes for the same file, so the model still sees attachments from earlier turns and the provider's prompt cache stays valid. Compaction replaces files in older tool results with their text reference. The `read_file` tool shows PNG, JPEG, GIF, and WebP files up to 3 MiB to the model as images, so the agent can look at an image again after compaction.## Custom providersdiff --git a/packages/eve/src/harness/attachment-staging.integration.test.ts b/packages/eve/src/harness/attachment-staging.integration.test.tsindex 7562901dc..9ad7ef35a 100644--- a/packages/eve/src/harness/attachment-staging.integration.test.ts+++ b/packages/eve/src/harness/attachment-staging.integration.test.ts@@ -733,6 +733,38 @@ describe("hydrateSandboxAttachments (integration)", () => {);});+ it("does not inline a staged attachment that sandbox code overwrote (#4284)", async () => {+ const sandbox = mockSandbox({ id: "sbx_overwritten" });+ const runtime = await createTestRuntime();++ const stagedContent = (await runtime.runAsSession({ sandbox }, async () =>+ stageAttachmentsToSandbox([+ {+ data: Buffer.from("original"),+ filename: "logo.png",+ mediaType: "image/png",+ type: "file",+ },+ ] as UserContent),+ )) as UserContent;+ const stagedPath = (stagedContent[0] as FilePart).filename as string;++ // Same size, different bytes: only the content address can tell them apart.+ await sandbox.session.writeBinaryFile({ content: Buffer.from("replaced"), path: stagedPath });++ const messages = [{ content: stagedContent, role: "user" as const }];+ const hydrated = await runtime.runAsSession({ sandbox }, async () =>+ hydrateSandboxAttachments(messages),+ );++ expect(hydrated[0]?.content).toEqual([+ {+ text: `FileNotFound: Current snapshot may be newer and does not contain ${stagedPath}.`,+ type: "text",+ },+ ]);+ });+it("does not touch the sandbox when every ref is non-inlinable — text references carry all the info", async () => {// Regression guard: the non-inlinable path must render the text// reference entirely from ref metadata (path, mediaType, size)diff --git a/packages/eve/src/harness/attachment-staging.ts b/packages/eve/src/harness/attachment-staging.tsindex 3e01e83ff..9bf0ae215 100644--- a/packages/eve/src/harness/attachment-staging.ts+++ b/packages/eve/src/harness/attachment-staging.ts@@ -1,5 +1,5 @@import { createHash } from "node:crypto";-import { basename, extname } from "node:path";+import { basename, dirname, extname } from "node:path";import type { FilePart, ModelMessage, TextPart, ToolResultPart, UserContent } from "ai";import { buildAdapterContext } from "#channel/adapter-context.js";@@ -357,6 +357,17 @@ async function readSandboxRefBytes(path: ref.path,size: ref.size,});+ return null;+ }+ // Sandbox code can overwrite a staged file; only the bytes eve staged may+ // reach the model as the original attachment.+ if (bytes.byteLength !== ref.size || sha256Prefix(bytes) !== basename(dirname(ref.path))) {+ log.warn("sandbox-ref attachment bytes changed since staging — degrading to text reference", {+ mediaType: ref.mediaType,+ path: ref.path,+ size: ref.size,+ });+ return null;}return bytes;}@@ -549,7 +560,7 @@ function reconstitueFilePartUrls(return changed ? result : content;}-function sha256Prefix(bytes: Buffer): string {+function sha256Prefix(bytes: Uint8Array): string {return createHash("sha256").update(bytes).digest("hex").slice(0, SHA_PREFIX_LENGTH);}