Skip to content

Commit f68ecec

Browse files
authored
fix(telegram): preserve slash-command failure outcomes
Track final delivery outcomes at the Telegram native-command owner so failed finals produce a visible fallback without overriding intentional suppression or partial delivery.
1 parent a52b336 commit f68ecec

2 files changed

Lines changed: 181 additions & 8 deletions

File tree

‎extensions/telegram/src/bot-native-commands.session-meta.test.ts‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Telegram tests cover bot native commands.session meta plugin behavior.
2+
import { createChannelPartialDeliveryError } from "openclaw/plugin-sdk/channel-inbound";
23
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
34
import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
45
import { resolveChunkMode } from "openclaw/plugin-sdk/reply-dispatch-runtime";
@@ -1445,6 +1446,161 @@ describe("registerTelegramNativeCommands — session metadata", () => {
14451446
);
14461447
});
14471448

1449+
it("emits the fallback when a non-final suppression precedes a final failure", async () => {
1450+
dispatchChannelInboundTurnMock.mockImplementationOnce(async (plan) => {
1451+
await plan.delivery.onDelivered?.(
1452+
{ text: "cancelled tool reply" },
1453+
{ kind: "tool" },
1454+
{
1455+
visibleReplySent: false,
1456+
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
1457+
},
1458+
);
1459+
plan.delivery.onError?.(new Error("Telegram final delivery failed"), {
1460+
kind: "final",
1461+
});
1462+
return {
1463+
admission: { kind: "dispatch" },
1464+
dispatched: true,
1465+
ctxPayload: plan.ctxPayload,
1466+
routeSessionKey: plan.route.sessionKey,
1467+
dispatchResult: {
1468+
queuedFinal: false,
1469+
counts: { block: 0, final: 0, tool: 0 },
1470+
},
1471+
};
1472+
});
1473+
const { handler } = registerAndResolveStatusHandler({ cfg: {} });
1474+
1475+
await handler(createTelegramPrivateCommandContext());
1476+
1477+
expect(deliveryMocks.deliverReplies).toHaveBeenCalledOnce();
1478+
expect(deliveryMocks.deliverReplies).toHaveBeenCalledWith(
1479+
expect.objectContaining({
1480+
replies: [{ text: "No response generated. Please try again." }],
1481+
}),
1482+
);
1483+
});
1484+
1485+
it("emits the fallback when a suppressed block reply precedes a final failure", async () => {
1486+
dispatchChannelInboundTurnMock.mockImplementationOnce(async (plan) => {
1487+
await plan.delivery.onDelivered?.(
1488+
{ text: "cancelled block reply" },
1489+
{ kind: "block" },
1490+
{
1491+
visibleReplySent: false,
1492+
suppression: { reason: "empty_after_reply_payload_sending_hook" },
1493+
},
1494+
);
1495+
plan.delivery.onError?.(new Error("Telegram final delivery failed"), {
1496+
kind: "final",
1497+
});
1498+
return {
1499+
admission: { kind: "dispatch" },
1500+
dispatched: true,
1501+
ctxPayload: plan.ctxPayload,
1502+
routeSessionKey: plan.route.sessionKey,
1503+
dispatchResult: {
1504+
queuedFinal: false,
1505+
counts: { block: 0, final: 0, tool: 0 },
1506+
},
1507+
};
1508+
});
1509+
const { handler } = registerAndResolveStatusHandler({ cfg: {} });
1510+
1511+
await handler(createTelegramPrivateCommandContext());
1512+
1513+
expect(deliveryMocks.deliverReplies).toHaveBeenCalledOnce();
1514+
});
1515+
1516+
it("emits the fallback when a final failure precedes a later suppressed final", async () => {
1517+
dispatchChannelInboundTurnMock.mockImplementationOnce(async (plan) => {
1518+
plan.delivery.onError?.(new Error("Telegram final delivery failed"), {
1519+
kind: "final",
1520+
});
1521+
await plan.delivery.onDelivered?.(
1522+
{ text: "cancelled final reply" },
1523+
{ kind: "final" },
1524+
{
1525+
visibleReplySent: false,
1526+
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
1527+
},
1528+
);
1529+
return {
1530+
admission: { kind: "dispatch" },
1531+
dispatched: true,
1532+
ctxPayload: plan.ctxPayload,
1533+
routeSessionKey: plan.route.sessionKey,
1534+
dispatchResult: {
1535+
queuedFinal: false,
1536+
counts: { block: 0, final: 0, tool: 0 },
1537+
},
1538+
};
1539+
});
1540+
const { handler } = registerAndResolveStatusHandler({ cfg: {} });
1541+
1542+
await handler(createTelegramPrivateCommandContext());
1543+
1544+
expect(deliveryMocks.deliverReplies).toHaveBeenCalledOnce();
1545+
});
1546+
1547+
it("preserves a suppressed final after a non-final delivery failure", async () => {
1548+
dispatchChannelInboundTurnMock.mockImplementationOnce(async (plan) => {
1549+
plan.delivery.onError?.(new Error("Telegram tool delivery failed"), {
1550+
kind: "tool",
1551+
});
1552+
await plan.delivery.onDelivered?.(
1553+
{ text: "cancelled final reply" },
1554+
{ kind: "final" },
1555+
{
1556+
visibleReplySent: false,
1557+
suppression: { reason: "cancelled_by_reply_payload_sending_hook" },
1558+
},
1559+
);
1560+
return {
1561+
admission: { kind: "dispatch" },
1562+
dispatched: true,
1563+
ctxPayload: plan.ctxPayload,
1564+
routeSessionKey: plan.route.sessionKey,
1565+
dispatchResult: {
1566+
queuedFinal: false,
1567+
counts: { block: 0, final: 0, tool: 0 },
1568+
},
1569+
};
1570+
});
1571+
const { handler } = registerAndResolveStatusHandler({ cfg: {} });
1572+
1573+
await handler(createTelegramPrivateCommandContext());
1574+
1575+
expect(deliveryMocks.deliverReplies).not.toHaveBeenCalled();
1576+
});
1577+
1578+
it("does not emit the fallback after a partially delivered final", async () => {
1579+
dispatchChannelInboundTurnMock.mockImplementationOnce(async (plan) => {
1580+
plan.delivery.onError?.(
1581+
createChannelPartialDeliveryError(new Error("Telegram final delivery failed"), {
1582+
visibleReplySent: true,
1583+
}),
1584+
{ kind: "final" },
1585+
);
1586+
return {
1587+
admission: { kind: "dispatch" },
1588+
dispatched: true,
1589+
ctxPayload: plan.ctxPayload,
1590+
routeSessionKey: plan.route.sessionKey,
1591+
dispatchResult: {
1592+
queuedFinal: false,
1593+
counts: { block: 0, final: 0, tool: 0 },
1594+
},
1595+
};
1596+
});
1597+
const { handler } = registerAndResolveStatusHandler({ cfg: {} });
1598+
1599+
await handler(createTelegramPrivateCommandContext());
1600+
1601+
expect(deliveryMocks.deliverReplies).not.toHaveBeenCalled();
1602+
});
1603+
14481604
it("retains the empty fallback for a true non-silent metadata-only native reply", async () => {
14491605
dispatchChannelInboundTurnMock.mockImplementationOnce(async (plan) => {
14501606
plan.dispatcherOptions?.onSkip?.({}, { kind: "final", reason: "empty" });

‎extensions/telegram/src/bot-native-commands.ts‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
resolveDefaultModelForAgent,
99
resolveThinkingDefaultWithRuntimeCatalog,
1010
} from "openclaw/plugin-sdk/agent-runtime";
11-
import type { ChannelInboundTurnPlan } from "openclaw/plugin-sdk/channel-inbound";
11+
import {
12+
isChannelPartialDeliveryError,
13+
type ChannelInboundTurnPlan,
14+
} from "openclaw/plugin-sdk/channel-inbound";
1215
import { resolveChannelStreamingBlockEnabled } from "openclaw/plugin-sdk/channel-outbound";
1316
import { resolveNativeCommandSessionTargets } from "openclaw/plugin-sdk/command-auth-native";
1417
import {
@@ -1732,10 +1735,10 @@ export const registerTelegramNativeCommands = ({
17321735
resolveTelegramNativeCommandDisableBlockStreaming(runtimeTelegramCfg);
17331736
const deliveryState = {
17341737
delivered: false,
1735-
intentionallySuppressed: false,
17361738
skippedNonSilent: 0,
17371739
failedNonSilent: 0,
17381740
};
1741+
let finalReplyOutcome: "accepted" | "failed" | "suppressed" | undefined;
17391742

17401743
const { deliverReplies } = await loadTelegramNativeCommandDeliveryRuntime();
17411744
let recordSessionMetaTask: Promise<unknown> | undefined;
@@ -1810,17 +1813,31 @@ export const registerTelegramNativeCommands = ({
18101813
suppression: { reason: "no_visible_result" as const },
18111814
};
18121815
},
1813-
onDelivered: (_payload, _info, result) => {
1816+
onDelivered: (_payload, info, result) => {
18141817
const reason = result?.suppression?.reason;
1818+
if (info.kind === "final" && result?.visibleReplySent) {
1819+
finalReplyOutcome = "accepted";
1820+
}
18151821
if (
1816-
reason === "cancelled_by_reply_payload_sending_hook" ||
1817-
reason === "empty_after_reply_payload_sending_hook"
1822+
info.kind === "final" &&
1823+
finalReplyOutcome !== "failed" &&
1824+
(reason === "cancelled_by_reply_payload_sending_hook" ||
1825+
reason === "empty_after_reply_payload_sending_hook")
18181826
) {
1819-
deliveryState.intentionallySuppressed = true;
1827+
finalReplyOutcome = "suppressed";
18201828
}
18211829
},
18221830
onError: (err, info) => {
18231831
deliveryState.failedNonSilent += 1;
1832+
const partialDelivery = isChannelPartialDeliveryError(err);
1833+
if (partialDelivery) {
1834+
deliveryState.delivered = true;
1835+
logVerbose("telegram slash reply partially delivered before failure");
1836+
}
1837+
if (info.kind === "final") {
1838+
// A failed final outweighs any earlier suppression until a final delivers.
1839+
finalReplyOutcome = partialDelivery ? "accepted" : "failed";
1840+
}
18241841
runtime.error?.(danger(`telegram slash ${info.kind} reply failed: ${String(err)}`));
18251842
},
18261843
},
@@ -1835,8 +1852,8 @@ export const registerTelegramNativeCommands = ({
18351852
)(turnPlan);
18361853
if (
18371854
!deliveryState.delivered &&
1838-
!deliveryState.intentionallySuppressed &&
1839-
deliveryState.skippedNonSilent > 0 &&
1855+
finalReplyOutcome !== "suppressed" &&
1856+
(deliveryState.skippedNonSilent > 0 || deliveryState.failedNonSilent > 0) &&
18401857
(!turnResult.dispatched ||
18411858
turnResult.dispatchResult.sourceReplyDeliveryMode !== "message_tool_only" ||
18421859
deliveryState.failedNonSilent > 0)

0 commit comments

Comments
 (0)