function collectEvidence(decisions) {
    return decisions.flatMap((decision) => decision.evidence);
}
function modelAdvice(input) {
    const risk = input.spec?.risk ?? 'medium';
    const effort = risk === 'critical'
        ? input.policy.modelEffort.maxRisk
        : risk === 'high'
            ? input.policy.modelEffort.highRisk
            : risk === 'low'
                ? input.policy.modelEffort.lowRisk
                : input.policy.modelEffort.default;
    return {
        effort,
        reason: `Selected ${effort} effort from spec risk ${risk}.`,
        evidence: [
            { source: 'spec', key: 'risk', value: risk },
            { source: 'policy', key: 'modelEffort' },
        ],
    };
}
function escapeInert(text) {
    return text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
export function redactSnippet(text, patterns, maxChars) {
    const truncated = text.slice(0, maxChars);
    return patterns.reduce((current, pattern) => {
        const re = new RegExp(pattern, 'gi');
        return current.replace(re, '[redacted]');
    }, truncated);
}
function sanitizeDecision(decision, patterns, maxChars) {
    return {
        ...decision,
        target: redactSnippet(decision.target, patterns, maxChars),
        reason: redactSnippet(decision.reason, patterns, maxChars),
        evidence: decision.evidence.map((item) => ({
            ...item,
            ...(typeof item.value === 'string'
                ? { value: redactSnippet(item.value, patterns, maxChars) }
                : {}),
        })),
    };
}
function sanitizeDecisions(decisions, input) {
    return decisions.map((decision) => sanitizeDecision(decision, input.policy.redaction.redactPatterns, input.policy.redaction.maxSnippetChars));
}
export function buildTokenWasteReport(input) {
    const actionsTaken = sanitizeDecisions([
        ...(input.context?.summarize ?? []),
        ...(input.context?.exclude ?? []),
        ...(input.output?.decisions ?? []),
    ], input);
    const recommendations = sanitizeDecisions([...(input.context?.include ?? []), ...(input.tools?.recommended ?? [])], input);
    const risks = sanitizeDecisions([...(input.tools?.avoided ?? []), ...(input.loops?.warnings ?? [])], input);
    const modelEffort = modelAdvice(input);
    return {
        actionsTaken,
        recommendations,
        risks,
        evidence: [
            ...collectEvidence(actionsTaken),
            ...collectEvidence(recommendations),
            ...collectEvidence(risks),
            ...modelEffort.evidence,
        ],
        modelEffort,
    };
}
export function formatTokenWasteReport(report) {
    const lines = ['## Token Waste Autopilot', ''];
    lines.push(`Model effort: ${escapeInert(report.modelEffort.effort)} — ${escapeInert(report.modelEffort.reason)}`);
    if (report.actionsTaken.length > 0) {
        lines.push('', 'Actions taken:');
        for (const item of report.actionsTaken.slice(0, 6)) {
            lines.push(`- ${escapeInert(item.decision)}: ${escapeInert(item.target)} — ${escapeInert(item.reason)}`);
        }
    }
    if (report.recommendations.length > 0) {
        lines.push('', 'Recommendations:');
        for (const item of report.recommendations.slice(0, 6)) {
            lines.push(`- ${escapeInert(item.target)}: ${escapeInert(item.reason)}`);
        }
    }
    if (report.risks.length > 0) {
        lines.push('', 'Risks:');
        for (const item of report.risks.slice(0, 6)) {
            lines.push(`- ${escapeInert(item.target)}: ${escapeInert(item.reason)}`);
        }
    }
    return lines.join('\n');
}
//# sourceMappingURL=autopilot.js.map