Files
openclaw/src/agents/timeout.ts
Tyler Yust 191da1feb5 fix: context overflow compaction and subagent announce improvements (#11664) (thanks @tyler6204)
* initial commit

* feat: implement deriveSessionTotalTokens function and update usage tests

* Added deriveSessionTotalTokens function to calculate total tokens based on usage and context tokens.
* Updated usage tests to include cases for derived session total tokens.
* Refactored session usage calculations in multiple files to utilize the new function for improved accuracy.

* fix: restore overflow truncation fallback + changelog/test hardening (#11551) (thanks @tyler6204)
2026-02-07 20:02:32 -08:00

49 lines
1.6 KiB
TypeScript

import type { OpenClawConfig } from "../config/config.js";
const DEFAULT_AGENT_TIMEOUT_SECONDS = 600;
const MAX_SAFE_TIMEOUT_MS = 2_147_000_000;
const normalizeNumber = (value: unknown): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? Math.floor(value) : undefined;
export function resolveAgentTimeoutSeconds(cfg?: OpenClawConfig): number {
const raw = normalizeNumber(cfg?.agents?.defaults?.timeoutSeconds);
const seconds = raw ?? DEFAULT_AGENT_TIMEOUT_SECONDS;
return Math.max(seconds, 1);
}
export function resolveAgentTimeoutMs(opts: {
cfg?: OpenClawConfig;
overrideMs?: number | null;
overrideSeconds?: number | null;
minMs?: number;
}): number {
const minMs = Math.max(normalizeNumber(opts.minMs) ?? 1, 1);
const clampTimeoutMs = (valueMs: number) =>
Math.min(Math.max(valueMs, minMs), MAX_SAFE_TIMEOUT_MS);
const defaultMs = clampTimeoutMs(resolveAgentTimeoutSeconds(opts.cfg) * 1000);
// Use the maximum timer-safe timeout to represent "no timeout" when explicitly set to 0.
const NO_TIMEOUT_MS = MAX_SAFE_TIMEOUT_MS;
const overrideMs = normalizeNumber(opts.overrideMs);
if (overrideMs !== undefined) {
if (overrideMs === 0) {
return NO_TIMEOUT_MS;
}
if (overrideMs < 0) {
return defaultMs;
}
return clampTimeoutMs(overrideMs);
}
const overrideSeconds = normalizeNumber(opts.overrideSeconds);
if (overrideSeconds !== undefined) {
if (overrideSeconds === 0) {
return NO_TIMEOUT_MS;
}
if (overrideSeconds < 0) {
return defaultMs;
}
return clampTimeoutMs(overrideSeconds * 1000);
}
return defaultMs;
}