Skip to content

Commit 5eb28a3

Browse files
yetvalaltaywtf
andauthored
fix(outbound): retry queued deliveries when a channel adapter is unavailable (#126800)
* fix(outbound): retry queued deliveries when a channel adapter is unavailable Delivery queue recovery classified "Outbound not configured for channel" as a permanent provider rejection, so a queued message and its spooled attachment were dead-lettered on the first attempt whenever the channel adapter was momentarily unresolvable. That string is a local adapter-resolution failure thrown by createChannelHandler, not a provider verdict, and the live send path already treats it as retryable. Remove the pattern so adapter unavailability consumes the normal retry budget. * fix(outbound): type adapter lookup failures --------- Co-authored-by: Altay <[email protected]>
1 parent 0095168 commit 5eb28a3

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎src/infra/outbound/deliver-channel.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
DurableFinalDeliveryRequirements,
2828
OutboundDurableDeliverySupport,
2929
} from "./deliver-contracts.js";
30-
import type { OutboundDeliveryResult } from "./deliver-types.js";
30+
import { PlatformMessageNotDispatchedError, type OutboundDeliveryResult } from "./deliver-types.js";
3131
import {
3232
attachOutboundDeliveryCommitHook,
3333
type OutboundDeliveryCommitHook,
@@ -57,7 +57,8 @@ export async function createChannelHandler(params: ChannelHandlerParams): Promis
5757
return createPluginHandler({ ...params, outbound, message });
5858
});
5959
if (!handler) {
60-
throw new Error(`Outbound not configured for channel: ${params.channel}`);
60+
const message = `Outbound not configured for channel: ${params.channel}`;
61+
throw new PlatformMessageNotDispatchedError(message, { cause: new Error(message) });
6162
}
6263
return scopeChannelHandler(handler, pluginRegistry);
6364
}

‎src/infra/outbound/deliver.queue-adapter-availability.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../p
88
import { withPluginRuntimeRegistryScope } from "../../plugins/runtime/gateway-request-scope.js";
99
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
1010
import { getDeliveryQueueEntryStatus } from "../delivery-queue-sqlite.js";
11+
import { PlatformMessageNotDispatchedError } from "./deliver-types.js";
1112
import {
1213
boundedCronCompletionRetention,
1314
matrixOutboundForQueueTest,
1415
} from "./deliver.queue-integration.test-support.js";
1516
import { OUTBOUND_DELIVERY_QUEUE_NAME } from "./delivery-queue-media-staging.js";
1617
import { recoverPendingDeliveries, type DeliverFn } from "./delivery-queue-recovery.js";
17-
import { loadPendingDeliveries } from "./delivery-queue-storage.js";
18+
import { enqueueDelivery, loadPendingDeliveries } from "./delivery-queue-storage.js";
1819
import {
1920
createRecoveryLog,
2021
installDeliveryQueueTmpDirHooks,
@@ -132,6 +133,48 @@ describe("queued lazy outbound adapter availability", () => {
132133
).toBe("completed");
133134
});
134135

136+
it("retains recovery custody when no outbound adapter can be resolved", async () => {
137+
process.env.OPENCLAW_STATE_DIR = tmpDir;
138+
setActivePluginRegistry(createEmptyPluginRegistry());
139+
const id = await enqueueDelivery(
140+
{
141+
channel: "missing-adapter-test",
142+
to: "recipient",
143+
payloads: [{ text: "retry after adapter resolution" }],
144+
},
145+
tmpDir,
146+
);
147+
let deliveryError: unknown;
148+
const recoveryDeliver = vi.fn<DeliverFn>(async (params) => {
149+
try {
150+
return await deliverOutboundPayloads(params);
151+
} catch (error) {
152+
deliveryError = error;
153+
throw error;
154+
}
155+
});
156+
157+
const result = await recoverPendingDeliveries({
158+
cfg: {} as OpenClawConfig,
159+
deliver: recoveryDeliver,
160+
log: createRecoveryLog(),
161+
stateDir: tmpDir,
162+
});
163+
164+
expect(result).toMatchObject({ failed: 1, recovered: 0, skippedMaxRetries: 0 });
165+
expect(recoveryDeliver).toHaveBeenCalledOnce();
166+
expect(deliveryError).toBeInstanceOf(PlatformMessageNotDispatchedError);
167+
const pendingEntry = expectDefined(
168+
(await loadPendingDeliveries(tmpDir))[0],
169+
"retained adapter-miss delivery",
170+
);
171+
expect(pendingEntry).toMatchObject({ id, retryCount: 1 });
172+
expect(pendingEntry.recoveryState).toBeUndefined();
173+
expect(pendingEntry.platformSendAttemptId).toBeUndefined();
174+
expect(pendingEntry.platformSendStartedAt).toBeUndefined();
175+
expect(getDeliveryQueueEntryStatus(OUTBOUND_DELIVERY_QUEUE_NAME, id, tmpDir)).toBe("pending");
176+
});
177+
135178
it("does not replay a provider call that already crossed the ambiguous send boundary", async () => {
136179
process.env.OPENCLAW_STATE_DIR = tmpDir;
137180
setActivePluginRegistry(

‎src/infra/outbound/delivery-queue-recovery.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ const PERMANENT_ERROR_PATTERNS: readonly RegExp[] = [
9494
/forbidden: bot was kicked/i,
9595
/chat_id is empty/i,
9696
/recipient is not a valid/i,
97-
/outbound not configured for channel/i,
9897
/ambiguous .* recipient/i,
9998
/User .* not in room/i,
10099
];

0 commit comments

Comments
 (0)