Package: @hexos/runtime

FIFO semaphore that limits the number of concurrent async operations.

When the concurrency limit is reached, subsequent acquire() calls queue up and wait for a slot to become available. Each successful acquire returns a release function that must be called when the operation completes to free the slot. The release function is idempotent — calling it multiple times has no effect.

Queue entries can time out via the timeoutMs parameter, throwing a SemaphoreTimeoutError. Used by AgentRuntime to limit concurrent tool executions via the maxConcurrentToolExecutions config.

class Semaphore {
    constructor(limit: number)
    acquire(timeoutMs: number, timeoutCode:  = 'SEMAPHORE_QUEUE_TIMEOUT', timeoutMessage:  = 'Semaphore acquire timeout') => Promise<() => void>;
    getInUseCount() => number;
    getQueueLength() => number;
}

constructor

(limit: number) => Semaphore

acquire

(timeoutMs: number, timeoutCode: = 'SEMAPHORE_QUEUE_TIMEOUT', timeoutMessage: = 'Semaphore acquire timeout') => Promise<() => void>

Acquires a concurrency slot, waiting in the FIFO queue if all slots are in use.

Returns a release function that must be called when the operation finishes. If the caller waits longer than timeoutMs, the request is removed from the queue and a SemaphoreTimeoutError is thrown.

getInUseCount

() => number

getQueueLength

() => number