// engine/compact/header-parser.ts — SPEC-922: Extract X-Context-Used from MCP request meta
/** Parse a string value into a clamped [0,1] number. Returns undefined if invalid. */
function parseClamped(value) {
    const trimmed = value.trim();
    if (trimmed === '') {
        return undefined;
    }
    const parsed = Number(trimmed);
    if (!Number.isFinite(parsed)) {
        return undefined;
    }
    return Math.max(0, Math.min(1, parsed));
}
/**
 * Extract X-Context-Used from MCP request extra metadata.
 * Checks both _meta (stdio) and requestInfo.headers (HTTP).
 */
export function parseContextHeader(meta) {
    if (meta === undefined) {
        return undefined;
    }
    // 1. Check _meta['X-Context-Used'] (stdio transport convention)
    const metaValue = meta['X-Context-Used'];
    if (typeof metaValue === 'string') {
        return parseClamped(metaValue);
    }
    if (typeof metaValue === 'number' && Number.isFinite(metaValue)) {
        return Math.max(0, Math.min(1, metaValue));
    }
    // 2. Check requestInfo.headers (HTTP transport)
    const requestInfo = meta.requestInfo;
    if (requestInfo !== undefined) {
        const headers = requestInfo.headers;
        if (headers !== undefined) {
            const headerValue = headers['x-context-used'] ?? headers['X-Context-Used'];
            if (typeof headerValue === 'string') {
                return parseClamped(headerValue);
            }
        }
    }
    return undefined;
}
//# sourceMappingURL=header-parser.js.map