Files
openclaw/src/cli/parse-timeout.ts
openclaw-clownfish[bot] 99d0bdc23a fix(cli): validate gateway RPC timeout inputs
Reject malformed or explicit empty Gateway RPC timeout values before opening Gateway calls, align the shared Gateway RPC omitted-timeout fallback with the 30000 ms CLI default, and validate explicit `cron add --timeout-seconds` values at the CLI boundary.

Carries forward the useful source work from #54646 and the earlier timeout-validation context from #40953. #60661 remains separate accepted-run timeout semantics work and is intentionally not folded into this change.

Validation:
- `npm run review-results -- /tmp/clownfish-check-27341769444`
- `git diff --check`
- OpenClaw PR checks on `ce7bd8b9388a5689b14ddc2b3a984f7b4647e5ca`: 132 pass, 0 pending, 0 failing
- ClawSweeper re-review: https://github.com/openclaw/clawsweeper/actions/runs/27344244608

Co-authored-by: RayRuan <43744645+ruanrrn@users.noreply.github.com>
Co-authored-by: Homeran <11574611+comeran@users.noreply.github.com>
2026-06-11 20:52:07 +09:00

70 lines
1.8 KiB
TypeScript

// Shared CLI timeout parsers for millisecond flags and config-backed fallbacks.
import { parseStrictPositiveInteger } from "../infra/parse-finite-number.js";
/** Parse a positive millisecond timeout, returning undefined for absent or invalid input. */
export function parseTimeoutMs(raw: unknown): number | undefined {
if (raw === undefined || raw === null) {
return undefined;
}
let value = Number.NaN;
if (typeof raw === "number") {
value = raw;
} else if (typeof raw === "bigint") {
value = Number(raw);
} else if (typeof raw === "string") {
const trimmed = raw.trim();
if (!trimmed) {
return undefined;
}
return parseStrictPositiveInteger(trimmed);
}
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
}
function invalidTimeout(value?: string): Error {
const suffix = value ? ` Received: "${value}".` : "";
return new Error(
`Invalid --timeout. Use a positive millisecond value, e.g. --timeout 30000.${suffix}`,
);
}
/** Parse a positive timeout or return the supplied fallback for missing values. */
export function parseTimeoutMsWithFallback(
raw: unknown,
fallbackMs: number,
options: {
invalidType?: "fallback" | "error";
} = {},
): number {
if (raw === undefined || raw === null) {
return fallbackMs;
}
const value =
typeof raw === "string"
? raw.trim()
: typeof raw === "number" || typeof raw === "bigint"
? String(raw)
: null;
if (value === null) {
if (options.invalidType === "error") {
throw invalidTimeout();
}
return fallbackMs;
}
if (!value) {
if (options.invalidType === "error") {
throw invalidTimeout();
}
return fallbackMs;
}
const parsed = parseStrictPositiveInteger(value);
if (parsed === undefined) {
throw invalidTimeout(value);
}
return parsed;
}