Skip to content

Commit 1a956b6

Browse files
committed
fix: require message tool for generated media completions
1 parent df3f983 commit 1a956b6

14 files changed

Lines changed: 174 additions & 56 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Docs: https://docs.openclaw.ai
3030
- Discord: bind delayed gateway `identify` retries to the originating socket generation so retries triggered after a reconnect do not identify against a fresh socket. Fixes #82225. Thanks @giodl73-repo.
3131
- ACP/control plane: refresh cached runtime handles when agent config changes so ACP sessions stop using stale runtimes after `agents.defaults` edits. Fixes #82237. Thanks @giodl73-repo.
3232
- Gateway/sessions: scope session data lookups by agent id so multi-agent gateway state cannot cross-leak session records across configured agents. (#81386) Thanks @pgondhi987.
33+
- Agents/media: require generated music/video completion agents to use the message tool for visible delivery and stop merging generated image attachments into message-tool-only source reply mirrors, avoiding direct fallback posts that can duplicate media the model already sent.
3334
- Agents/media: accept generated media attachments on internal completion events and report delivery-loss failures as errors, so completed background music/video tasks do not disappear after provider success.
3435
- Matrix/approvals: release in-flight reaction bindings when the channel approval handler stops mid-delivery, preventing stale approval targets after restart. Fixes #82485. (#82482) Thanks @Feelw00.
3536
- Matrix/E2EE: stop requesting MSC4222 `state_after` sync responses so homeservers with incomplete state-after data do not leave fresh encrypted rooms without outbound room encryptors. Fixes #82515. Thanks @nickdecooman.

‎docs/tools/image-generation.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ sidebarTitle: "Image generation"
99
---
1010

1111
The `image_generate` tool lets the agent create and edit images using your
12-
configured providers. Generated images are delivered automatically as media
13-
attachments in the agent's reply.
12+
configured providers. Generated images are returned as structured media
13+
attachments. On routes that require the `message` tool for visible replies,
14+
the agent must send the generated images through `message`; OpenClaw does not
15+
auto-post a private final reply as a fallback.
1416

1517
<Note>
1618
The tool only appears when at least one image-generation provider is
@@ -52,7 +54,9 @@ or sign in with OpenAI Codex OAuth.
5254
_"Generate an image of a friendly robot mascot."_
5355

5456
The agent calls `image_generate` automatically. No tool allow-listing
55-
needed - it is enabled by default when a provider is available.
57+
needed - it is enabled by default when a provider is available. In
58+
message-tool-only channels, the agent then sends the generated attachment
59+
through the `message` tool.
5660

5761
</Step>
5862
</Steps>

‎docs/tools/media-overview.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ telephony, meetings, browser realtime, and native push-to-talk clients.
2525
<CardGroup cols={2}>
2626
<Card title="Image generation" href="/tools/image-generation" icon="image">
2727
Create and edit images from text prompts or reference images via
28-
`image_generate`. Synchronous — completes inline with the reply.
28+
`image_generate`. Synchronous — returns structured media attachments for
29+
the agent to deliver with the reply contract.
2930
</Card>
3031
<Card title="Video generation" href="/tools/video-generation" icon="video">
3132
Text-to-video, image-to-video, and video-to-video via `video_generate`.
@@ -88,7 +89,7 @@ reply model.
8889

8990
| Capability | Mode | Why |
9091
| --------------- | ------------ | ---------------------------------------------------------------------------------------------------- |
91-
| Image | Synchronous | Provider responses return in seconds; completes inline with reply. |
92+
| Image | Synchronous | Provider responses return in seconds; tool-result media follows the current reply contract. |
9293
| Text-to-speech | Synchronous | Provider responses return in seconds; attached to the reply audio. |
9394
| Video | Asynchronous | Provider processing takes 30 s to several minutes; slow queues can run up to the configured timeout. |
9495
| Music (shared) | Asynchronous | Same provider-processing characteristic as video. |
@@ -98,10 +99,9 @@ For async tools, OpenClaw submits the request to the provider, returns a task
9899
id immediately, and tracks the job in the task ledger. The agent continues
99100
responding to other messages while the job runs. When the provider finishes,
100101
OpenClaw wakes the agent with the generated media paths so it can tell the
101-
user and, when required by source-delivery policy, relay the result through
102-
the message tool. For message-tool-only group/channel routes, OpenClaw treats
103-
missing message-tool delivery evidence as a failed completion attempt and sends
104-
the generated media fallback directly to the original channel.
102+
user and relay the result through the message tool. OpenClaw treats missing
103+
message-tool delivery evidence as a failed completion attempt and does not
104+
auto-post the generated media as a fallback.
105105

106106
## Speech-to-text and Voice Call
107107

‎docs/tools/music-generation.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ MiniMax, and workflow-configured ComfyUI today.
1515
For session-backed agent runs, OpenClaw starts music generation as a
1616
background task, tracks it in the task ledger, then wakes the agent again
1717
when the track is ready so the agent can tell the user and attach the
18-
finished audio. In group/channel chats that use message-tool-only visible
19-
delivery, the agent relays the result through the message tool. If the
20-
completion agent writes only a private final reply, OpenClaw falls back to a
21-
direct channel send with the generated media. The completion wake explicitly
22-
warns the agent that normal final replies are private in those routes.
18+
finished audio. Generated-media completions are delivered by the agent through
19+
the message tool; OpenClaw does not auto-post the file as a fallback if the
20+
completion agent writes only a private final reply. The completion wake
21+
explicitly warns the agent that normal final replies are private for this
22+
route.
2323

2424
<Note>
2525
The built-in shared tool only appears when at least one music-generation

‎docs/tools/video-generation.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ session:
6262
1. OpenClaw submits the request to the provider and immediately returns a task id.
6363
2. The provider processes the job in the background (typically 30 seconds to several minutes depending on the provider and resolution; slow queue-backed providers can run up to the configured timeout).
6464
3. When the video is ready, OpenClaw wakes the same session with an internal completion event.
65-
4. The agent tells the user and attaches the finished video. In group/channel
66-
chats that use message-tool-only visible delivery, the agent relays the
67-
result through the message tool instead of OpenClaw posting it directly.
65+
4. The agent tells the user and attaches the finished video through the
66+
message tool. OpenClaw does not auto-post the video as a fallback if the
67+
completion agent writes only a private final reply.
6868

6969
While a job is in flight, duplicate `video_generate` calls in the same
7070
session return the current task status instead of starting another

‎src/agents/pi-embedded-runner/run.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,7 @@ export async function runEmbeddedPiAgent(
25762576
toolMediaUrls: attempt.toolMediaUrls,
25772577
toolAudioAsVoice: attempt.toolAudioAsVoice,
25782578
toolTrustedLocalMedia: attempt.toolTrustedLocalMedia,
2579+
sourceReplyDeliveryMode: params.sourceReplyDeliveryMode,
25792580
});
25802581
const timedOutDuringPrompt =
25812582
timedOut && !timedOutDuringCompaction && !timedOutDuringToolExecution;

‎src/agents/pi-embedded-runner/run/tool-media-payloads.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,33 @@ describe("mergeAttemptToolMediaPayloads", () => {
8989
},
9090
]);
9191
});
92+
93+
it("does not attach tool media to message-tool-only source reply mirrors", () => {
94+
const sourceReply = setReplyPayloadMetadata(
95+
{ text: "sent through message tool" },
96+
{
97+
deliverDespiteSourceReplySuppression: true,
98+
sourceReplyTranscriptMirror: {
99+
sessionKey: "agent:main",
100+
text: "sent through message tool",
101+
},
102+
},
103+
);
104+
105+
const [mergedReply] =
106+
mergeAttemptToolMediaPayloads({
107+
payloads: [sourceReply],
108+
toolMediaUrls: ["/tmp/generated.png"],
109+
sourceReplyDeliveryMode: "message_tool_only",
110+
}) ?? [];
111+
112+
expect(mergedReply).toEqual({ text: "sent through message tool" });
113+
expect(getReplyPayloadMetadata(mergedReply ?? {})).toMatchObject({
114+
deliverDespiteSourceReplySuppression: true,
115+
sourceReplyTranscriptMirror: {
116+
sessionKey: "agent:main",
117+
text: "sent through message tool",
118+
},
119+
});
120+
});
92121
});

‎src/agents/pi-embedded-runner/run/tool-media-payloads.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { copyReplyPayloadMetadata } from "../../../auto-reply/reply-payload.js";
1+
import type { SourceReplyDeliveryMode } from "../../../auto-reply/get-reply-options.types.js";
2+
import {
3+
copyReplyPayloadMetadata,
4+
getReplyPayloadMetadata,
5+
} from "../../../auto-reply/reply-payload.js";
26
import type { EmbeddedPiRunResult } from "../types.js";
37

48
type EmbeddedRunPayload = NonNullable<EmbeddedPiRunResult["payloads"]>[number];
@@ -8,6 +12,7 @@ export function mergeAttemptToolMediaPayloads(params: {
812
toolMediaUrls?: string[];
913
toolAudioAsVoice?: boolean;
1014
toolTrustedLocalMedia?: boolean;
15+
sourceReplyDeliveryMode?: SourceReplyDeliveryMode;
1116
}): EmbeddedRunPayload[] | undefined {
1217
const mediaUrls = Array.from(
1318
new Set(params.toolMediaUrls?.map((url) => url.trim()).filter(Boolean) ?? []),
@@ -20,6 +25,12 @@ export function mergeAttemptToolMediaPayloads(params: {
2025
const payloadIndex = payloads.findIndex((payload) => !payload.isReasoning);
2126
if (payloadIndex >= 0) {
2227
const payload = payloads[payloadIndex];
28+
if (
29+
params.sourceReplyDeliveryMode === "message_tool_only" &&
30+
getReplyPayloadMetadata(payload)?.sourceReplyTranscriptMirror
31+
) {
32+
return payloads;
33+
}
2334
const mergedMediaUrls = Array.from(new Set([...(payload.mediaUrls ?? []), ...mediaUrls]));
2435
payloads[payloadIndex] = copyReplyPayloadMetadata(payload, {
2536
...payload,

‎src/agents/subagent-announce-delivery.test.ts‎

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,7 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
11991199
const result = await deliverDiscordDirectMessageCompletion({
12001200
callGateway,
12011201
sendMessage,
1202+
sourceTool: "music_generate",
12021203
internalEvents: [
12031204
{
12041205
type: "task_completion",
@@ -1219,14 +1220,15 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
12191220
expectRecordFields(result, {
12201221
delivered: false,
12211222
path: "direct",
1222-
error: "completion agent did not produce a visible reply",
1223+
error: "completion agent did not deliver through the message tool",
12231224
});
12241225
expectGatewayAgentParams(callGateway, {
1225-
deliver: true,
1226+
deliver: false,
12261227
channel: "discord",
12271228
accountId: "acct-1",
12281229
to: "dm:U123",
12291230
threadId: undefined,
1231+
sourceReplyDeliveryMode: "message_tool_only",
12301232
});
12311233
expect(sendMessage).not.toHaveBeenCalled();
12321234
});
@@ -1243,6 +1245,7 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
12431245
const result = await deliverDiscordDirectMessageCompletion({
12441246
callGateway,
12451247
sendMessage,
1248+
sourceTool: "music_generate",
12461249
internalEvents: [
12471250
{
12481251
type: "task_completion",
@@ -1265,14 +1268,27 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
12651268
path: "direct",
12661269
});
12671270
expect(callGateway).toHaveBeenCalledTimes(1);
1271+
expectGatewayAgentParams(callGateway, {
1272+
deliver: false,
1273+
channel: "discord",
1274+
accountId: "acct-1",
1275+
to: "dm:U123",
1276+
threadId: undefined,
1277+
sourceReplyDeliveryMode: "message_tool_only",
1278+
});
12681279
expect(sendMessage).not.toHaveBeenCalled();
12691280
});
12701281

1271-
it("delivers generated media completions through the announce agent in automatic DMs", async () => {
1282+
it("requires generated media completion DMs to use the message tool", async () => {
12721283
const callGateway = createGatewayMock({
12731284
result: {
1274-
payloads: [
1285+
payloads: [],
1286+
messagingToolSentTargets: [
12751287
{
1288+
tool: "message",
1289+
provider: "discord",
1290+
accountId: "acct-1",
1291+
to: "dm:U123",
12761292
text: "The track is ready.",
12771293
mediaUrls: ["/tmp/generated-night-drive.mp3"],
12781294
},
@@ -1307,11 +1323,63 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
13071323
path: "direct",
13081324
});
13091325
expectGatewayAgentParams(callGateway, {
1310-
deliver: true,
1326+
deliver: false,
13111327
channel: "discord",
13121328
accountId: "acct-1",
13131329
to: "dm:U123",
13141330
threadId: undefined,
1331+
sourceReplyDeliveryMode: "message_tool_only",
1332+
});
1333+
expect(sendMessage).not.toHaveBeenCalled();
1334+
});
1335+
1336+
it("accepts failed generated media completion notices through the message tool", async () => {
1337+
const callGateway = createGatewayMock({
1338+
result: {
1339+
payloads: [],
1340+
messagingToolSentTargets: [
1341+
{
1342+
tool: "message",
1343+
provider: "discord",
1344+
accountId: "acct-1",
1345+
to: "dm:U123",
1346+
text: "Music generation failed: provider failed.",
1347+
},
1348+
],
1349+
},
1350+
});
1351+
const sendMessage = createSendMessageMock();
1352+
const result = await deliverDiscordDirectMessageCompletion({
1353+
callGateway,
1354+
sendMessage,
1355+
sourceTool: "music_generate",
1356+
internalEvents: [
1357+
{
1358+
type: "task_completion",
1359+
source: "music_generation",
1360+
childSessionKey: "music_generate:task-123",
1361+
childSessionId: "task-123",
1362+
announceType: "music generation task",
1363+
taskLabel: "night-drive synthwave",
1364+
status: "error",
1365+
statusLabel: "failed",
1366+
result: "provider failed",
1367+
replyInstruction: "Deliver the failure through the message tool.",
1368+
},
1369+
],
1370+
});
1371+
1372+
expectRecordFields(result, {
1373+
delivered: true,
1374+
path: "direct",
1375+
});
1376+
expectGatewayAgentParams(callGateway, {
1377+
deliver: false,
1378+
channel: "discord",
1379+
accountId: "acct-1",
1380+
to: "dm:U123",
1381+
threadId: undefined,
1382+
sourceReplyDeliveryMode: "message_tool_only",
13151383
});
13161384
expect(sendMessage).not.toHaveBeenCalled();
13171385
});
@@ -1358,7 +1426,7 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
13581426
expectRecordFields(result, {
13591427
delivered: false,
13601428
path: "direct",
1361-
error: "completion agent did not deliver generated media",
1429+
error: "completion agent did not deliver through the message tool",
13621430
});
13631431
expect(sendMessage).not.toHaveBeenCalled();
13641432
});

‎src/agents/subagent-announce-delivery.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { completionRequiresMessageToolDelivery } from "../auto-reply/reply/completion-delivery-policy.js";
21
import type { OpenClawConfig } from "../config/types.openclaw.js";
32
import type { ConversationRef } from "../infra/outbound/session-binding-service.js";
43
import { stringifyRouteThreadId } from "../plugin-sdk/channel-route.js";
@@ -630,16 +629,7 @@ async function sendSubagentAnnounceDirectly(params: {
630629
expectsCompletionMessage: params.expectsCompletionMessage,
631630
sourceTool: params.sourceTool,
632631
});
633-
const requiresMessageToolDelivery =
634-
agentMediatedCompletion &&
635-
completionRequiresMessageToolDelivery({
636-
cfg,
637-
requesterSessionKey: params.requesterSessionKey,
638-
targetRequesterSessionKey: params.targetRequesterSessionKey,
639-
requesterEntry,
640-
directOrigin: effectiveDirectOrigin,
641-
requesterSessionOrigin,
642-
});
632+
const requiresMessageToolDelivery = agentMediatedCompletion;
643633
const completionSourceReplyDeliveryMode = requiresMessageToolDelivery
644634
? "message_tool_only"
645635
: undefined;

0 commit comments

Comments
 (0)