/**
 * In-flight tool call tracking for auto-mode idle detection.
 * Tracks which tool calls are currently executing so the idle watchdog
 * can distinguish "waiting for tool completion" from "truly idle".
 */
const inFlightTools = new Map();
/**
 * Tools that block waiting for human input by design.
 * The idle watchdog must not treat these as stalled.
 */
const INTERACTIVE_TOOLS = new Set(["ask_user_questions", "secure_env_collect"]);
/**
 * Mark a tool execution as in-flight.
 * Records start time and tool name so the idle watchdog can detect tools
 * hung longer than the idle timeout while exempting interactive tools.
 */
export function markToolStart(toolCallId, isActive, toolName) {
    if (!isActive)
        return;
    inFlightTools.set(toolCallId, { startedAt: Date.now(), toolName: toolName ?? "unknown" });
}
/**
 * Mark a tool execution as completed.
 */
export function markToolEnd(toolCallId) {
    inFlightTools.delete(toolCallId);
}
/**
 * Returns the age (ms) of the oldest currently in-flight tool, or 0 if none.
 */
export function getOldestInFlightToolAgeMs() {
    if (inFlightTools.size === 0)
        return 0;
    let oldestStart = Infinity;
    for (const t of inFlightTools.values()) {
        if (t.startedAt < oldestStart)
            oldestStart = t.startedAt;
    }
    return Date.now() - oldestStart;
}
/**
 * Returns the number of currently in-flight tools.
 */
export function getInFlightToolCount() {
    return inFlightTools.size;
}
/**
 * Returns the start timestamp of the oldest in-flight tool, or undefined if none.
 */
export function getOldestInFlightToolStart() {
    if (inFlightTools.size === 0)
        return undefined;
    let oldest = Infinity;
    for (const t of inFlightTools.values()) {
        if (t.startedAt < oldest)
            oldest = t.startedAt;
    }
    return oldest;
}
/**
 * Returns true if any currently in-flight tool is a user-interactive tool
 * (e.g. ask_user_questions, secure_env_collect) that blocks waiting for
 * human input. These must be exempt from idle stall detection.
 */
export function hasInteractiveToolInFlight() {
    for (const { toolName } of inFlightTools.values()) {
        if (INTERACTIVE_TOOLS.has(toolName))
            return true;
    }
    return false;
}
/**
 * Clear all in-flight tool tracking state.
 */
export function clearInFlightTools() {
    inFlightTools.clear();
}
