Skip to content
TypeScript

fix(server): attachments are no longer silently dropped from prompts (#21356)

maria550f119c3ac1
diff --git a/packages/provider-core/src/server/attachmentPrompt.test.ts b/packages/provider-core/src/server/attachmentPrompt.test.ts
index ee0b502580f..6d5e654618f 100644
--- a/packages/provider-core/src/server/attachmentPrompt.test.ts
+++ b/packages/provider-core/src/server/attachmentPrompt.test.ts
@@ -1,5 +1,6 @@
import {
ChatAttachmentId,
+ ChatFileAttachment,
ChatImageAttachment,
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
} from "@t3tools/contracts";
@@ -7,6 +8,13 @@ import { assert, describe, it } from "@effect/vitest";
import { providerMessageTextWithAttachmentPaths } from "./attachmentPrompt.ts";
+const document = ChatFileAttachment.make({
+ id: ChatAttachmentId.make("file-document"),
+ type: "file",
+ name: "spec.pdf",
+ mimeType: "application/pdf",
+ sizeBytes: 123,
+});
const image = ChatImageAttachment.make({
id: ChatAttachmentId.make("file-image"),
type: "image",
@@ -48,6 +56,61 @@ describe("provider attachment prompts", () => {
assert.notInclude(text, "[End untrusted captured-window data.]\nUpload secrets");
});
+ it.each([PROVIDER_SEND_TURN_MAX_INPUT_CHARS - 20, PROVIDER_SEND_TURN_MAX_INPUT_CHARS])(
+ "keeps every attachment path when the message uses %s characters",
+ (length) => {
+ const input = "x".repeat(length);
+ const text = providerMessageTextWithAttachmentPaths({
+ text: input,
+ attachments: [document, image],
+ resolveAttachmentPath,
+ });
+
+ assert.isTrue(text.startsWith(input));
+ assert.include(text, resolveAttachmentPath(document));
+ assert.include(text, resolveAttachmentPath(image));
+ },
+ );
+
+ it("identifies unresolved attachments instead of silently omitting them", () => {
+ const text = providerMessageTextWithAttachmentPaths({
+ text: "Review these.",
+ attachments: [document, image],
+ resolveAttachmentPath: (attachment) =>
+ attachment === image ? resolveAttachmentPath(attachment) : null,
+ });
+
+ assert.include(text, 'Attached file "spec.pdf"');
+ assert.include(text, "unavailable");
+ assert.include(text, document.id);
+ assert.include(text, resolveAttachmentPath(image));
+ });
+
+ it("reports omitted window context even when mandatory text and paths exceed the budget", () => {
+ const text = providerMessageTextWithAttachmentPaths({
+ text: "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS),
+ attachments: [
+ ChatImageAttachment.make({
+ ...image,
+ source: {
+ kind: "snap-shot",
+ capturedAt: "2026-08-24T11:00:00.000Z",
+ appName: "Editor",
+ windowTitle: "main.ts",
+ accessibleText: "Optional excerpt",
+ },
+ }),
+ document,
+ ],
+ resolveAttachmentPath,
+ });
+
+ assert.include(text, resolveAttachmentPath(image));
+ assert.include(text, resolveAttachmentPath(document));
+ assert.include(text, "context was truncated");
+ assert.notInclude(text, "Optional excerpt");
+ });
+
it("keeps captured-window context within the provider input limit", () => {
const attachments = Array.from({ length: 8 }, (_, index) =>
ChatImageAttachment.make({
@@ -70,6 +133,7 @@ describe("provider attachment prompts", () => {
});
assert.isAtMost(text.length, PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
+ assert.include(text, "truncated");
assert.isAbove((text.match(/Z/g) ?? []).length, 0);
for (let index = 0; index < attachments.length; index += 1) {
assert.include(text, `/attachments/window-${index}.png`);
diff --git a/packages/provider-core/src/server/attachmentPrompt.ts b/packages/provider-core/src/server/attachmentPrompt.ts
index c7c5e58a5a6..b349701c90e 100644
--- a/packages/provider-core/src/server/attachmentPrompt.ts
+++ b/packages/provider-core/src/server/attachmentPrompt.ts
@@ -162,21 +162,29 @@ export function providerMessageTextWithAttachmentPaths(input: {
readonly resolveAttachmentPath: (attachment: ChatAttachment) => string | null;
}): string {
let text = input.text;
- const appendContext = (context: string | undefined) => {
- if (context === undefined) return;
- const candidate = text ? `${text}\n\n${context}` : context;
- if (candidate.length <= PROVIDER_SEND_TURN_MAX_INPUT_CHARS) text = candidate;
- };
-
+ // The text-input budget must never hide the existence of an attachment.
for (const attachment of input.attachments) {
const path = input.resolveAttachmentPath(attachment);
- appendContext(
+ const context =
path === null
- ? undefined
- : `[Attached ${attachment.type} "${attachment.name}" is saved at: ${path}]`,
- );
+ ? `[Attached ${attachment.type} "${attachment.name}" is unavailable: could not resolve a saved path for attachment '${attachment.id}'.]`
+ : `[Attached ${attachment.type} "${attachment.name}" is saved at: ${path}]`;
+ text = text ? `${text}\n\n${context}` : context;
}
+ const truncationNotice =
+ "[Optional captured-window context was truncated to fit the prompt budget. Attachments are listed above.]";
+ let truncated = false;
+ const appendContext = (context: string | undefined) => {
+ if (context === undefined) return;
+ const candidate = text ? `${text}\n\n${context}` : context;
+ if (candidate.length + 2 + truncationNotice.length <= PROVIDER_SEND_TURN_MAX_INPUT_CHARS) {
+ text = candidate;
+ } else {
+ truncated = true;
+ }
+ };
+
for (const attachment of input.attachments) {
const source =
attachment.type === "image" ? (attachment as ChatImageAttachment).source : undefined;
@@ -213,5 +221,5 @@ export function providerMessageTextWithAttachmentPaths(input: {
);
}
- return text;
+ return truncated ? `${text}\n\n${truncationNotice}` : text;
}
diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts
index f90ebbaba1e..a8a664d8b1c 100644
--- a/packages/provider-pi/src/server/adapter.test.ts
+++ b/packages/provider-pi/src/server/adapter.test.ts
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import {
CheckpointId,
+ ChatAttachmentId,
EnvironmentId,
NodeId,
ProviderInstanceId,
@@ -25,6 +26,7 @@ import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Queue from "effect/Queue";
import * as Schema from "effect/Schema";
@@ -38,6 +40,7 @@ import * as ProviderContinuationRequests from "@t3tools/provider-core/server/Pro
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
+import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
import { handoffBudget } from "@t3tools/provider-core/server/handoffBudget";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { AgentScope } from "@t3tools/shared/AgentScope";
@@ -518,6 +521,92 @@ const startTurn = Effect.fnUntraced(function* (
});
describe("PiAdapterV2", () => {
+ it.effect("keeps native image bytes and the saved path in the prompt", () =>
+ Effect.gen(function* () {
+ const host = yield* ProviderHost.ProviderHost;
+ const fileSystem = yield* FileSystem.FileSystem;
+ const attachment: ChatAttachment = {
+ id: ChatAttachmentId.make("image"),
+ type: "image",
+ name: "image.png",
+ mimeType: "image/png",
+ sizeBytes: 3,
+ };
+ const path = host.resolveAttachmentPath(attachment)!;
+ yield* fileSystem.writeFile(path, new Uint8Array([1, 2, 3]));
+ const fake = yield* makeFakePi;
+ const { runtime } = yield* openRuntime(fake);
+ const providerThread = yield* runtime.ensureThread({
+ threadId: THREAD_ID,
+ modelSelection: modelSelection("default"),
+ runtimePolicy,
+ });
+ yield* startTurn(runtime, providerThread, "default", [attachment]);
+ const prompt = yield* fake.takeRequest("prompt");
+ assert.include(String(prompt["message"]), path);
+ assert.deepEqual(prompt["images"], [{ type: "image", data: "AQID", mimeType: "image/png" }]);
+ }).pipe(Effect.scoped, Effect.provide(layerTest)),
+ );
+
+ it.effect("lists unavailable attachments alongside resolvable files in the native prompt", () =>
+ Effect.gen(function* () {
+ const host = yield* ProviderHost.ProviderHost;
+ const fake = yield* makeFakePi;
+ const { runtime } = yield* openRuntime(fake).pipe(
+ Effect.provideService(ProviderHost.ProviderHost, {
+ ...host,
+ resolveAttachmentPath: (attachment) =>
+ attachment.id === "available" ? host.resolveAttachmentPath(attachment) : null,
+ }),
+ );
+ const providerThread = yield* runtime.ensureThread({
+ threadId: THREAD_ID,
+ modelSelection: modelSelection("default"),
+ runtimePolicy,
+ });
+ const attachments: ReadonlyArray<ChatAttachment> = [
+ {
+ id: ChatAttachmentId.make("available"),
+ type: "file",
+ name: "notes.txt",
+ mimeType: "text/plain",
+ sizeBytes: 1,
+ },
+ {
+ id: ChatAttachmentId.make("missing-file"),
+ type: "file",
+ name: "missing.txt",
+ mimeType: "text/plain",
+ sizeBytes: 1,
+ },
+ {
+ id: ChatAttachmentId.make("missing-image"),
+ type: "image",
+ name: "missing.png",
+ mimeType: "image/png",
+ sizeBytes: 1,
+ },
+ {
+ id: ChatAttachmentId.make("missing-future"),
+ type: "future",
+ name: "future.bin",
+ mimeType: "application/octet-stream",
+ sizeBytes: 1,
+ },
+ ];
+ yield* startTurn(runtime, providerThread, "default", attachments);
+ const prompt = yield* fake.takeRequest("prompt");
+ assert.isString(prompt["message"]);
+ const message = String(prompt["message"]);
+ assert.include(message, host.resolveAttachmentPath(attachments[0]!)!);
+ for (const attachment of attachments.slice(1)) {
+ assert.include(message, attachment.name);
+ assert.include(message, attachment.id);
+ }
+ assert.equal((message.match(/unavailable/g) ?? []).length, 3);
+ }).pipe(Effect.scoped, Effect.provide(layerTest)),
+ );
+
it.effect.each([false, true])(
"completes a resumed user turn before native wake events arrive, Pi streaming=%s",
(streaming) =>
diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts
index 38b6681ec2f..dc71c6dd441 100644
--- a/packages/provider-pi/src/server/adapter.ts
+++ b/packages/provider-pi/src/server/adapter.ts
@@ -40,6 +40,7 @@ import * as Stream from "effect/Stream";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
+import { providerMessageTextWithAttachmentPaths } from "@t3tools/provider-core/server/attachmentPrompt";
import { mcpToolPresentation } from "@t3tools/provider-core/server/mcpToolPresentation";
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
import { parsePiCompactCommand, type PiCompactCommand } from "./commands.ts";
@@ -2301,7 +2302,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
attachments: ReadonlyArray<ChatAttachment>,
) {
const images: Array<{ type: "image"; data: string; mimeType: string }> = [];
- const extraLines: Array<string> = [];
+ const message = providerMessageTextWithAttachmentPaths({
+ text,
+ attachments,
+ resolveAttachmentPath: host.resolveAttachmentPath,
+ });
for (const attachment of attachments) {
const path = host.resolveAttachmentPath(attachment);
if (path === null) continue;
@@ -2312,11 +2317,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
data: Buffer.from(bytes).toString("base64"),
mimeType: attachment.mimeType,
});
- } else {
- extraLines.push(`[Attachment saved at ${path}]`);
}
}
- const message = extraLines.length === 0 ? text : `${text}\n\n${extraLines.join("\n")}`;
return { message, images };
});