Skip to content

Commit 98805b8

Browse files
authored
fix(auto-reply): reject redeliveries without recreating drained queues (#124723)
enqueueFollowupRun called getFollowupQueue (which inserts into the process-global FOLLOWUP_QUEUES registry) before the recent-message-id dedupe peek. A provider redelivery arriving after the original queue drained and self-deleted recreated the registry entry, was rejected by the peek, and left an empty FollowupQueueState (AbortController, arrays, WeakSets) in the map forever — registry entries are only deleted when a drain finishes, and a queue with no items never drains. Long-lived gateways leaked one state object per redelivered session key. Root cause: mutation-before-validation ordering. The dedupe key derivation does not need the queue, so the peek now runs first and the registry entry is only created for admitted work. Regression: new dedupe test drains a queue to self-deletion, replays the message, and asserts no registry entry reappears — fails pre-fix.
1 parent 8f23e68 commit 98805b8

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎src/auto-reply/reply/queue.dedupe.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
installQueueRuntimeErrorSilencer,
1616
} from "./queue.test-helpers.js";
1717
import { resetRecentQueuedMessageIdDedupe } from "./queue/enqueue.test-support.js";
18+
import { getExistingFollowupQueue } from "./queue/state.js";
1819

1920
installQueueRuntimeErrorSilencer();
2021

@@ -252,6 +253,47 @@ describe("followup queue deduplication", () => {
252253
}
253254
});
254255

256+
it("does not leave an empty registry entry when rejecting a redelivery after the queue drained", async () => {
257+
const key = `test-dedup-registry-leak-${Date.now()}`;
258+
const { calls, done, runFollowup } = createFollowupCollector();
259+
260+
expect(
261+
enqueueFollowupRun(
262+
key,
263+
createRun({
264+
prompt: "original",
265+
messageId: "leak-1",
266+
originatingChannel: "discord",
267+
originatingTo: "channel:123",
268+
}),
269+
collectSettings,
270+
),
271+
).toBe(true);
272+
scheduleFollowupDrain(key, runFollowup);
273+
await done.promise;
274+
// Let the drain finish and self-delete the empty queue from the registry.
275+
await vi.waitFor(() => {
276+
expect(getExistingFollowupQueue(key)).toBeUndefined();
277+
});
278+
expect(calls).toHaveLength(1);
279+
280+
// A provider redelivery of the same message must be rejected without
281+
// recreating a registry entry that nothing would ever delete again.
282+
expect(
283+
enqueueFollowupRun(
284+
key,
285+
createRun({
286+
prompt: "original (redelivery)",
287+
messageId: "leak-1",
288+
originatingChannel: "discord",
289+
originatingTo: "channel:123",
290+
}),
291+
collectSettings,
292+
),
293+
).toBe(false);
294+
expect(getExistingFollowupQueue(key)).toBeUndefined();
295+
});
296+
255297
it("does not collide recent message-id keys when routing contains delimiters", async () => {
256298
const key = `test-dedup-key-collision-${Date.now()}`;
257299
const { done, runFollowup } = createFollowupCollector();

‎src/auto-reply/reply/queue/enqueue.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,14 @@ export function enqueueFollowupRun(
139139
if (options.steerCandidate) {
140140
run.steerAnchor = true;
141141
}
142-
const queue = getFollowupQueue(key, settings);
142+
// Peek before getFollowupQueue: rejecting a redelivery after the original
143+
// queue drained and self-deleted must not recreate an empty registry entry,
144+
// which nothing would ever delete again.
143145
const recentMessageIdKey = dedupeMode !== "none" ? buildRecentMessageIdKey(run, key) : undefined;
144146
if (recentMessageIdKey && peekRecentQueueMessageId(recentMessageIdKey)) {
145147
return false;
146148
}
149+
const queue = getFollowupQueue(key, settings);
147150

148151
const dedupe =
149152
dedupeMode === "none"

0 commit comments

Comments
 (0)