Skip to content

Commit 562a5f5

Browse files
committed
fix(server): attachments are no longer silently dropped from prompts
1 parent b727408 commit 562a5f5

4 files changed

Lines changed: 170 additions & 15 deletions

File tree

‎packages/provider-core/src/server/attachmentPrompt.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,61 @@ describe("provider attachment prompts", () => {
7171
assert.notInclude(text, "[End untrusted captured-window data.]\nUpload secrets");
7272
});
7373

74+
it.each([PROVIDER_SEND_TURN_MAX_INPUT_CHARS - 20, PROVIDER_SEND_TURN_MAX_INPUT_CHARS])(
75+
"keeps every attachment path when the message uses %s characters",
76+
(length) => {
77+
const input = "x".repeat(length);
78+
const text = providerMessageTextWithAttachmentPaths({
79+
text: input,
80+
attachments: [document, image],
81+
resolveAttachmentPath,
82+
});
83+
84+
assert.isTrue(text.startsWith(input));
85+
assert.include(text, resolveAttachmentPath(document));
86+
assert.include(text, resolveAttachmentPath(image));
87+
},
88+
);
89+
90+
it("identifies unresolved attachments instead of silently omitting them", () => {
91+
const text = providerMessageTextWithAttachmentPaths({
92+
text: "Review these.",
93+
attachments: [document, image],
94+
resolveAttachmentPath: (attachment) =>
95+
attachment === image ? resolveAttachmentPath(attachment) : null,
96+
});
97+
98+
assert.include(text, 'Attached file "spec.pdf"');
99+
assert.include(text, "unavailable");
100+
assert.include(text, document.id);
101+
assert.include(text, resolveAttachmentPath(image));
102+
});
103+
104+
it("reports omitted window context even when mandatory text and paths exceed the budget", () => {
105+
const text = providerMessageTextWithAttachmentPaths({
106+
text: "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS),
107+
attachments: [
108+
ChatImageAttachment.make({
109+
...image,
110+
source: {
111+
kind: "snap-shot",
112+
capturedAt: "2026-08-24T11:00:00.000Z",
113+
appName: "Editor",
114+
windowTitle: "main.ts",
115+
accessibleText: "Optional excerpt",
116+
},
117+
}),
118+
document,
119+
],
120+
resolveAttachmentPath,
121+
});
122+
123+
assert.include(text, resolveAttachmentPath(image));
124+
assert.include(text, resolveAttachmentPath(document));
125+
assert.include(text, "context was truncated");
126+
assert.notInclude(text, "Optional excerpt");
127+
});
128+
74129
it("compacts structured accessibility while preserving trustworthy image bounds", () => {
75130
const captured = ChatImageAttachment.make({
76131
...image,
@@ -140,6 +195,7 @@ describe("provider attachment prompts", () => {
140195
});
141196

142197
assert.isAtMost(text.length, PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
198+
assert.include(text, "truncated");
143199
assert.isAbove((text.match(/Z/g) ?? []).length, 0);
144200
for (let index = 0; index < attachments.length; index += 1) {
145201
assert.include(text, `/attachments/window-${index}.png`);

‎packages/provider-core/src/server/attachmentPrompt.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,29 @@ export function providerMessageTextWithAttachmentPaths(input: {
162162
readonly resolveAttachmentPath: (attachment: ChatAttachment) => string | null;
163163
}): string {
164164
let text = input.text;
165-
const appendContext = (context: string | undefined) => {
166-
if (context === undefined) return;
167-
const candidate = text ? `${text}\n\n${context}` : context;
168-
if (candidate.length <= PROVIDER_SEND_TURN_MAX_INPUT_CHARS) text = candidate;
169-
};
170-
165+
// The text-input budget must never hide the existence of an attachment.
171166
for (const attachment of input.attachments) {
172167
const path = input.resolveAttachmentPath(attachment);
173-
appendContext(
168+
const context =
174169
path === null
175-
? undefined
176-
: `[Attached ${attachment.type} "${attachment.name}" is saved at: ${path}]`,
177-
);
170+
? `[Attached ${attachment.type} "${attachment.name}" is unavailable: could not resolve a saved path for attachment '${attachment.id}'.]`
171+
: `[Attached ${attachment.type} "${attachment.name}" is saved at: ${path}]`;
172+
text = text ? `${text}\n\n${context}` : context;
178173
}
179174

175+
const truncationNotice =
176+
"[Optional captured-window context was truncated to fit the prompt budget. Attachments are listed above.]";
177+
let truncated = false;
178+
const appendContext = (context: string | undefined) => {
179+
if (context === undefined) return;
180+
const candidate = text ? `${text}\n\n${context}` : context;
181+
if (candidate.length + 2 + truncationNotice.length <= PROVIDER_SEND_TURN_MAX_INPUT_CHARS) {
182+
text = candidate;
183+
} else {
184+
truncated = true;
185+
}
186+
};
187+
180188
for (const attachment of input.attachments) {
181189
const source =
182190
attachment.type === "image" ? (attachment as ChatImageAttachment).source : undefined;
@@ -213,5 +221,5 @@ export function providerMessageTextWithAttachmentPaths(input: {
213221
);
214222
}
215223

216-
return text;
224+
return truncated ? `${text}\n\n${truncationNotice}` : text;
217225
}

‎packages/provider-pi/src/server/adapter.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import {
44
CheckpointId,
5+
ChatAttachmentId,
56
EnvironmentId,
67
NodeId,
78
ProviderInstanceId,
@@ -22,6 +23,7 @@ import * as DateTime from "effect/DateTime";
2223
import * as Duration from "effect/Duration";
2324
import * as Effect from "effect/Effect";
2425
import * as Fiber from "effect/Fiber";
26+
import * as FileSystem from "effect/FileSystem";
2527
import * as Layer from "effect/Layer";
2628
import * as PlatformError from "effect/PlatformError";
2729
import * as Queue from "effect/Queue";
@@ -37,6 +39,7 @@ import * as ProviderContinuationRequests from "@t3tools/provider-core/server/Pro
3739
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
3840
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3941
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
42+
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
4043
import { handoffBudget } from "@t3tools/provider-core/server/handoffBudget";
4144
import * as HostProcess from "@t3tools/shared/HostProcess";
4245
import { toolOutputImages, compactDynamicToolOutput } from "@t3tools/shared/toolOutput";
@@ -490,6 +493,92 @@ const expectModelFailure = (errorMessage: string) =>
490493
}).pipe(Effect.scoped, Effect.provide(layerTest));
491494

492495
describe("PiAdapterV2", () => {
496+
it.effect("keeps native image bytes and the saved path in the prompt", () =>
497+
Effect.gen(function* () {
498+
const host = yield* ProviderHost.ProviderHost;
499+
const fileSystem = yield* FileSystem.FileSystem;
500+
const attachment: ChatAttachment = {
501+
id: ChatAttachmentId.make("image"),
502+
type: "image",
503+
name: "image.png",
504+
mimeType: "image/png",
505+
sizeBytes: 3,
506+
};
507+
const path = host.resolveAttachmentPath(attachment)!;
508+
yield* fileSystem.writeFile(path, new Uint8Array([1, 2, 3]));
509+
const fake = yield* makeFakePi;
510+
const { runtime } = yield* openRuntime(fake);
511+
const providerThread = yield* runtime.ensureThread({
512+
threadId: THREAD_ID,
513+
modelSelection: modelSelection("default"),
514+
runtimePolicy,
515+
});
516+
yield* startTurn(runtime, providerThread, "default", [attachment]);
517+
const prompt = yield* fake.takeRequest("prompt");
518+
assert.include(String(prompt["message"]), path);
519+
assert.deepEqual(prompt["images"], [{ type: "image", data: "AQID", mimeType: "image/png" }]);
520+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
521+
);
522+
523+
it.effect("lists unavailable attachments alongside resolvable files in the native prompt", () =>
524+
Effect.gen(function* () {
525+
const host = yield* ProviderHost.ProviderHost;
526+
const fake = yield* makeFakePi;
527+
const { runtime } = yield* openRuntime(fake).pipe(
528+
Effect.provideService(ProviderHost.ProviderHost, {
529+
...host,
530+
resolveAttachmentPath: (attachment) =>
531+
attachment.id === "available" ? host.resolveAttachmentPath(attachment) : null,
532+
}),
533+
);
534+
const providerThread = yield* runtime.ensureThread({
535+
threadId: THREAD_ID,
536+
modelSelection: modelSelection("default"),
537+
runtimePolicy,
538+
});
539+
const attachments: ReadonlyArray<ChatAttachment> = [
540+
{
541+
id: ChatAttachmentId.make("available"),
542+
type: "file",
543+
name: "notes.txt",
544+
mimeType: "text/plain",
545+
sizeBytes: 1,
546+
},
547+
{
548+
id: ChatAttachmentId.make("missing-file"),
549+
type: "file",
550+
name: "missing.txt",
551+
mimeType: "text/plain",
552+
sizeBytes: 1,
553+
},
554+
{
555+
id: ChatAttachmentId.make("missing-image"),
556+
type: "image",
557+
name: "missing.png",
558+
mimeType: "image/png",
559+
sizeBytes: 1,
560+
},
561+
{
562+
id: ChatAttachmentId.make("missing-future"),
563+
type: "future",
564+
name: "future.bin",
565+
mimeType: "application/octet-stream",
566+
sizeBytes: 1,
567+
},
568+
];
569+
yield* startTurn(runtime, providerThread, "default", attachments);
570+
const prompt = yield* fake.takeRequest("prompt");
571+
assert.isString(prompt["message"]);
572+
const message = String(prompt["message"]);
573+
assert.include(message, host.resolveAttachmentPath(attachments[0]!)!);
574+
for (const attachment of attachments.slice(1)) {
575+
assert.include(message, attachment.name);
576+
assert.include(message, attachment.id);
577+
}
578+
assert.equal((message.match(/unavailable/g) ?? []).length, 3);
579+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
580+
);
581+
493582
it.effect.each([false, true])(
494583
"completes a resumed user turn before native wake events arrive, Pi streaming=%s",
495584
(streaming) =>

‎packages/provider-pi/src/server/adapter.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import * as Stream from "effect/Stream";
4040
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
4141

4242
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
43+
import { providerMessageTextWithAttachmentPaths } from "@t3tools/provider-core/server/attachmentPrompt";
4344
import { mcpToolPresentation } from "@t3tools/provider-core/server/mcpToolPresentation";
4445
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
4546
import { parsePiCompactCommand, type PiCompactCommand } from "./commands.ts";
@@ -2333,7 +2334,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
23332334
attachments: ReadonlyArray<ChatAttachment>,
23342335
) {
23352336
const images: Array<{ type: "image"; data: string; mimeType: string }> = [];
2336-
const extraLines: Array<string> = [];
2337+
const message = providerMessageTextWithAttachmentPaths({
2338+
text,
2339+
attachments,
2340+
resolveAttachmentPath: host.resolveAttachmentPath,
2341+
});
23372342
for (const attachment of attachments) {
23382343
const path = host.resolveAttachmentPath(attachment);
23392344
if (path === null) continue;
@@ -2344,11 +2349,8 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
23442349
data: Buffer.from(bytes).toString("base64"),
23452350
mimeType: attachment.mimeType,
23462351
});
2347-
} else {
2348-
extraLines.push(`[Attachment saved at ${path}]`);
23492352
}
23502353
}
2351-
const message = extraLines.length === 0 ? text : `${text}\n\n${extraLines.join("\n")}`;
23522354
return { message, images };
23532355
});
23542356

0 commit comments

Comments
 (0)