"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PermissionPromptBroker = void 0;
const DEFAULT_PROMPT_TIMEOUT_MS = 300_000;
function resolveDefaultTimeout() {
    // Override via env so unattended/headless runs can fail fast (e.g.
    // 30s) instead of stalling for the full 5 minutes before the model
    // gets the denial. Invalid or non-positive values fall back to the
    // built-in default.
    const raw = process.env.HARNESS_PERMISSION_PROMPT_TIMEOUT_MS;
    if (!raw)
        return DEFAULT_PROMPT_TIMEOUT_MS;
    const parsed = Number(raw);
    if (!Number.isFinite(parsed) || parsed <= 0)
        return DEFAULT_PROMPT_TIMEOUT_MS;
    return Math.floor(parsed);
}
class PermissionPromptBroker {
    pending = new Map();
    timeoutMs;
    constructor(timeoutMs) {
        this.timeoutMs = timeoutMs ?? resolveDefaultTimeout();
    }
    request(call, reason) {
        const id = createPromptId();
        return new Promise((resolve) => {
            const timer = setTimeout(() => {
                // Embed the tool name and the original reason so the model gets
                // an actionable error rather than the bare "Permission prompt
                // timed out" string. Common autonomous-run remediations: add
                // the destination to allowed-paths, lower
                // HARNESS_PERMISSION_PROMPT_TIMEOUT_MS, or run with permission
                // mode 'auto'. The hint is appended once per timeout.
                const detail = reason ? ` (${reason})` : '';
                const hint = ` Add the path to allowed-external-paths, set HARNESS_PERMISSION_PROMPT_TIMEOUT_MS, or rerun with permission mode 'auto'.`;
                this.resolve(id, false, `Permission prompt for '${call.name}' timed out after ${this.timeoutMs}ms${detail}.${hint}`);
            }, this.timeoutMs);
            this.pending.set(id, {
                id,
                call,
                reason,
                createdAt: new Date().toISOString(),
                resolve,
                timer,
            });
        });
    }
    list() {
        return Array.from(this.pending.values()).map(({ resolve: _resolve, timer: _timer, ...prompt }) => prompt);
    }
    resolve(id, allowed, reason) {
        const prompt = this.pending.get(id);
        if (!prompt)
            return false;
        clearTimeout(prompt.timer);
        this.pending.delete(id);
        prompt.resolve({ allowed, reason: reason ?? (allowed ? 'Approved by user' : 'Denied by user') });
        return true;
    }
    clear() {
        for (const prompt of this.pending.values()) {
            clearTimeout(prompt.timer);
            prompt.resolve({ allowed: false, reason: 'Permission prompts cleared' });
        }
        this.pending.clear();
    }
}
exports.PermissionPromptBroker = PermissionPromptBroker;
function createPromptId() {
    return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
}
//# sourceMappingURL=promptBroker.js.map