Skip to content

Commit 6c85b90

Browse files
committed
fix(replies): clamp block delivery timeouts
1 parent ef41560 commit 6c85b90

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/auto-reply/reply/block-reply-pipeline.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** Tests block reply pipeline buffering, dedupe, and final flush behavior. */
2+
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
23
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
34
import { getReplyPayloadMetadata, setReplyPayloadMetadata } from "../reply-payload.js";
45
import {
@@ -449,4 +450,30 @@ describe("createBlockReplyPipeline content coverage dedup", () => {
449450

450451
expect(pipeline.hasSentPayload({ text: "summary" })).toBe(false);
451452
});
453+
454+
it("clamps oversized delivery timeouts before arming timers", async () => {
455+
vi.useFakeTimers();
456+
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
457+
const observedTimeouts: number[] = [];
458+
const pipeline = createBlockReplyPipeline({
459+
onBlockReply: async (_payload, options) => {
460+
observedTimeouts.push(options?.timeoutMs ?? 0);
461+
await waitForAbort(options?.abortSignal);
462+
},
463+
timeoutMs: MAX_TIMER_TIMEOUT_MS + 1,
464+
});
465+
466+
pipeline.enqueue({ text: "slow block" });
467+
const flushing = pipeline.flush({ force: true });
468+
await Promise.resolve();
469+
470+
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
471+
expect(observedTimeouts).toEqual([MAX_TIMER_TIMEOUT_MS]);
472+
473+
await vi.advanceTimersByTimeAsync(MAX_TIMER_TIMEOUT_MS);
474+
await flushing;
475+
476+
expect(pipeline.isAborted()).toBe(true);
477+
setTimeoutSpy.mockRestore();
478+
});
452479
});

‎src/auto-reply/reply/block-reply-pipeline.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Buffers streaming reply blocks before coalesced final delivery.
2+
import { clampPositiveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
23
import {
34
hasOutboundReplyContent,
45
resolveSendableOutboundReplyParts,
@@ -95,6 +96,10 @@ const withTimeout = async <T>(
9596
}
9697
};
9798

99+
function resolveBlockReplyTimeoutMs(timeoutMs: number): number {
100+
return clampPositiveTimerTimeoutMs(timeoutMs) ?? 0;
101+
}
102+
98103
/** Creates the ordered block reply delivery pipeline for streamed payloads. */
99104
export function createBlockReplyPipeline(params: {
100105
onBlockReply: (
@@ -105,7 +110,8 @@ export function createBlockReplyPipeline(params: {
105110
coalescing?: BlockStreamingCoalescing;
106111
buffer?: BlockReplyBuffer;
107112
}): BlockReplyPipeline {
108-
const { onBlockReply, timeoutMs, coalescing, buffer } = params;
113+
const { onBlockReply, coalescing, buffer } = params;
114+
const timeoutMs = resolveBlockReplyTimeoutMs(params.timeoutMs);
109115
const sentKeys = new Set<string>();
110116
const sentContentKeys = new Set<string>();
111117
const sentMediaUrls = new Set<string>();

0 commit comments

Comments
 (0)