Skip to content

Commit c6bcd93

Browse files
steipetePeter Steinberger
andauthored
fix(discord): preserve final slash-command failure outcomes (#118545)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 6ce00ca commit c6bcd93

2 files changed

Lines changed: 154 additions & 17 deletions

File tree

‎extensions/discord/src/monitor/native-command-agent-reply.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function dispatchDiscordNativeAgentReply(params: {
5050
const blockStreamingEnabled = resolveChannelStreamingBlockEnabled(params.discordConfig);
5151

5252
let didReply = false;
53-
let settledWithoutVisibleReply = false;
53+
let finalReplyOutcome: "accepted" | "failed" | "suppressed" | undefined;
5454
const turnResult = await nativeCommandRuntime.dispatchChannelInboundTurn({
5555
cfg: params.cfg,
5656
channel: "discord",
@@ -92,18 +92,27 @@ export async function dispatchDiscordNativeAgentReply(params: {
9292
suppression: { reason: "no_visible_result" as const },
9393
};
9494
},
95-
onDelivered: (_payload, _info, result) => {
96-
if (result?.visibleReplySent === false) {
97-
settledWithoutVisibleReply = true;
95+
onDelivered: (_payload, info, result) => {
96+
// A failed final outweighs later suppression until Discord accepts a final.
97+
if (
98+
info.kind === "final" &&
99+
result?.visibleReplySent !== undefined &&
100+
(result.visibleReplySent || finalReplyOutcome !== "failed")
101+
) {
102+
finalReplyOutcome = result.visibleReplySent ? "accepted" : "suppressed";
98103
}
99104
},
100105
onError: (err, info) => {
101-
if (isChannelPartialDeliveryError(err)) {
106+
const partialDelivery = isChannelPartialDeliveryError(err);
107+
if (partialDelivery) {
102108
// Preserve failed delivery accounting while preventing an empty fallback from
103109
// obscuring the prefix that Discord already accepted for this payload.
104110
didReply = true;
105111
logVerbose("discord: interaction reply partially delivered before expiry");
106112
}
113+
if (info.kind === "final") {
114+
finalReplyOutcome = partialDelivery ? "accepted" : "failed";
115+
}
107116
const message = err instanceof Error ? (err.stack ?? err.message) : String(err);
108117
params.log.error(`discord slash ${info.kind} reply failed: ${message}`);
109118
},
@@ -119,14 +128,12 @@ export async function dispatchDiscordNativeAgentReply(params: {
119128
},
120129
});
121130

122-
if (!didReply && (params.suppressReplies || settledWithoutVisibleReply)) {
131+
if (!didReply && (params.suppressReplies || finalReplyOutcome === "suppressed")) {
123132
await settleDiscordInteractionWithoutVisibleReply(params.interaction);
124133
return;
125134
}
126135
if (
127-
params.suppressReplies ||
128136
didReply ||
129-
settledWithoutVisibleReply ||
130137
(turnResult.dispatched && hasVisibleInboundReplyDispatch(turnResult.dispatchResult))
131138
) {
132139
return;

‎extensions/discord/src/monitor/native-command.plugin-dispatch.test.ts‎

Lines changed: 139 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -984,19 +984,54 @@ describe("Discord native plugin command dispatch", () => {
984984
expect(interaction.reply).not.toHaveBeenCalled();
985985
});
986986

987-
it("does not emit the empty warning when the routed reply hook cancels delivery", async () => {
987+
it("warns when a final delivery observer does not report its outcome", async () => {
988988
const cfg = createConfig();
989989
const interaction = createInteraction();
990990
runtimeModuleMocks.matchPluginCommand.mockReturnValue(null);
991991
nativeCommandRuntime.dispatchChannelInboundTurn = async (plan) => {
992-
await plan.delivery.onDelivered?.(
993-
{ text: "cancelled" },
994-
{ kind: "final" },
995-
{
996-
visibleReplySent: false,
997-
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
992+
await plan.delivery.onDelivered?.({ text: "unreported" }, { kind: "final" }, undefined);
993+
return {
994+
admission: { kind: "dispatch" },
995+
dispatched: true,
996+
ctxPayload: plan.ctxPayload,
997+
routeSessionKey: plan.route.sessionKey,
998+
dispatchResult: {
999+
counts: { final: 0, block: 0, tool: 0 },
1000+
queuedFinal: false,
9981001
},
999-
);
1002+
};
1003+
};
1004+
const command = await createNativeCommand(cfg, {
1005+
name: "new",
1006+
description: "Start a new session.",
1007+
acceptsArgs: true,
1008+
});
1009+
1010+
await (command as { run: (interaction: unknown) => Promise<void> }).run(interaction as unknown);
1011+
1012+
expectFollowUpFields(interaction, {
1013+
content: "⚠️ Command produced no visible reply.",
1014+
ephemeral: true,
1015+
});
1016+
expect(interaction.reply).not.toHaveBeenCalled();
1017+
expect(interaction.deleteReply).not.toHaveBeenCalled();
1018+
});
1019+
1020+
it.each([1, 2])("settles %i suppressed finals without an empty warning", async (count) => {
1021+
const cfg = createConfig();
1022+
const interaction = createInteraction();
1023+
runtimeModuleMocks.matchPluginCommand.mockReturnValue(null);
1024+
nativeCommandRuntime.dispatchChannelInboundTurn = async (plan) => {
1025+
for (let index = 0; index < count; index += 1) {
1026+
await plan.delivery.onDelivered?.(
1027+
{ text: "cancelled" },
1028+
{ kind: "final" },
1029+
{
1030+
visibleReplySent: false,
1031+
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
1032+
},
1033+
);
1034+
}
10001035
return {
10011036
admission: { kind: "dispatch" },
10021037
dispatched: true,
@@ -1062,7 +1097,18 @@ describe("Discord native plugin command dispatch", () => {
10621097
expect(interaction.deleteReply).not.toHaveBeenCalled();
10631098
});
10641099

1065-
it("does not classify an already-deferred interaction as partial before this payload sends", async () => {
1100+
it.each([
1101+
{ label: "no intermediate suppression" },
1102+
{ label: "a suppressed block reply", kind: "block" as const },
1103+
{ label: "a suppressed tool reply", kind: "tool" as const },
1104+
{ label: "a prior suppressed final reply", kind: "final" as const },
1105+
{ label: "a later suppressed final reply", suppressAfterFailure: true },
1106+
{
1107+
label: "suppressed final replies before and after failure",
1108+
kind: "final" as const,
1109+
suppressAfterFailure: true,
1110+
},
1111+
])("warns when a deferred final fails with $label", async ({ kind, suppressAfterFailure }) => {
10661112
const cfg = createConfig();
10671113
const interaction = createInteraction();
10681114
interaction.followUp.mockRejectedValue({
@@ -1071,6 +1117,18 @@ describe("Discord native plugin command dispatch", () => {
10711117
});
10721118
runtimeModuleMocks.matchPluginCommand.mockReturnValue(null);
10731119
nativeCommandRuntime.dispatchChannelInboundTurn = async (plan) => {
1120+
const reportSuppressed = (suppressedKind: "block" | "final" | "tool") =>
1121+
plan.delivery.onDelivered?.(
1122+
{ text: "cancelled intermediate reply" },
1123+
{ kind: suppressedKind },
1124+
{
1125+
visibleReplySent: false,
1126+
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
1127+
},
1128+
);
1129+
if (kind) {
1130+
await reportSuppressed(kind);
1131+
}
10741132
if (!("deliver" in plan.delivery)) {
10751133
throw new Error("expected direct delivery adapter");
10761134
}
@@ -1088,6 +1146,9 @@ describe("Discord native plugin command dispatch", () => {
10881146
plan.delivery.onError?.(error, info);
10891147
}
10901148
expect(deliveryError).toBeInstanceOf(PlatformMessageNotDispatchedError);
1149+
if (suppressAfterFailure) {
1150+
await reportSuppressed("final");
1151+
}
10911152
return {
10921153
admission: { kind: "dispatch" },
10931154
dispatched: true,
@@ -1118,6 +1179,75 @@ describe("Discord native plugin command dispatch", () => {
11181179
expect(interaction.deleteReply).not.toHaveBeenCalled();
11191180
});
11201181

1182+
it.each([
1183+
{ label: "a suppressed final", outcomes: ["suppressed", "accepted"] as const },
1184+
{ label: "an earlier failed final", outcomes: ["failed", "accepted"] as const },
1185+
{ label: "a later failed final", outcomes: ["accepted", "failed"] as const },
1186+
])("keeps an accepted final visible alongside $label", async ({ outcomes }) => {
1187+
const cfg = createConfig();
1188+
const interaction = createInteraction();
1189+
runtimeModuleMocks.matchPluginCommand.mockReturnValue(null);
1190+
for (const outcome of outcomes) {
1191+
if (outcome === "accepted") {
1192+
interaction.followUp.mockResolvedValueOnce({ ok: true });
1193+
} else if (outcome === "failed") {
1194+
interaction.followUp.mockRejectedValueOnce(new Error("provider connection failed"));
1195+
}
1196+
}
1197+
nativeCommandRuntime.dispatchChannelInboundTurn = async (plan) => {
1198+
if (!("deliver" in plan.delivery) || !plan.delivery.deliver) {
1199+
throw new Error("expected direct delivery adapter");
1200+
}
1201+
let failedFinals = 0;
1202+
for (const outcome of outcomes) {
1203+
const payload = { text: `${outcome} final` };
1204+
const info = { kind: "final" as const };
1205+
if (outcome === "suppressed") {
1206+
await plan.delivery.onDelivered?.(payload, info, {
1207+
visibleReplySent: false,
1208+
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
1209+
});
1210+
continue;
1211+
}
1212+
try {
1213+
const result = await plan.delivery.deliver(payload, info);
1214+
await plan.delivery.onDelivered?.(payload, info, result);
1215+
} catch (error) {
1216+
failedFinals += 1;
1217+
plan.delivery.onError?.(error, info);
1218+
}
1219+
}
1220+
return {
1221+
admission: { kind: "dispatch" },
1222+
dispatched: true,
1223+
ctxPayload: plan.ctxPayload,
1224+
routeSessionKey: plan.route.sessionKey,
1225+
dispatchResult: {
1226+
counts: { final: 1, block: 0, tool: 0 },
1227+
failedCounts: { final: failedFinals, block: 0, tool: 0 },
1228+
queuedFinal: false,
1229+
},
1230+
};
1231+
};
1232+
const command = await createNativeCommand(cfg, {
1233+
name: "new",
1234+
description: "Start a new session.",
1235+
acceptsArgs: true,
1236+
});
1237+
1238+
await (command as { run: (interaction: unknown) => Promise<void> }).run(interaction as unknown);
1239+
1240+
expect(interaction.followUp).toHaveBeenCalledTimes(
1241+
outcomes.filter((outcome) => outcome !== "suppressed").length,
1242+
);
1243+
expect(interaction.followUp).toHaveBeenCalledWith(
1244+
expect.objectContaining({ content: "accepted final" }),
1245+
);
1246+
expectNoFollowUpContent(interaction, "⚠️ Command produced no visible reply.");
1247+
expect(interaction.reply).not.toHaveBeenCalled();
1248+
expect(interaction.deleteReply).not.toHaveBeenCalled();
1249+
});
1250+
11211251
it("preserves partial delivery when a later Discord chunk fails without expiry", async () => {
11221252
const cfg = createConfig();
11231253
const interaction = createInteraction();

0 commit comments

Comments
 (0)