Skip to content

Commit c8514ed

Browse files
authored
fix(gateway): preserve mixed media failure receipts (#128423)
* fix(gateway): preserve mixed media failure receipts Adopted from Microsoft's Lobster patch 0044 via giodl73-repo/lobster-plugins-and-patches (source commit e9f84234ebdf). * chore: drop changelog edit; release generation owns that file
1 parent 6bd6554 commit c8514ed

4 files changed

Lines changed: 79 additions & 8 deletions

File tree

‎docs/web/webchat.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Status: the macOS/iOS SwiftUI chat UI talks directly to the Gateway WebSocket. N
3333
- Replying to a specific message (right-click → Reply) sends the target's transcript id as `replyToId` on `chat.send`. The Gateway resolves that message from session history and hydrates the same channel-agnostic reply context metadata Discord replies use: agents see `has_reply_context` plus the untrusted "Reply target of current user message" block with sender label and body. (Webchat prompts keep volatile conversation ids such as `reply_to_id` suppressed, per the existing byte-stable prompt policy for direct webchat sessions.) Reply targets without a persisted transcript id (for example pending sends) fall back to an inline quote in the message body.
3434
- Workspace startup files and pending `BOOTSTRAP.md` instructions are supplied through the agent system prompt's `# Project Context` section, not copied into the WebChat user message. If bootstrap content is truncated, the system prompt gets a short "Bootstrap Context Notice" instead; detailed counts and config knobs stay on diagnostic surfaces.
3535
- Display normalization on `chat.history` strips: runtime-only OpenClaw context, inbound envelope wrappers, inline delivery directive tags such as `[[reply_to_current]]`, `[[reply_to:<id>]]`, and `[[audio_as_voice]]`, plain-text tool-call XML payloads (`<tool_call>`, `<function_call>`, `<tool_calls>`, `<function_calls>`, including truncated blocks), and leaked ASCII/full-width model control tokens. Assistant entries whose whole visible text is only the silent token `NO_REPLY` (case-insensitive) are omitted.
36+
- When a reply attachment cannot be read or prepared, WebChat preserves any deliverable attachments and shows one short failure warning without exposing local filesystem paths.
3637
- Reasoning-flagged reply payloads (`isReasoning: true`) are excluded from WebChat assistant content, transcript replay text, and audio content blocks, so thinking-only payloads do not surface as visible assistant messages or playable audio.
3738
- `chat.inject` appends an assistant note directly to the transcript and broadcasts it to the UI (no agent run).
3839
- Aborted runs can keep partial assistant output visible in the UI. Gateway persists that partial text into transcript history when buffered output exists, and marks the entry with abort metadata.

‎src/channels/plugins/contracts/outbound-payload-testkit.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,22 @@ export function installChannelOutboundPayloadContractSuite(params: {
5555
resetGlobalHookRunner();
5656
});
5757

58-
it("text-only delegates to sendText", async () => {
58+
it.each([
59+
{ label: "plain text", text: "hello" },
60+
{
61+
label: "a bounded media failure receipt",
62+
text: "⚠️ Media failed. Try sending a smaller supported file or a different format.",
63+
},
64+
])("$label delegates to sendText", async ({ text }) => {
5965
const { run, sendMock, to } = await params.createHarness({
60-
payload: { text: "hello" },
66+
payload: { text },
6167
});
6268
const result = await run();
6369

6470
expect(sendMock).toHaveBeenCalledTimes(1);
6571
const call = sendCall(sendMock, 0);
6672
expect(call[0]).toBe(to);
67-
expect(call[1]).toBe("hello");
73+
expect(call[1]).toBe(text);
6874
expect(call[2]).toBeDefined();
6975
expect(result.channel).toBe(params.channel);
7076
});

‎src/gateway/server-methods/chat-reply-media.test.ts‎

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,78 @@ describe("normalizeWebchatReplyMediaPathsForDisplay", () => {
439439
expect(blocks).toHaveLength(2);
440440
});
441441

442-
it("does not add a failure warning when a mixed inline image survives", async () => {
442+
it.each([
443+
{
444+
label: "before the inline image",
445+
mediaUrls: (imagePath: string, dataUrl: string) => [imagePath, dataUrl],
446+
},
447+
{
448+
label: "after the inline image",
449+
mediaUrls: (imagePath: string, dataUrl: string) => [dataUrl, imagePath],
450+
},
451+
])("keeps a sanitized failure receipt when unreadable media is $label", async ({ mediaUrls }) => {
443452
const dataUrl = dataImageUrl();
444-
const { stateDir, payload } = await normalizeCodexHomeImage({
453+
const { stateDir, sourcePath, payload } = await normalizeCodexHomeImage({
445454
allowRead: false,
446-
payload: (imagePath) => ({ mediaUrls: [imagePath, dataUrl] }),
455+
payload: (imagePath) => ({ mediaUrls: mediaUrls(imagePath, dataUrl) }),
447456
});
448457

449-
expect(payload?.text).toBeUndefined();
458+
expect(payload?.text).toBe(
459+
"⚠️ Media failed. Try sending a smaller supported file or a different format.",
460+
);
461+
expect(payload?.text).not.toContain(sourcePath);
462+
expect(Buffer.byteLength(payload?.text ?? "")).toBeLessThan(256);
450463
expect(payload?.mediaUrl).toBe(dataUrl);
451464
expect(payload?.mediaUrls).toEqual([dataUrl]);
452465
await expectOutboundMediaMissing(stateDir);
453466
});
467+
468+
it.each([
469+
{
470+
label: "a missing attachment before the staged attachment",
471+
mediaUrls: (missingPath: string, imagePath: string, dataUrl: string) => [
472+
missingPath,
473+
imagePath,
474+
dataUrl,
475+
],
476+
},
477+
{
478+
label: "a missing attachment after the staged attachment",
479+
mediaUrls: (missingPath: string, imagePath: string, dataUrl: string) => [
480+
imagePath,
481+
missingPath,
482+
dataUrl,
483+
],
484+
},
485+
{
486+
label: "multiple missing attachments around surviving media",
487+
mediaUrls: (missingPath: string, imagePath: string, dataUrl: string) => [
488+
missingPath,
489+
imagePath,
490+
dataUrl,
491+
path.join(path.dirname(imagePath), "private customer report.png"),
492+
],
493+
},
494+
])("keeps exactly one failure receipt for $label", async ({ mediaUrls }) => {
495+
const dataUrl = dataImageUrl();
496+
const { stateDir, sourcePath, payload } = await normalizeCodexHomeImage({
497+
allowRead: true,
498+
payload: (imagePath) => ({
499+
text: "Here is the surviving attachment",
500+
mediaUrls: mediaUrls(path.join(path.dirname(imagePath), "missing.png"), imagePath, dataUrl),
501+
}),
502+
});
503+
const normalizedLocalPath = requireString(payload?.mediaUrls?.[0], "normalized local media");
504+
505+
expect(payload?.text).toBe(
506+
"Here is the surviving attachment\n⚠️ Media failed. Try sending a smaller supported file or a different format.",
507+
);
508+
expect(payload?.text).not.toContain(sourcePath);
509+
expect(payload?.text).not.toContain("private customer report.png");
510+
expect(Buffer.byteLength(payload?.text ?? "")).toBeLessThan(256);
511+
expect(payload?.mediaUrl).toBe(normalizedLocalPath);
512+
expect(payload?.mediaUrls).toEqual([normalizedLocalPath, dataUrl]);
513+
expect(normalizedLocalPath).not.toBe(sourcePath);
514+
expect(normalizedLocalPath.startsWith(path.join(stateDir, "media"))).toBe(true);
515+
});
454516
});

‎src/gateway/server-methods/chat-reply-media.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,20 @@ export async function normalizeWebchatReplyMediaPathsForDisplay(params: {
6565
continue;
6666
}
6767
const mergedMediaUrls: string[] = [];
68-
const text = payload.text;
68+
let text = payload.text;
6969
for (const mediaUrl of mediaUrls) {
7070
if (shouldPreserveDisplayMediaUrl(payload, mediaUrl)) {
7171
mergedMediaUrls.push(mediaUrl);
7272
continue;
7373
}
7474
const normalizedPayload = await normalizeMediaPaths({
7575
...payload,
76+
text,
7677
mediaUrl,
7778
mediaUrls: [mediaUrl],
7879
});
7980
const normalizedMediaUrls = resolveSendableOutboundReplyParts(normalizedPayload).mediaUrls;
81+
text = normalizedPayload.text;
8082
if (normalizedMediaUrls.length === 0) {
8183
continue;
8284
}

0 commit comments

Comments
 (0)