Skip to content

Commit e1a15aa

Browse files
fix(agent): always reply after settled tool work (#133520)
* fix(agent): always finalize settled tool turns * fix(agent): preserve silent helper failures * fix(agent): preserve cancellation during fallback persistence * test(qa): update settled fallback contracts * chore: leave changelog to release generation * fix(agent): preserve fallback writer ownership * fix(agent): fence settled final reply delivery * fix(agent): fence settled replies at transport * fix(outbound): fence recovered final replies * fix(outbound): fence final adapter handoff * refactor(agent): keep run loop within lint budget * fix(channels): fence provider sends after custody refresh * test(infra): expect delivery handoff fence
1 parent 607a49e commit e1a15aa

84 files changed

Lines changed: 2159 additions & 203 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎extensions/codex/src/app-server/settled-turn-finalizer.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,15 @@ describe("runCodexSettledTurnFinalization", () => {
227227
expect(mocks.mirror).not.toHaveBeenCalled();
228228
});
229229

230-
it("rejects an intentionally silent final answer before transcript mutation", async () => {
230+
it("returns an intentionally silent final answer as completed empty before transcript mutation", async () => {
231231
mocks.runBounded.mockResolvedValue({ text: "NO_REPLY", items: [], model: "gpt-5.4" });
232232

233233
await expect(
234234
runCodexSettledTurnFinalization(
235235
{ attempt: createAttempt(), settledAttempt: createSettledAttempt() },
236236
{},
237237
),
238-
).rejects.toThrow("completed without a visible answer");
238+
).resolves.toMatchObject({ assistant: { content: [{ type: "text", text: "" }] } });
239239
expect(mocks.mirror).not.toHaveBeenCalled();
240240
});
241241

‎extensions/codex/src/app-server/settled-turn-finalizer.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function runCodexSettledTurnFinalization(
8383
);
8484
}
8585
const text = bounded.text.trim();
86-
if (!text) {
86+
if (!text || isSilentReplyText(text)) {
8787
return {
8888
assistant: createAssistantMessage(attempt, "", {
8989
tokenUsage: bounded.usage,
@@ -93,9 +93,6 @@ export async function runCodexSettledTurnFinalization(
9393
...(bounded.usage ? { usage: bounded.usage } : {}),
9494
};
9595
}
96-
if (isSilentReplyText(text)) {
97-
throw new Error("Codex settled-turn finalization completed without a visible answer");
98-
}
9996

10097
const mirrorIdentity = `settled-finalizer:${attempt.runId}`;
10198
const assistant = attachCodexMirrorIdentity(

‎extensions/discord/src/monitor/agent-components.dispatch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ export async function dispatchDiscordComponentEvent(params: {
336336
kind: info.kind,
337337
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
338338
onPlatformSendDispatch: info.onPlatformSendDispatch,
339+
assertPlatformSendAuthorized: info.assertPlatformSendAuthorized,
339340
});
340341
if (result.visibleReplySent) {
341342
replyReference.markSent();

‎extensions/discord/src/monitor/message-handler.process.test-harness.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ vi.mock("openclaw/plugin-sdk/channel-inbound", async (importOriginal) => {
446446
const providerInfo = {
447447
...info,
448448
onPlatformSendDispatch: async () => undefined,
449+
assertPlatformSendAuthorized: () => undefined,
449450
};
450451
return delivery.deliverWithProviderMessageSending(payload, providerInfo);
451452
},

‎extensions/discord/src/monitor/message-handler.process.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type DiscordProviderDeliveryInfo = {
7676
kind: ReplyDispatchKind;
7777
bindPendingFinalDelivery?: <T extends ReplyPayload>(payload: T) => T;
7878
onPlatformSendDispatch: () => Promise<void>;
79+
assertPlatformSendAuthorized: () => void;
7980
};
8081

8182
export async function processDiscordMessage(
@@ -330,6 +331,7 @@ async function processDiscordMessageInner(
330331
kind: "block",
331332
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
332333
onPlatformSendDispatch: info.onPlatformSendDispatch,
334+
assertPlatformSendAuthorized: info.assertPlatformSendAuthorized,
333335
});
334336
if (result.visibleReplySent) {
335337
replyReference.markSent();
@@ -486,6 +488,7 @@ async function processDiscordMessageInner(
486488
kind: info.kind,
487489
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
488490
onPlatformSendDispatch: info.onPlatformSendDispatch,
491+
assertPlatformSendAuthorized: info.assertPlatformSendAuthorized,
489492
});
490493
return deliveryResult.visibleReplySent;
491494
},
@@ -536,6 +539,7 @@ async function processDiscordMessageInner(
536539
kind: info.kind,
537540
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
538541
onPlatformSendDispatch: info.onPlatformSendDispatch,
542+
assertPlatformSendAuthorized: info.assertPlatformSendAuthorized,
539543
});
540544
if (!result.visibleReplySent) {
541545
return result;
@@ -695,6 +699,7 @@ async function processDiscordMessageInner(
695699
deliverDiscordPayload(payload, {
696700
...info,
697701
onPlatformSendDispatch: () => Promise.resolve(),
702+
assertPlatformSendAuthorized: () => undefined,
698703
}),
699704
onDiscordDeliveryError,
700705
);

‎extensions/discord/src/monitor/reply-delivery.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export async function deliverDiscordReply(params: {
231231
kind: "tool" | "block" | "final";
232232
bindPendingFinalDelivery?: <T extends ReplyPayload>(payload: T) => T;
233233
onPlatformSendDispatch?: () => Promise<void>;
234+
assertPlatformSendAuthorized?: () => void;
234235
}) {
235236
void params.runtime;
236237

@@ -259,6 +260,7 @@ export async function deliverDiscordReply(params: {
259260
threadId: delivery.threadId,
260261
identity: delivery.identity,
261262
onPlatformSendDispatch: params.onPlatformSendDispatch,
263+
assertDirectAdapterHandoff: params.assertPlatformSendAuthorized,
262264
deps: createDiscordDeliveryDeps({
263265
cfg: params.cfg,
264266
token: params.token,

‎extensions/discord/src/outbound-adapter.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ async function maybeSendDiscordWebhookText(params: {
7575
identity?: OutboundIdentity;
7676
replyToId?: string | null;
7777
onPlatformSendDispatch?: () => Promise<void>;
78+
assertPlatformSendAuthorized?: () => void;
7879
}): Promise<{ messageId: string; channelId: string } | null> {
7980
if (params.threadId == null) {
8081
return null;
@@ -107,6 +108,7 @@ async function maybeSendDiscordWebhookText(params: {
107108
username: persona.username,
108109
avatarUrl: persona.avatarUrl,
109110
onPlatformSendDispatch: params.onPlatformSendDispatch,
111+
assertPlatformSendAuthorized: params.assertPlatformSendAuthorized,
110112
});
111113
return result;
112114
}
@@ -144,6 +146,7 @@ async function resolveDiscordOutboundMessageSend(params: DiscordOutboundMessageC
144146
}
145147
: undefined,
146148
onPlatformSendDispatch: params.onPlatformSendDispatch,
149+
assertPlatformSendAuthorized: params.assertDirectAdapterHandoff,
147150
},
148151
};
149152
}
@@ -202,6 +205,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
202205
await ctx.onPlatformSendDispatch?.();
203206
}
204207
: undefined,
208+
assertPlatformSendAuthorized: ctx.assertDirectAdapterHandoff,
205209
});
206210
if (webhookResult) {
207211
return toDiscordOutboundDeliveryResult(webhookResult);
@@ -235,6 +239,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
235239
mediaLocalRoots: ctx.mediaLocalRoots,
236240
mediaReadFile: ctx.mediaReadFile,
237241
onPlatformSendDispatch: ctx.onPlatformSendDispatch,
242+
assertPlatformSendAuthorized: ctx.assertDirectAdapterHandoff,
238243
}),
239244
);
240245
}
@@ -277,6 +282,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
277282
sessionKey,
278283
inboundEventKind,
279284
onPlatformSendDispatch,
285+
assertDirectAdapterHandoff,
280286
}) => {
281287
if (!createDiscordActionGate({ cfg, accountId })("polls")) {
282288
throw new Error("Discord polls are disabled.");
@@ -291,6 +297,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
291297
silent: silent ?? undefined,
292298
cfg,
293299
onPlatformSendDispatch,
300+
assertPlatformSendAuthorized: assertDirectAdapterHandoff,
294301
});
295302
discordInboundEventDelivery.notify({
296303
sessionKey,

‎extensions/discord/src/outbound-payload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function resolveDiscordDeliveryOptions(
5757
silent: ctx.silent ?? undefined,
5858
cfg: ctx.cfg,
5959
onPlatformSendDispatch: ctx.onPlatformSendDispatch,
60+
assertPlatformSendAuthorized: ctx.assertDirectAdapterHandoff,
6061
};
6162
}
6263

‎extensions/discord/src/send.components.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ type DiscordComponentSendOpts = {
172172
/** Persist the concrete platform send before component bookkeeping can fail. */
173173
onDeliveryResult?: (result: DiscordSendResult) => Promise<void> | void;
174174
onPlatformSendDispatch?: () => Promise<void>;
175+
assertPlatformSendAuthorized?: () => void;
175176
};
176177

177178
export function registerBuiltDiscordComponentMessage(params: {
@@ -294,6 +295,7 @@ export async function sendDiscordComponentMessage(
294295
chunkMode: opts.chunkMode,
295296
onDeliveryResult: opts.onDeliveryResult,
296297
onPlatformSendDispatch: opts.onPlatformSendDispatch,
298+
assertPlatformSendAuthorized: opts.assertPlatformSendAuthorized,
297299
...(opts.suppressEmbeds === undefined ? {} : { suppressEmbeds: opts.suppressEmbeds }),
298300
});
299301
}
@@ -326,6 +328,7 @@ export async function sendDiscordComponentMessage(
326328
result = (await request(
327329
async () => {
328330
await opts.onPlatformSendDispatch?.();
331+
opts.assertPlatformSendAuthorized?.();
329332
return createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, {
330333
body,
331334
});

‎extensions/discord/src/send.outbound.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ type DiscordSendOpts = {
7171
onDeliveryResult?: (result: DiscordSendResult) => Promise<void> | void;
7272
/** @internal Refresh durable custody immediately before Discord REST I/O. */
7373
onPlatformSendDispatch?: () => Promise<void>;
74+
/** @internal Synchronously fence custody after refresh and immediately before Discord REST I/O. */
75+
assertPlatformSendAuthorized?: () => void;
7476
};
7577

7678
type DiscordClientRequest = ReturnType<typeof createDiscordClient>["request"];
@@ -94,6 +96,7 @@ async function sendDiscordThreadTextChunks(params: {
9496
allowedMentions?: DiscordAllowedMentions;
9597
onResult?: DiscordSendProgress;
9698
onPlatformSendDispatch?: () => Promise<void>;
99+
assertPlatformSendAuthorized?: () => void;
97100
}): Promise<void> {
98101
for (const chunk of params.chunks) {
99102
await sendDiscordText({
@@ -109,6 +112,7 @@ async function sendDiscordThreadTextChunks(params: {
109112
maxChars: params.maxChars,
110113
onResult: params.onResult,
111114
onPlatformSendDispatch: params.onPlatformSendDispatch,
115+
assertPlatformSendAuthorized: params.assertPlatformSendAuthorized,
112116
});
113117
}
114118
}
@@ -256,6 +260,7 @@ export async function sendMessageDiscord(
256260
threadRes = (await request(
257261
async () => {
258262
await opts.onPlatformSendDispatch?.();
263+
opts.assertPlatformSendAuthorized?.();
259264
return createThread<{ id: string; message?: { id: string; channel_id: string } }>(
260265
rest,
261266
channelId,
@@ -323,6 +328,7 @@ export async function sendMessageDiscord(
323328
maxChars: textLimit,
324329
onResult: reportResult,
325330
onPlatformSendDispatch: opts.onPlatformSendDispatch,
331+
assertPlatformSendAuthorized: opts.assertPlatformSendAuthorized,
326332
});
327333
await sendDiscordThreadTextChunks({
328334
rest,
@@ -337,6 +343,7 @@ export async function sendMessageDiscord(
337343
allowedMentions: opts.allowedMentions,
338344
onResult: reportResult,
339345
onPlatformSendDispatch: opts.onPlatformSendDispatch,
346+
assertPlatformSendAuthorized: opts.assertPlatformSendAuthorized,
340347
});
341348
} else {
342349
await sendDiscordThreadTextChunks({
@@ -352,6 +359,7 @@ export async function sendMessageDiscord(
352359
allowedMentions: opts.allowedMentions,
353360
onResult: reportResult,
354361
onPlatformSendDispatch: opts.onPlatformSendDispatch,
362+
assertPlatformSendAuthorized: opts.assertPlatformSendAuthorized,
355363
});
356364
}
357365
} catch (err) {
@@ -400,6 +408,7 @@ export async function sendMessageDiscord(
400408
maxChars: textLimit,
401409
onResult: reportResult,
402410
onPlatformSendDispatch: opts.onPlatformSendDispatch,
411+
assertPlatformSendAuthorized: opts.assertPlatformSendAuthorized,
403412
});
404413
} else {
405414
result = await sendDiscordText({
@@ -418,6 +427,7 @@ export async function sendMessageDiscord(
418427
maxChars: textLimit,
419428
onResult: reportResult,
420429
onPlatformSendDispatch: opts.onPlatformSendDispatch,
430+
assertPlatformSendAuthorized: opts.assertPlatformSendAuthorized,
421431
});
422432
}
423433
} catch (err) {
@@ -510,6 +520,7 @@ async function resolveDiscordStructuredSendContext(
510520
const result = (await request(
511521
async () => {
512522
await opts.onPlatformSendDispatch?.();
523+
opts.assertPlatformSendAuthorized?.();
513524
return createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, {
514525
body,
515526
});

0 commit comments

Comments
 (0)