Package: @hexos/runtime

Wraps an async operation with a timeout, rejecting with TimeoutError if it exceeds the limit.

Accepts either a Promise or a factory function that returns a Promise. The timer is cleaned up on both success and failure to prevent memory leaks. Used throughout AgentRuntime for tool execution timeouts, approval wait timeouts, and MCP request timeouts.

function withTimeout<T>(operation: Promise<T> | (() => Promise<T>), timeoutMs: number, timeoutMessage: string, timeoutCode:  = 'TIMEOUT'): Promise<T>

Parameters

operation

Promise | (() => Promise)

timeoutMs

number

timeoutMessage

string

timeoutCode