Skip to content

Commit 1027837

Browse files
authored
fix(msteams): avoid ambiguous delivery replay (#125127)
1 parent 0e3a3aa commit 1027837

14 files changed

Lines changed: 380 additions & 107 deletions

‎extensions/msteams/src/errors.test.ts‎

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
import { describe, expect, it, vi } from "vitest";
33
import {
44
classifyMSTeamsSendError,
5+
formatMSTeamsDeliveryFailureGuidance,
56
formatMSTeamsSendErrorHint,
67
formatUnknownError,
78
isRevokedProxyError,
89
} from "./errors.js";
910
import { withRevokedProxyFallback } from "./revoked-context.js";
1011

12+
function replaySafeRetryAfterMs(error: unknown): number | undefined {
13+
const classification = classifyMSTeamsSendError(error);
14+
return classification.kind === "replay-safe" ? classification.retryAfterMs : undefined;
15+
}
16+
1117
describe("msteams errors", () => {
1218
it("formats unknown errors", () => {
1319
expect(formatUnknownError("oops")).toBe("oops");
@@ -35,61 +41,59 @@ describe("msteams errors", () => {
3541
expect(result.errorCode).toBe("ContentStreamNotAllowed");
3642
});
3743

38-
it("classifies throttling errors and parses retry-after", () => {
44+
it("classifies Teams rate limiting as replay-safe and parses retry-after", () => {
3945
const result = classifyMSTeamsSendError({ statusCode: 429, retryAfter: "1.5" });
40-
expect(result.kind).toBe("throttled");
46+
expect(result.kind).toBe("replay-safe");
4147
expect(result.statusCode).toBe(429);
42-
expect(result.retryAfterMs).toBe(1500);
48+
expect(replaySafeRetryAfterMs({ statusCode: 429, retryAfter: "1.5" })).toBe(1500);
4349
});
4450

4551
it("does not parse partial retry-after values", () => {
52+
expect(replaySafeRetryAfterMs({ statusCode: 429, retryAfter: "1.5s" })).toBeUndefined();
4653
expect(
47-
classifyMSTeamsSendError({ statusCode: 429, retryAfter: "1.5s" }).retryAfterMs,
48-
).toBeUndefined();
49-
expect(
50-
classifyMSTeamsSendError({
54+
replaySafeRetryAfterMs({
5155
statusCode: 429,
5256
response: { headers: { "retry-after": "2 seconds" } },
53-
}).retryAfterMs,
57+
}),
5458
).toBeUndefined();
5559
expect(
56-
classifyMSTeamsSendError({
60+
replaySafeRetryAfterMs({
5761
statusCode: 429,
5862
response: { headers: new Headers({ "retry-after": "3 seconds" }) },
59-
}).retryAfterMs,
63+
}),
6064
).toBeUndefined();
6165
});
6266

6367
it("ignores unsafe retry-after magnitudes", () => {
6468
expect(
65-
classifyMSTeamsSendError({
69+
replaySafeRetryAfterMs({
6670
statusCode: 429,
6771
retryAfterMs: Number.MAX_SAFE_INTEGER + 1,
68-
}).retryAfterMs,
72+
}),
6973
).toBeUndefined();
7074
expect(
71-
classifyMSTeamsSendError({
75+
replaySafeRetryAfterMs({
7276
statusCode: 429,
7377
retryAfter: Number.MAX_SAFE_INTEGER,
74-
}).retryAfterMs,
78+
}),
7579
).toBeUndefined();
7680
expect(
77-
classifyMSTeamsSendError({
81+
replaySafeRetryAfterMs({
7882
statusCode: 429,
7983
retryAfter: "9007199254741",
80-
}).retryAfterMs,
84+
}),
8185
).toBeUndefined();
8286
expect(
83-
classifyMSTeamsSendError({
87+
replaySafeRetryAfterMs({
8488
statusCode: 429,
8589
response: { headers: { "retry-after": "9007199254741" } },
86-
}).retryAfterMs,
90+
}),
8791
).toBeUndefined();
8892
expect(
89-
classifyMSTeamsSendError({
93+
replaySafeRetryAfterMs({
9094
statusCode: 429,
9195
response: { headers: new Headers({ "retry-after": "9007199254741" }) },
92-
}).retryAfterMs,
96+
}),
9397
).toBeUndefined();
9498
});
9599

@@ -101,10 +105,30 @@ describe("msteams errors", () => {
101105
).toBe("unknown");
102106
});
103107

104-
it("classifies transient errors", () => {
105-
const result = classifyMSTeamsSendError({ statusCode: 503 });
106-
expect(result.kind).toBe("transient");
107-
expect(result.statusCode).toBe(503);
108+
it.each([408, 500, 502, 503, 504])("classifies HTTP %i as delivery-ambiguous", (statusCode) => {
109+
const classification = classifyMSTeamsSendError({ statusCode });
110+
expect(classification).toMatchObject({
111+
kind: "ambiguous",
112+
source: "http",
113+
statusCode,
114+
});
115+
expect(formatMSTeamsSendErrorHint(classification)).toContain("outcome is unknown");
116+
expect(formatMSTeamsSendErrorHint(classification)).not.toMatch(/retry|resend/iu);
117+
expect(formatMSTeamsDeliveryFailureGuidance(classification)).toContain(
118+
"may already have succeeded",
119+
);
120+
expect(formatMSTeamsDeliveryFailureGuidance(classification)).not.toContain(
121+
"Retrying later may succeed",
122+
);
123+
});
124+
125+
it("keeps model guidance aligned with replay safety", () => {
126+
expect(
127+
formatMSTeamsDeliveryFailureGuidance(classifyMSTeamsSendError({ statusCode: 429 })),
128+
).toBe("The request was rate-limited before delivery; retrying later may succeed.");
129+
expect(
130+
formatMSTeamsDeliveryFailureGuidance(classifyMSTeamsSendError({ statusCode: 401 })),
131+
).toBeUndefined();
108132
});
109133

110134
it("classifies permanent 4xx errors", () => {
@@ -114,13 +138,19 @@ describe("msteams errors", () => {
114138
});
115139

116140
it("provides actionable hints for common cases", () => {
117-
expect(formatMSTeamsSendErrorHint({ kind: "auth" })).toContain("msteams");
118-
expect(formatMSTeamsSendErrorHint({ kind: "throttled" })).toContain("throttled");
141+
expect(formatMSTeamsSendErrorHint(classifyMSTeamsSendError({ statusCode: 401 }))).toContain(
142+
"msteams",
143+
);
144+
expect(formatMSTeamsSendErrorHint(classifyMSTeamsSendError({ statusCode: 429 }))).toContain(
145+
"throttled",
146+
);
119147
expect(
120-
formatMSTeamsSendErrorHint({
121-
kind: "permanent",
122-
errorCode: "ContentStreamNotAllowed",
123-
}),
148+
formatMSTeamsSendErrorHint(
149+
classifyMSTeamsSendError({
150+
statusCode: 403,
151+
response: { body: { error: { code: "ContentStreamNotAllowed" } } },
152+
}),
153+
),
124154
).toContain("expired the content stream");
125155
});
126156

@@ -132,20 +162,35 @@ describe("msteams errors", () => {
132162
const etimedout = Object.assign(new Error("ETIMEDOUT"), { code: "ETIMEDOUT" });
133163

134164
const econnrefusedResult = classifyMSTeamsSendError(econnrefused);
135-
expect(econnrefusedResult.kind).toBe("network");
165+
expect(econnrefusedResult).toMatchObject({ kind: "ambiguous", source: "transport" });
136166
expect(econnrefusedResult.errorCode).toBe("ECONNREFUSED");
137167
const enotfoundResult = classifyMSTeamsSendError(enotfound);
138-
expect(enotfoundResult.kind).toBe("network");
168+
expect(enotfoundResult).toMatchObject({ kind: "ambiguous", source: "transport" });
139169
expect(enotfoundResult.errorCode).toBe("ENOTFOUND");
140170
const etimedoutResult = classifyMSTeamsSendError(etimedout);
141-
expect(etimedoutResult.kind).toBe("network");
171+
expect(etimedoutResult).toMatchObject({ kind: "ambiguous", source: "transport" });
142172
expect(etimedoutResult.errorCode).toBe("ETIMEDOUT");
143173

144-
// Hints for network errors must mention smba (Connector endpoint) and egress
145-
expect(formatMSTeamsSendErrorHint({ kind: "network" })).toContain("smba");
146-
expect(formatMSTeamsSendErrorHint({ kind: "network" })).toContain("egress");
174+
const hint = formatMSTeamsSendErrorHint(econnrefusedResult);
175+
expect(hint).toContain("smba");
176+
expect(hint).toContain("egress");
177+
expect(hint).toContain("outcome is unknown");
178+
expect(hint).not.toMatch(/retry|resend/iu);
147179
});
148180

181+
it.each(["ECONNABORTED", "ETIMEDOUT", "ECONNRESET"])(
182+
"keeps transport %s delivery ambiguous without retry guidance",
183+
(code) => {
184+
const classification = classifyMSTeamsSendError(Object.assign(new Error(code), { code }));
185+
expect(classification).toMatchObject({
186+
kind: "ambiguous",
187+
source: "transport",
188+
errorCode: code,
189+
});
190+
expect(formatMSTeamsSendErrorHint(classification)).not.toMatch(/retry|resend/iu);
191+
},
192+
);
193+
149194
it("still classifies HTTP errors as unknown when no status code and no network code", () => {
150195
expect(classifyMSTeamsSendError(new Error("unexpected error")).kind).toBe("unknown");
151196
expect(classifyMSTeamsSendError(null).kind).toBe("unknown");

‎extensions/msteams/src/errors.ts‎

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Msteams plugin module implements errors behavior.
22
import { asFiniteNumberInRange, parseStrictFiniteNumber } from "openclaw/plugin-sdk/number-runtime";
3-
import { parseRetryAfterHeaderSeconds } from "openclaw/plugin-sdk/retry-runtime";
3+
import {
4+
isTransientNetworkError,
5+
parseRetryAfterHeaderSeconds,
6+
} from "openclaw/plugin-sdk/retry-runtime";
47
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
58

69
const MAX_SAFE_RETRY_AFTER_SECONDS = Number.MAX_SAFE_INTEGER / 1000;
@@ -174,28 +177,28 @@ function parseNonNegativeRetryAfterSeconds(raw: string): number | undefined {
174177
});
175178
}
176179

177-
type MSTeamsSendErrorKind =
178-
| "auth"
179-
| "throttled"
180-
| "transient"
181-
| "permanent"
182-
| "network"
183-
| "unknown";
184-
185-
type MSTeamsSendErrorClassification = {
186-
kind: MSTeamsSendErrorKind;
180+
type MSTeamsSendErrorMetadata = {
187181
statusCode?: number;
188-
retryAfterMs?: number;
189182
errorCode?: string;
190183
};
191184

185+
type MSTeamsSendErrorClassification =
186+
| (MSTeamsSendErrorMetadata & { kind: "auth" })
187+
| (MSTeamsSendErrorMetadata & {
188+
kind: "replay-safe";
189+
statusCode: 429;
190+
retryAfterMs?: number;
191+
})
192+
| (MSTeamsSendErrorMetadata & {
193+
kind: "ambiguous";
194+
source: "http" | "transport";
195+
})
196+
| (MSTeamsSendErrorMetadata & { kind: "permanent" })
197+
| (MSTeamsSendErrorMetadata & { kind: "unknown" });
198+
192199
/**
193-
* Classify outbound send errors for safe retries and actionable logs.
194-
*
195-
* Important: We only mark errors as retryable when we have an explicit HTTP
196-
* status code that indicates the message was not accepted (e.g. 429, 5xx).
197-
* For transport-level errors where delivery is ambiguous, we prefer to avoid
198-
* retries to reduce the chance of duplicate posts.
200+
* Owns retry safety and delivery ambiguity for Teams send failures.
201+
* Only Teams rate-limit rejection proves this unkeyed POST safe to replay.
199202
*/
200203
export function classifyMSTeamsSendError(err: unknown): MSTeamsSendErrorClassification {
201204
const statusCode = extractStatusCode(err);
@@ -215,7 +218,7 @@ export function classifyMSTeamsSendError(err: unknown): MSTeamsSendErrorClassifi
215218

216219
if (statusCode === 429) {
217220
return {
218-
kind: "throttled",
221+
kind: "replay-safe",
219222
statusCode,
220223
retryAfterMs: retryAfterMs ?? undefined,
221224
errorCode,
@@ -224,9 +227,9 @@ export function classifyMSTeamsSendError(err: unknown): MSTeamsSendErrorClassifi
224227

225228
if (statusCode === 408 || (statusCode != null && statusCode >= 500)) {
226229
return {
227-
kind: "transient",
230+
kind: "ambiguous",
231+
source: "http",
228232
statusCode,
229-
retryAfterMs: retryAfterMs ?? undefined,
230233
errorCode,
231234
};
232235
}
@@ -235,25 +238,15 @@ export function classifyMSTeamsSendError(err: unknown): MSTeamsSendErrorClassifi
235238
return { kind: "permanent", statusCode, errorCode };
236239
}
237240

238-
// Transport-level errors (no HTTP status code) — check for well-known
239-
// network error codes that indicate egress is blocked (#77674).
240-
if (statusCode == null) {
241-
const networkCode = isRecord(err) && typeof err.code === "string" ? err.code : null;
242-
if (
243-
networkCode === "ECONNREFUSED" ||
244-
networkCode === "ENOTFOUND" ||
245-
networkCode === "EHOSTUNREACH" ||
246-
networkCode === "ETIMEDOUT" ||
247-
networkCode === "ECONNRESET"
248-
) {
249-
return { kind: "network", errorCode: networkCode };
250-
}
241+
// A transport failure can happen after the Connector accepted the POST.
242+
// Preserve its code for diagnostics without implying replay is safe.
243+
if (statusCode == null && isTransientNetworkError(err)) {
244+
return { kind: "ambiguous", source: "transport", errorCode };
251245
}
252246

253247
return {
254248
kind: "unknown",
255249
statusCode: statusCode ?? undefined,
256-
retryAfterMs: retryAfterMs ?? undefined,
257250
errorCode,
258251
};
259252
}
@@ -282,14 +275,26 @@ export function formatMSTeamsSendErrorHint(
282275
if (classification.errorCode === "ContentStreamNotAllowed") {
283276
return "Teams expired the content stream; stop streaming earlier and fall back to normal message delivery";
284277
}
285-
if (classification.kind === "throttled") {
278+
if (classification.kind === "replay-safe") {
286279
return "Teams throttled the bot; backing off may help";
287280
}
288-
if (classification.kind === "transient") {
289-
return "transient Teams/Bot Framework error; retry may succeed";
281+
if (classification.kind === "ambiguous") {
282+
if (classification.source === "transport") {
283+
return "transport-level failure sending to Teams Bot Connector (smba.trafficmanager.net); the outcome is unknown, so check egress and conversation history before taking further action";
284+
}
285+
return "Teams/Bot Framework delivery outcome is unknown; inspect the conversation history before taking further action";
286+
}
287+
return undefined;
288+
}
289+
290+
export function formatMSTeamsDeliveryFailureGuidance(
291+
classification: MSTeamsSendErrorClassification,
292+
): string | undefined {
293+
if (classification.kind === "replay-safe") {
294+
return "The request was rate-limited before delivery; retrying later may succeed.";
290295
}
291-
if (classification.kind === "network") {
292-
return "transport-level failure sending reply to Teams Bot Connector (smba.trafficmanager.net) — check egress firewall rules allow outbound HTTPS to smba.trafficmanager.net";
296+
if (classification.kind === "ambiguous") {
297+
return "Delivery may already have succeeded; do not send the same content again without checking the conversation.";
293298
}
294299
return undefined;
295300
}

0 commit comments

Comments
 (0)