Skip to content

Commit 5b4b09f

Browse files
fix(clownfish): address review for ghcrawl-156876-autonomous-smoke (1)
Co-authored-by: Langning Zhang <[email protected]> Co-authored-by: Sumaia Zaman <[email protected]> Co-authored-by: pick-cat <[email protected]>
1 parent 81fbe79 commit 5b4b09f

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

extensions/telegram/src/sendchataction-401-backoff.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ describe("createTelegramSendChatActionHandler", () => {
206206
expect(fn).toHaveBeenCalledTimes(2);
207207
});
208208

209-
it("rejects transient cooldown starts before same-chat coalescing", async () => {
210-
let now = 1000;
211-
const fn = vi.fn().mockRejectedValueOnce(makeTelegramError("Bad Gateway", 502));
209+
it("rejects transient keepalive ticks until same-chat coalescing expires", async () => {
210+
let now = 0;
211+
const fn = vi
212+
.fn()
213+
.mockRejectedValueOnce(makeTelegramError("Bad Gateway", 502))
214+
.mockResolvedValue(true);
212215
const logger = vi.fn();
213216
const handler = createTelegramSendChatActionHandler({
214217
sendChatActionFn: fn,
@@ -218,12 +221,19 @@ describe("createTelegramSendChatActionHandler", () => {
218221
});
219222

220223
await expect(handler.sendChatAction(-100, "typing")).rejects.toThrow("Bad Gateway");
224+
expect(logger.mock.calls.at(-1)).toEqual([
225+
"sendChatAction transient error (1). Cooling down 4000ms before retry.",
226+
]);
221227

222-
now = 1500;
228+
now = 3000;
223229
await expect(handler.sendChatAction(-100, "typing")).rejects.toThrow(
224230
"transient cooldown active",
225231
);
226232
expect(fn).toHaveBeenCalledTimes(1);
233+
234+
now = 4000;
235+
await handler.sendChatAction(-100, "typing");
236+
expect(fn).toHaveBeenCalledTimes(2);
227237
});
228238

229239
it("resets transient counters on non-transient errors", async () => {

extensions/telegram/src/sendchataction-401-backoff.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,15 @@ export function createTelegramSendChatActionHandler({
196196
} else if (isTransientSendChatActionError(error)) {
197197
consecutiveTransientFailures++;
198198
const cooldownMs = resolveTransientCooldownMs(error, consecutiveTransientFailures);
199-
transientCooldownUntilMs = now() + cooldownMs;
199+
const cooldownStartedAt = now();
200+
// Keep transient failures rejected through the same-chat coalesce window;
201+
// otherwise the next typing keepalive can look successful and reset its guard.
202+
const coalescingUntilMs = key ? attemptedAt + minIntervalMs : 0;
203+
transientCooldownUntilMs = Math.max(cooldownStartedAt + cooldownMs, coalescingUntilMs);
204+
const effectiveCooldownMs = Math.max(0, transientCooldownUntilMs - cooldownStartedAt);
200205
logger(
201206
`sendChatAction transient error (${consecutiveTransientFailures}). ` +
202-
`Cooling down ${cooldownMs}ms before retry.`,
207+
`Cooling down ${effectiveCooldownMs}ms before retry.`,
203208
);
204209
} else {
205210
clearTransientCooldown();

0 commit comments

Comments
 (0)