UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

1,383 lines • 59.3 kB
import { proxyTokenUsage } from "./proxyTokenUsage.js";
import { createReadStream } from "node:fs";
import { isDeepStrictEqual } from "node:util";
import { isProxyAuxiliaryRequest } from "./proxyRequestKind.js";
import { lstat, readdir, realpath, stat } from "node:fs/promises";
import { homedir } from "node:os";
import { createInterface } from "node:readline";
import { isAbsolute, join, relative, resolve, sep } from "node:path";
import { ACCOUNT_COOLING_REASONS, PROXY_ACCOUNT_TYPES, PROXY_ACCOUNT_ROUTING_MODES, PROXY_ACCOUNT_ROUTING_REASONS, PROXY_ACCOUNT_ROUTING_STRATEGIES, } from "./routingEvidence.js";
import { calculateCost, hasPricing, isExactPricingMatch, } from "../utils/pricing.js";
const LIFECYCLE_FILE_PATTERN = /^proxy-(?:lifecycle|supervisor)-\d{4}-\d{2}-\d{2}\.jsonl$/;
const REQUEST_FILE_PATTERN = /^proxy-\d{4}-\d{2}-\d{2}\.jsonl$/;
const ATTEMPT_FILE_PATTERN = /^proxy-attempts-\d{4}-\d{2}-\d{2}\.jsonl$/;
const DEBUG_FILE_PATTERN = /^proxy-debug-\d{4}-\d{2}-\d{2}\.jsonl$/;
const ARTIFACT_STAT_CONCURRENCY = 64;
const LIFECYCLE_EVENTS = new Set([
    "runtime_sample",
    "supervisor_event",
    "request_accepted",
    "response_headers",
    "response_first_chunk",
    "request_terminal",
]);
const ROUTING_STRATEGIES = new Set(PROXY_ACCOUNT_ROUTING_STRATEGIES);
const ROUTING_MODES = new Set(PROXY_ACCOUNT_ROUTING_MODES);
const ROUTING_REASONS = new Set(PROXY_ACCOUNT_ROUTING_REASONS);
const ROUTING_ACCOUNT_TYPES = new Set(PROXY_ACCOUNT_TYPES);
const ROUTING_RANKINGS = new Set([
    "expiry-first",
    "headroom-first",
]);
const AFFINITY_SKIP_REASONS = new Set([
    "unusable",
    "session_saturated",
    "expired",
    "no_session",
    "disabled",
]);
const COOLING_REASONS = new Set(ACCOUNT_COOLING_REASONS);
const QUOTA_FRESHNESS_VALUES = new Set([
    "unknown",
    "fresh",
    "stale_known",
    "refresh_due",
]);
const QUOTA_REFRESH_REASONS = new Set([
    "startup_unknown",
    "handoff_prewarm",
    "ambiguous_snapshot",
    "manual",
]);
const QUOTA_SATURATION_KINDS = new Set(["none", "soft", "hard"]);
const MAX_RETAINED_ROUTING_RECORDS = 200;
const MAX_ROUTING_RECORDS_BEFORE_COMPACTION = MAX_RETAINED_ROUTING_RECORDS * 2;
function isContainedPath(root, candidate) {
    const candidateRelative = relative(root, candidate);
    return (candidateRelative.length > 0 &&
        !isAbsolute(candidateRelative) &&
        candidateRelative !== ".." &&
        !candidateRelative.startsWith(`..${sep}`));
}
async function inspectBodyArtifact(artifactPath, canonicalBodiesRoot) {
    if (!canonicalBodiesRoot) {
        return "missing";
    }
    try {
        const canonicalArtifactPath = await realpath(artifactPath);
        if (!isContainedPath(canonicalBodiesRoot, canonicalArtifactPath)) {
            return "invalid";
        }
        return (await stat(canonicalArtifactPath)).isFile() ? "present" : "missing";
    }
    catch {
        return "missing";
    }
}
function increment(counter, key) {
    counter[key] = (counter[key] ?? 0) + 1;
}
function finiteNumber(value) {
    return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function stringValue(value) {
    return typeof value === "string" && value.length > 0 ? value : null;
}
function isNullableString(value) {
    return value === null || (typeof value === "string" && value.length > 0);
}
function isNullableFiniteNumber(value) {
    return value === null || finiteNumber(value) !== null;
}
function routingCandidateValue(value) {
    if (!value || typeof value !== "object" || Array.isArray(value)) {
        return null;
    }
    const candidate = value;
    const requiredBooleanFields = [
        "configuredPrimary",
        "usable",
        "saturated",
        "quotaObserved",
        "coolingActive",
    ];
    const requiredNullableNumberFields = [
        "quotaLastUpdated",
        "quotaAgeMs",
        "coolingUntil",
        "sessionUsed",
        "sessionResetAt",
        "sessionResetBucket",
        "weeklyUsed",
        "weeklyResetAt",
    ];
    const requiredNullableStringFields = [
        "unifiedStatus",
        "overageStatus",
        "sessionStatus",
        "weeklyStatus",
    ];
    const optionalNullableStringFields = [
        "fallbackStatus",
        "upgradePaths",
        "scopedModel",
        "scopedStatus",
    ];
    const optionalNullableNumberFields = ["scopedUsed", "scopedResetAt"];
    if (!stringValue(candidate.account) ||
        typeof candidate.accountType !== "string" ||
        !ROUTING_ACCOUNT_TYPES.has(candidate.accountType) ||
        !Number.isInteger(candidate.sourceIndex) ||
        candidate.sourceIndex < 0 ||
        !Number.isInteger(candidate.rank) ||
        candidate.rank < 0 ||
        requiredBooleanFields.some((field) => typeof candidate[field] !== "boolean") ||
        requiredNullableNumberFields.some((field) => !isNullableFiniteNumber(candidate[field])) ||
        requiredNullableStringFields.some((field) => !isNullableString(candidate[field])) ||
        optionalNullableStringFields.some((field) => field in candidate &&
            candidate[field] !== undefined &&
            !isNullableString(candidate[field])) ||
        ("quotaStale" in candidate &&
            candidate.quotaStale !== undefined &&
            typeof candidate.quotaStale !== "boolean") ||
        ("overageEligible" in candidate &&
            candidate.overageEligible !== undefined &&
            typeof candidate.overageEligible !== "boolean") ||
        ("quotaFreshness" in candidate &&
            candidate.quotaFreshness !== undefined &&
            (typeof candidate.quotaFreshness !== "string" ||
                !QUOTA_FRESHNESS_VALUES.has(candidate.quotaFreshness))) ||
        ("refreshNeeded" in candidate &&
            candidate.refreshNeeded !== undefined &&
            typeof candidate.refreshNeeded !== "boolean") ||
        ("refreshInFlight" in candidate &&
            candidate.refreshInFlight !== undefined &&
            typeof candidate.refreshInFlight !== "boolean") ||
        ("refreshReason" in candidate &&
            candidate.refreshReason !== undefined &&
            candidate.refreshReason !== null &&
            (typeof candidate.refreshReason !== "string" ||
                !QUOTA_REFRESH_REASONS.has(candidate.refreshReason))) ||
        [
            "lastRefreshAttemptAt",
            "lastRefreshSuccessAt",
            "nextRefreshEligibleAt",
            ...optionalNullableNumberFields,
        ].some((field) => field in candidate &&
            candidate[field] !== undefined &&
            !isNullableFiniteNumber(candidate[field])) ||
        ("saturationKind" in candidate &&
            candidate.saturationKind !== undefined &&
            (typeof candidate.saturationKind !== "string" ||
                !QUOTA_SATURATION_KINDS.has(candidate.saturationKind))) ||
        ("softLimitOverrideReason" in candidate &&
            candidate.softLimitOverrideReason !== undefined &&
            candidate.softLimitOverrideReason !== null &&
            candidate.softLimitOverrideReason !== "overage" &&
            candidate.softLimitOverrideReason !== "weekly_expiry") ||
        !(candidate.coolingReason === null ||
            (typeof candidate.coolingReason === "string" &&
                COOLING_REASONS.has(candidate.coolingReason)))) {
        return null;
    }
    return {
        account: candidate.account,
        accountType: candidate.accountType,
        sourceIndex: candidate.sourceIndex,
        rank: candidate.rank,
        configuredPrimary: candidate.configuredPrimary,
        usable: candidate.usable,
        saturated: candidate.saturated,
        quotaObserved: candidate.quotaObserved,
        quotaStale: candidate.quotaStale === true,
        quotaFreshness: candidate.quotaFreshness,
        refreshNeeded: candidate.refreshNeeded,
        refreshReason: candidate.refreshReason,
        refreshInFlight: candidate.refreshInFlight,
        lastRefreshAttemptAt: candidate.lastRefreshAttemptAt,
        lastRefreshSuccessAt: candidate.lastRefreshSuccessAt,
        nextRefreshEligibleAt: candidate.nextRefreshEligibleAt,
        saturationKind: candidate.saturationKind,
        softLimitOverrideReason: candidate.softLimitOverrideReason,
        quotaLastUpdated: candidate.quotaLastUpdated,
        quotaAgeMs: candidate.quotaAgeMs,
        coolingActive: candidate.coolingActive,
        coolingReason: candidate.coolingReason,
        coolingUntil: candidate.coolingUntil,
        unifiedStatus: candidate.unifiedStatus,
        fallbackStatus: candidate.fallbackStatus,
        upgradePaths: candidate.upgradePaths,
        overageEligible: candidate.overageEligible,
        overageStatus: candidate.overageStatus,
        sessionStatus: candidate.sessionStatus,
        sessionUsed: candidate.sessionUsed,
        sessionResetAt: candidate.sessionResetAt,
        sessionResetBucket: candidate.sessionResetBucket,
        weeklyStatus: candidate.weeklyStatus,
        weeklyUsed: candidate.weeklyUsed,
        weeklyResetAt: candidate.weeklyResetAt,
        scopedModel: candidate.scopedModel,
        scopedStatus: candidate.scopedStatus,
        scopedUsed: candidate.scopedUsed,
        scopedResetAt: candidate.scopedResetAt,
    };
}
function plainObject(value) {
    return value && typeof value === "object" && !Array.isArray(value)
        ? value
        : null;
}
function routingPolicyValue(value) {
    const policy = plainObject(value);
    if (!policy ||
        typeof policy.ranking !== "string" ||
        !ROUTING_RANKINGS.has(policy.ranking) ||
        typeof policy.preferPrimary !== "boolean" ||
        typeof policy.sessionAffinity !== "boolean" ||
        !Number.isInteger(policy.sessionAffinityIdleTtlMs) ||
        !Number.isInteger(policy.spillInflight)) {
        return undefined;
    }
    return {
        ranking: policy.ranking,
        preferPrimary: policy.preferPrimary,
        sessionAffinity: policy.sessionAffinity,
        sessionAffinityIdleTtlMs: policy.sessionAffinityIdleTtlMs,
        spillInflight: policy.spillInflight,
    };
}
function routingAffinityValue(value) {
    const affinity = plainObject(value);
    if (!affinity ||
        typeof affinity.sessionBound !== "boolean" ||
        !isNullableString(affinity.boundAccount) ||
        typeof affinity.applied !== "boolean" ||
        (affinity.skippedReason !== null &&
            (typeof affinity.skippedReason !== "string" ||
                !AFFINITY_SKIP_REASONS.has(affinity.skippedReason)))) {
        return undefined;
    }
    return {
        sessionBound: affinity.sessionBound,
        boundAccount: affinity.boundAccount,
        applied: affinity.applied,
        skippedReason: affinity.skippedReason,
    };
}
function routingSpillValue(value) {
    const spill = plainObject(value);
    const from = stringValue(spill?.from);
    const to = stringValue(spill?.to);
    if (!spill ||
        !from ||
        !to ||
        !Number.isInteger(spill.inflight) ||
        spill.inflight < 0) {
        return undefined;
    }
    return { from, to, inflight: spill.inflight };
}
function routingDecisionValue(value) {
    if (!value || typeof value !== "object" || Array.isArray(value)) {
        return null;
    }
    const decision = value;
    const candidates = Array.isArray(decision.candidates)
        ? decision.candidates.map(routingCandidateValue)
        : [];
    if (decision.schemaVersion !== 1 ||
        !stringValue(decision.evaluatedAt) ||
        !Number.isFinite(Date.parse(String(decision.evaluatedAt))) ||
        typeof decision.strategy !== "string" ||
        !ROUTING_STRATEGIES.has(decision.strategy) ||
        typeof decision.mode !== "string" ||
        !ROUTING_MODES.has(decision.mode) ||
        typeof decision.selectionReason !== "string" ||
        !ROUTING_REASONS.has(decision.selectionReason) ||
        typeof decision.quotaRoutingEnabled !== "boolean" ||
        typeof decision.quotaInputsUsed !== "boolean" ||
        finiteNumber(decision.sessionSoftLimit) === null ||
        decision.sessionSoftLimit <= 0 ||
        decision.sessionSoftLimit > 1 ||
        !Number.isInteger(decision.sessionResetToleranceMs) ||
        decision.sessionResetToleranceMs <= 0 ||
        !isNullableString(decision.configuredPrimaryAccount) ||
        typeof decision.configuredPrimaryMatched !== "boolean" ||
        !Number.isInteger(decision.rotationOffset) ||
        decision.rotationOffset < 0 ||
        !stringValue(decision.initialAccount) ||
        candidates.length === 0 ||
        candidates.some((candidate) => candidate === null)) {
        return null;
    }
    return {
        schemaVersion: 1,
        evaluatedAt: decision.evaluatedAt,
        strategy: decision.strategy,
        mode: decision.mode,
        selectionReason: decision.selectionReason,
        quotaRoutingEnabled: decision.quotaRoutingEnabled,
        quotaInputsUsed: decision.quotaInputsUsed,
        sessionSoftLimit: decision.sessionSoftLimit,
        sessionResetToleranceMs: decision.sessionResetToleranceMs,
        configuredPrimaryAccount: decision.configuredPrimaryAccount,
        configuredPrimaryMatched: decision.configuredPrimaryMatched,
        rotationOffset: decision.rotationOffset,
        initialAccount: decision.initialAccount,
        candidates: candidates,
        ...optionalEvidence("policy", routingPolicyValue(decision.policy)),
        ...optionalEvidence("affinity", routingAffinityValue(decision.affinity)),
        ...optionalEvidence("spill", routingSpillValue(decision.spill)),
    };
}
// Evidence is optional per decision; a field that is absent or malformed is
// left out rather than rejecting a decision that is otherwise valid.
function optionalEvidence(key, value) {
    return value === undefined
        ? {}
        : { [key]: value };
}
function percentile(sorted, fraction) {
    if (sorted.length === 0) {
        return null;
    }
    const index = Math.max(0, Math.ceil(sorted.length * fraction) - 1);
    return Number(sorted[index].toFixed(3));
}
function summarizeLatency(values) {
    const sorted = values.filter(Number.isFinite).sort((a, b) => a - b);
    return {
        count: sorted.length,
        p50: percentile(sorted, 0.5),
        p95: percentile(sorted, 0.95),
        p99: percentile(sorted, 0.99),
        max: sorted.length ? Number(sorted[sorted.length - 1].toFixed(3)) : null,
    };
}
function parseSince(value, nowMs) {
    const relative = /^(\d+(?:\.\d+)?)(m|h|d|w)$/i.exec(value.trim());
    if (relative) {
        const amount = Number(relative[1]);
        const unitMs = {
            m: 60_000,
            h: 3_600_000,
            d: 86_400_000,
            w: 604_800_000,
        };
        return nowMs - amount * unitMs[relative[2].toLowerCase()];
    }
    const parsed = Date.parse(value);
    if (!Number.isFinite(parsed)) {
        throw new Error(`Invalid --since value "${value}". Use an ISO timestamp or a duration such as 6h, 1d, or 1w.`);
    }
    return parsed;
}
function parseUntil(value, nowMs) {
    let parsed;
    try {
        parsed = parseSince(value, nowMs);
    }
    catch {
        throw new Error(`Invalid --until value "${value}". Use an ISO timestamp or a duration such as 6h, 1d, or 1w.`);
    }
    if (parsed > nowMs) {
        throw new Error(`Invalid --until value "${value}". It must not be later than the analysis start time.`);
    }
    return parsed;
}
async function readJsonLines(filePath, onRecord, onMalformed) {
    let linesRead = 0;
    const lines = createInterface({
        input: createReadStream(filePath, { encoding: "utf8" }),
        crlfDelay: Infinity,
    });
    for await (const line of lines) {
        if (!line.trim()) {
            continue;
        }
        linesRead += 1;
        try {
            const parsed = JSON.parse(line);
            if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
                onMalformed();
                continue;
            }
            onRecord(parsed);
        }
        catch {
            onMalformed();
        }
    }
    return linesRead;
}
function accountEntry(accounts, account, accountType) {
    const key = `${accountType}:${account}`;
    const existing = accounts.get(key);
    if (existing) {
        return existing;
    }
    const created = {
        account,
        accountType,
        attempts: 0,
        attemptErrors: 0,
        finalRequests: 0,
        finalErrors: 0,
        transientRateLimits: 0,
        quotaRateLimits: 0,
        unclassifiedRateLimits: 0,
    };
    accounts.set(key, created);
    return created;
}
function summarizeFinalRequests(finalRequests, terminalStreamErrors, attemptsByRequest, accounts) {
    let clientFinals = 0;
    let internalFinals = 0;
    let success = 0;
    let errors = 0;
    let finalRateLimits = 0;
    let recoveredAfterRetry = 0;
    let requestsWithUsage = 0;
    let requestsWithCacheRead = 0;
    // Turns whose cache breakdown the provider actually reported. A turn that
    // reported none cannot count as a miss, so it is excluded from the hit-rate
    // denominator rather than folded in as a zero.
    let requestsWithCacheObservation = 0;
    let cacheReadTokens = 0;
    let cacheCreationTokens = 0;
    let inputTokens = 0;
    let outputTokens = 0;
    let estimatedCostUsd = 0;
    let requestsPriced = 0;
    let requestsPricedByPrefix = 0;
    let requestsUnpriced = 0;
    const modelsPricedByPrefix = new Set();
    const unpricedModels = new Set();
    const finalRequestLatency = [];
    const singleAttemptDelta = [];
    const errorTypes = {};
    const errorCodes = {};
    for (const [requestId, request] of finalRequests) {
        if (request.accountingScope === "internal") {
            internalFinals++;
        }
        else {
            clientFinals++;
            const failed = request.status >= 400 ||
                !!request.errorType ||
                terminalStreamErrors.has(requestId);
            if (failed) {
                errors += 1;
            }
            else {
                success += 1;
            }
            finalRateLimits += request.status === 429 ? 1 : 0;
            const accountStats = accountEntry(accounts, request.account, request.accountType);
            accountStats.finalRequests += 1;
            accountStats.finalErrors += failed ? 1 : 0;
            if (failed) {
                increment(errorTypes, terminalStreamErrors.has(requestId)
                    ? "stream_error"
                    : (request.errorType ?? `http_${request.status}`));
                if (request.errorCode) {
                    increment(errorCodes, request.errorCode);
                }
            }
            if (request.durationMs !== null && request.durationMs >= 0) {
                finalRequestLatency.push(request.durationMs);
            }
            const requestAttempts = attemptsByRequest.get(requestId);
            recoveredAfterRetry += !failed && requestAttempts?.hadError ? 1 : 0;
            if (requestAttempts?.count === 1 &&
                requestAttempts.durationCount === 1 &&
                request.durationMs !== null &&
                Number.isFinite(requestAttempts.totalDurationMs)) {
                singleAttemptDelta.push(request.durationMs - requestAttempts.totalDurationMs);
            }
        }
        // Only the designated owner contributes provider usage. Client bridge
        // records retain outcome/latency while their linked child owns token cost.
        if (request.usageOwnerRequestId &&
            request.usageOwnerRequestId !== requestId) {
            continue;
        }
        if (request.inputTokens !== null ||
            request.outputTokens !== null ||
            request.cacheReadTokens !== null ||
            request.cacheCreationTokens !== null) {
            const billingUsage = proxyTokenUsage({
                inputIncludesCachedTokens: request.inputIncludesCachedTokens,
                inputTokens: request.inputTokens ?? 0,
                outputTokens: request.outputTokens ?? 0,
                cacheCreationTokens: request.cacheCreationTokens ?? 0,
                cacheReadTokens: request.cacheReadTokens ?? 0,
                ...(request.cacheCreation1hTokens !== null
                    ? { cacheCreation1hTokens: request.cacheCreation1hTokens }
                    : {}),
            });
            requestsWithUsage += 1;
            inputTokens += billingUsage.input;
            outputTokens += request.outputTokens ?? 0;
            cacheReadTokens += request.cacheReadTokens ?? 0;
            cacheCreationTokens += request.cacheCreationTokens ?? 0;
            if (request.cacheReadTokensObserved !== false) {
                requestsWithCacheObservation += 1;
                requestsWithCacheRead += (request.cacheReadTokens ?? 0) > 0 ? 1 : 0;
            }
            if (request.model) {
                // Records written before `provider` existed carry only a model name.
                // "openai-compatible" resolves to the cross-provider table search in
                // pricing.ts, which finds the model wherever it lives — a far better
                // guess than assuming Anthropic and pricing a GPT model at $0.
                const cost = calculateCost(request.provider ?? "openai-compatible", request.model, billingUsage);
                // Ask the table directly rather than inferring from cost > 0: a real
                // request with trivial usage can round to $0.000000 and is priced, not
                // unpriced.
                const priced = hasPricing(request.provider ?? "openai-compatible", request.model);
                if (priced) {
                    estimatedCostUsd += cost;
                    requestsPriced += 1;
                    // A prefix fallback means the rate was inherited from a
                    // similarly-named model, not quoted for this one. Surface it rather
                    // than presenting a guess as a figure.
                    if (!isExactPricingMatch(request.provider ?? "openai-compatible", request.model)) {
                        requestsPricedByPrefix += 1;
                        modelsPricedByPrefix.add(request.model);
                    }
                }
                else {
                    requestsUnpriced += 1;
                    unpricedModels.add(request.model);
                }
            }
        }
    }
    return {
        requestTotals: {
            completed: clientFinals,
            internalCompleted: internalFinals,
            success,
            errors,
            finalRateLimits,
            recoveredAfterRetry,
            errorTypes,
            errorCodes,
        },
        cache: {
            requestsWithUsage,
            requestsWithCacheRead,
            requestsWithCacheObservation,
            cacheReadTokens,
            cacheCreationTokens,
            inputTokens,
            outputTokens,
            estimatedCostUsd: Number(estimatedCostUsd.toFixed(6)),
            requestsPriced,
            requestsPricedByPrefix,
            modelsPricedByPrefix: [...modelsPricedByPrefix].sort(),
            requestsUnpriced,
            unpricedModels: [...unpricedModels].sort(),
            requestHitRate: requestsWithCacheObservation > 0
                ? Number((requestsWithCacheRead / requestsWithCacheObservation).toFixed(4))
                : null,
        },
        finalRequestLatency,
        singleAttemptDelta,
    };
}
function summarizeRouting(finalRequests) {
    const modes = {};
    const selectionReasons = {};
    const initialAccounts = {};
    const retainedRecords = [];
    let totalRecords = 0;
    let finalAccountChanges = 0;
    let finalOutsideCandidateSet = 0;
    for (const [requestId, request] of finalRequests) {
        const decision = request.routingDecision;
        if (!decision) {
            continue;
        }
        increment(modes, decision.mode);
        increment(selectionReasons, decision.selectionReason);
        increment(initialAccounts, decision.initialAccount);
        const finalCandidate = decision.candidates.some((candidate) => candidate.account === request.account &&
            candidate.accountType === request.accountType);
        if (!finalCandidate) {
            finalOutsideCandidateSet += 1;
        }
        else if (decision.initialAccount !== request.account) {
            finalAccountChanges += 1;
        }
        totalRecords += 1;
        retainedRecords.push({
            record: {
                requestId,
                timestamp: request.timestamp,
                responseStatus: request.status,
                finalAccount: request.account,
                finalAccountType: request.accountType,
                decision,
            },
            timestampMs: Date.parse(request.timestamp),
            sequence: totalRecords,
        });
        if (retainedRecords.length > MAX_ROUTING_RECORDS_BEFORE_COMPACTION) {
            retainedRecords.sort((left, right) => left.timestampMs - right.timestampMs ||
                left.sequence - right.sequence);
            retainedRecords.splice(0, retainedRecords.length - MAX_RETAINED_ROUTING_RECORDS);
        }
    }
    retainedRecords.sort((left, right) => left.timestampMs - right.timestampMs || left.sequence - right.sequence);
    const records = retainedRecords
        .slice(-MAX_RETAINED_ROUTING_RECORDS)
        .map(({ record }) => record);
    return {
        modes,
        selectionReasons,
        initialAccounts,
        finalAccountChanges,
        finalOutsideCandidateSet,
        totalRecords,
        records,
    };
}
async function discoverLogFiles(logsDir) {
    const entries = await readdir(logsDir, { withFileTypes: true }).catch((error) => {
        throw new Error(`Unable to read proxy logs at ${logsDir}: ${error.message}`);
    });
    const matching = (pattern) => entries
        .filter((entry) => entry.isFile() && pattern.test(entry.name))
        .map((entry) => join(logsDir, entry.name))
        .sort();
    return {
        lifecycleFiles: matching(LIFECYCLE_FILE_PATTERN),
        requestFiles: matching(REQUEST_FILE_PATTERN),
        attemptFiles: matching(ATTEMPT_FILE_PATTERN),
        debugFiles: matching(DEBUG_FILE_PATTERN),
    };
}
/** Analyze local proxy logs without reading request or response body artifacts. */
export async function analyzeProxyLogs(options) {
    const nowMs = options?.nowMs ?? Date.now();
    const sinceMs = parseSince(options?.since ?? "24h", nowMs);
    const untilMs = options?.until ? parseUntil(options.until, nowMs) : nowMs;
    if (untilMs < sinceMs) {
        throw new Error(`Invalid analysis window: --until must not be earlier than --since.`);
    }
    const logsDir = resolve(options?.logsDir ?? join(homedir(), ".neurolink", "logs"));
    const { lifecycleFiles, requestFiles, attemptFiles, debugFiles } = await discoverLogFiles(logsDir);
    const observedRanges = {
        lifecycle: { from: null, to: null },
        requests: { from: null, to: null },
        attempts: { from: null, to: null },
        debug: { from: null, to: null },
    };
    const observeTimestamp = (stream, record) => {
        const timestamp = Date.parse(String(record.timestamp ?? ""));
        if (!Number.isFinite(timestamp)) {
            return null;
        }
        const range = observedRanges[stream];
        range.from =
            range.from === null ? timestamp : Math.min(range.from, timestamp);
        range.to = range.to === null ? timestamp : Math.max(range.to, timestamp);
        return timestamp;
    };
    let linesRead = 0;
    let malformedLines = 0;
    let unsupportedLifecycleLines = 0;
    const accepted = new Set();
    const internalRequests = new Set();
    const auxiliaryRequests = new Set();
    const headers = new Set();
    const firstChunks = new Set();
    const terminal = new Set();
    const terminalOutcomes = {};
    const lifecycleErrorTypes = {};
    const lifecycleErrorCodes = {};
    const headersLatencyByRequest = new Map();
    const firstChunkLatencyByRequest = new Map();
    const terminalLatencyByRequest = new Map();
    const sequences = new Map();
    const selectedSequenceRanges = new Map();
    const seenLifecycleEvents = new Map();
    let conflictingLifecycleDuplicates = 0;
    const conflictedRequests = new Set();
    const terminalRecords = new Map();
    const acceptedWorkers = new Map();
    const admittedWorkerIds = new Set();
    const conflictedWorkerExits = new Set();
    const conflictingIdentities = new Set();
    const runtimeRecords = new Map();
    const workerExits = new Map();
    const runtime = {
        samples: 0,
        maxEventLoopDelayMs: null,
        maxRssBytes: null,
        maxCpuPercentOneCore: null,
        maxHostLoad1m: null,
    };
    for (const filePath of lifecycleFiles) {
        linesRead += await readJsonLines(filePath, (record) => {
            const operational = record.event === "runtime_sample" ||
                record.event === "supervisor_event";
            const operationalTimestamp = Date.parse(String(record.timestamp));
            const timestamp = operational
                ? Number.isFinite(operationalTimestamp)
                    ? operationalTimestamp
                    : null
                : observeTimestamp("lifecycle", record);
            const requestId = stringValue(record.requestId);
            if (timestamp === null || !requestId) {
                return;
            }
            if (record.accountingScope === "internal") {
                internalRequests.add(requestId);
            }
            const event = stringValue(record.event);
            if (record.schemaVersion !== 1 ||
                !event ||
                !LIFECYCLE_EVENTS.has(event) ||
                !requestId) {
                if (accepted.has(requestId) ||
                    (timestamp >= sinceMs && timestamp <= untilMs)) {
                    unsupportedLifecycleLines += 1;
                }
                return;
            }
            const processId = stringValue(record.processInstanceId);
            const sequence = finiteNumber(record.sequence);
            if (processId && sequence !== null && Number.isInteger(sequence)) {
                const values = sequences.get(processId) ?? [];
                values.push(sequence);
                sequences.set(processId, values);
            }
            const details = record.supervisorEvent && typeof record.supervisorEvent === "object"
                ? record.supervisorEvent
                : undefined;
            const exitedWorkerId = stringValue(details?.workerProcessInstanceId);
            const relatedExit = event === "supervisor_event" &&
                details?.type === "worker_exit" &&
                exitedWorkerId &&
                admittedWorkerIds.has(exitedWorkerId);
            // Audit intervening sequences before selecting the request cohort.
            if (!accepted.has(requestId) &&
                !relatedExit &&
                (timestamp < sinceMs || timestamp > untilMs)) {
                return;
            }
            if (processId && sequence !== null && Number.isInteger(sequence)) {
                const range = selectedSequenceRanges.get(processId);
                selectedSequenceRanges.set(processId, {
                    min: Math.min(range?.min ?? sequence, sequence),
                    max: Math.max(range?.max ?? sequence, sequence),
                });
                const identity = `${processId}:${sequence}`;
                const previous = seenLifecycleEvents.get(identity);
                if (previous) {
                    if (!isDeepStrictEqual(previous, record)) {
                        conflictingLifecycleDuplicates += 1;
                        conflictingIdentities.add(identity);
                        for (const copy of [previous, record]) {
                            const detail = copy.supervisorEvent;
                            const workerId = stringValue(detail?.workerProcessInstanceId);
                            if (workerId) {
                                conflictedWorkerExits.add(workerId);
                            }
                        }
                        conflictedRequests.add(requestId);
                        const previousRequestId = stringValue(previous.requestId);
                        if (previousRequestId) {
                            conflictedRequests.add(previousRequestId);
                        }
                    }
                    return;
                }
                seenLifecycleEvents.set(identity, record);
            }
            if (event === "runtime_sample") {
                if (record.runtimeSample &&
                    typeof record.runtimeSample === "object" &&
                    processId &&
                    sequence !== null) {
                    runtimeRecords.set(`${processId}:${sequence}`, record.runtimeSample);
                }
                return;
            }
            if (event === "supervisor_event") {
                if (details?.type === "worker_exit" && exitedWorkerId) {
                    const exit = { ...details, at: record.timestamp };
                    const previous = workerExits.get(exitedWorkerId);
                    if (previous && !isDeepStrictEqual(previous, exit)) {
                        conflictedWorkerExits.add(exitedWorkerId);
                    }
                    workerExits.set(exitedWorkerId, exit);
                }
                return;
            }
            if (isProxyAuxiliaryRequest(stringValue(record.method) ?? "", stringValue(record.path) ?? "")) {
                auxiliaryRequests.add(requestId);
            }
            const elapsed = finiteNumber(record.elapsedMs);
            if (event === "request_accepted") {
                accepted.add(requestId);
                if (processId) {
                    acceptedWorkers.set(requestId, processId);
                    admittedWorkerIds.add(processId);
                }
            }
            else if (event === "response_headers") {
                if (headers.has(requestId)) {
                    return;
                }
                headers.add(requestId);
                if (elapsed !== null && elapsed >= 0) {
                    headersLatencyByRequest.set(requestId, elapsed);
                }
            }
            else if (event === "response_first_chunk") {
                if (firstChunks.has(requestId)) {
                    return;
                }
                firstChunks.add(requestId);
                if (elapsed !== null && elapsed >= 0) {
                    firstChunkLatencyByRequest.set(requestId, elapsed);
                }
            }
            else {
                if (terminal.has(requestId)) {
                    return;
                }
                terminal.add(requestId);
                terminalRecords.set(requestId, record);
                if (elapsed !== null && elapsed >= 0) {
                    terminalLatencyByRequest.set(requestId, elapsed);
                }
            }
        }, () => {
            malformedLines += 1;
        });
    }
    for (const [identity, sample] of runtimeRecords) {
        if (conflictingIdentities.has(identity)) {
            continue;
        }
        runtime.samples += 1;
        const mapping = {
            maxEventLoopDelayMs: "eventLoopDelayMaxMs",
            maxRssBytes: "rssBytes",
            maxCpuPercentOneCore: "cpuPercentOneCore",
            maxHostLoad1m: "hostLoad1m",
        };
        for (const [key, source] of Object.entries(mapping)) {
            const value = finiteNumber(sample[source]);
            if (value !== null && value >= 0) {
                const target = key;
                runtime[target] = Math.max(runtime[target] ?? value, value);
            }
        }
    }
    // Contradictory copies are not reliable latency samples. Keep their data
    // quality count, but do not choose one timing arbitrarily.
    const verifiedLatencies = (values) => [...values]
        .filter(([id]) => !conflictedRequests.has(id) && !internalRequests.has(id))
        .map(([, ms]) => ms);
    let lifecycleSequenceGaps = 0;
    let lifecycleSequenceDuplicates = 0;
    for (const [processId, allValues] of sequences) {
        const range = selectedSequenceRanges.get(processId);
        if (!range) {
            continue;
        }
        const values = allValues.filter((value) => value >= range.min && value <= range.max);
        values.sort((a, b) => a - b);
        for (let index = 1; index < values.length; index += 1) {
            const difference = values[index] - values[index - 1];
            if (difference === 0) {
                lifecycleSequenceDuplicates += 1;
            }
            else if (difference > 1) {
                lifecycleSequenceGaps += difference - 1;
            }
        }
    }
    const accounts = new Map();
    const attemptsByRequest = new Map();
    const attemptLatency = [];
    let totalAttempts = 0;
    let totalAttemptErrors = 0;
    const attemptErrorTypes = {};
    const attemptErrorCodes = {};
    const attemptTransportScopes = {};
    let attemptRateLimits = 0;
    let transientRateLimits = 0;
    let quotaRateLimits = 0;
    let unclassifiedRateLimits = 0;
    const uniqueAttempts = new Map();
    let duplicateAttempts = 0;
    for (const filePath of attemptFiles) {
        linesRead += await readJsonLines(filePath, (record) => {
            const timestamp = observeTimestamp("attempts", record);
            const requestId = stringValue(record.requestId);
            const parentId = stringValue(record.parentRequestId) ??
                requestId?.replace(/:codex-fallback$/, "");
            if (timestamp === null ||
                !requestId ||
                (!accepted.has(parentId ?? requestId) &&
                    (timestamp < sinceMs || timestamp > untilMs))) {
                return;
            }
            const status = finiteNumber(record.responseStatus);
            if (!requestId || status === null) {
                return;
            }
            const identity = `${requestId}:${String(record.attempt ?? record.timestamp)}`;
            const previous = uniqueAttempts.get(identity);
            if (previous) {
                duplicateAttempts += 1;
            }
            // A later terminal update enriches the same attempt, never creates a
            // second upstream call. Preserve failure evidence across enrichment.
            uniqueAttempts.set(identity, previous
                ? {
                    ...previous,
                    ...record,
                    ...((previous.errorType ||
                        Number(previous.responseStatus) >= 400) &&
                        !record.errorType &&
                        status < 400
                        ? {
                            errorType: previous.errorType,
                            responseStatus: previous.responseStatus,
                            errorCode: previous.errorCode,
                        }
                        : {}),
                }
                : record);
        }, () => {
            malformedLines += 1;
        });
    }
    for (const record of uniqueAttempts.values()) {
        const rawRequestId = String(record.requestId);
        const requestId = stringValue(record.parentRequestId) ??
            rawRequestId.replace(/:codex-fallback$/, "");
        const status = Number(record.responseStatus);
        const duration = finiteNumber(record.attemptDurationMs);
        const account = stringValue(record.account) ?? "unknown";
        const accountType = stringValue(record.accountType) ?? "unknown";
        const accountStats = accountEntry(accounts, account, accountType);
        totalAttempts += 1;
        accountStats.attempts += 1;
        const hadError = status >= 400 || !!stringValue(record.errorType);
        if (hadError) {
            totalAttemptErrors += 1;
            accountStats.attemptErrors += 1;
            increment(attemptErrorTypes, stringValue(record.errorType) ?? `http_${status}`);
            const errorCode = stringValue(record.errorCode);
            if (errorCode) {
                increment(attemptErrorCodes, errorCode);
            }
        }
        const transportScope = stringValue(record.transportScope);
        if (transportScope) {
            increment(attemptTransportScopes, transportScope);
        }
        const requestAttempts = attemptsByRequest.get(requestId) ?? {
            count: 0,
            hadError: false,
            totalDurationMs: 0,
            durationCount: 0,
        };
        requestAttempts.count += 1;
        requestAttempts.hadError = requestAttempts.hadError || hadError;
        if (duration !== null && duration >= 0) {
            requestAttempts.totalDurationMs += duration;
            requestAttempts.durationCount += 1;
            attemptLatency.push(duration);
        }
        attemptsByRequest.set(requestId, requestAttempts);
        if (status === 429) {
            attemptRateLimits += 1;
            if (record.rateLimitKind === "transient") {
                transientRateLimits += 1;
                accountStats.transientRateLimits += 1;
            }
            else if (record.rateLimitKind === "quota") {
                quotaRateLimits += 1;
                accountStats.quotaRateLimits += 1;
            }
            else {
                unclassifiedRateLimits += 1;
                accountStats.unclassifiedRateLimits += 1;
            }
        }
    }
    const finalRequests = new Map();
    const terminalStreamErrors = new Set();
    let validRoutingDecisions = 0;
    let invalidRoutingDecisions = 0;
    let absentRoutingDecisions = 0;
    for (const filePath of requestFiles) {
        linesRead += await readJsonLines(filePath, (record) => {
            const timestamp = observeTimestamp("requests", record);
            if (timestamp === null) {
                return;
            }
            const requestId = stringValue(record.requestId);
            if (!requestId) {
                return;
            }
            // A streamed request is logged twice — once when the response headers
            // are known, again when the body finishes and its token counts arrive
            // — and those two writes can straddle the window edge. A Codex turn
            // whose headers land at 23:59:50 and whose stream ends at 00:00:05 has
            // every token it spent in the second record. Filtering that record out
            // by its own timestamp would leave the request counted as completed
            // but contributing nothing to tokens or cost, with nothing in the
            // report to say so. A request the window already admitted therefore
            // keeps accepting its own later records.
            const alreadyAdmitted = accepted.has(requestId) ||
                finalRequests.has(requestId) ||
                terminalStreamErrors.has(requestId);
            if (!alreadyAdmitted && (timestamp < sinceMs || timestamp > untilMs)) {
                return;
            }
            if (finiteNumber(record.terminalStatus) !== null) {
                terminalStreamErrors.add(requestId);
                return;
            }
            const status = finiteNumber(record.responseStatus);
            if (status === null || !stringValue(record.method)) {
                return;
            }
            const hasRoutingDecision = Object.prototype.hasOwnProperty.call(record, "routingDecision");
            const routingDecision = hasRoutingDecision
                ? routingDecisionValue(record.routingDecision)
                : null;
            if (routingDecision) {
                validRoutingDecisions += 1;
            }
            else if (hasRoutingDecision) {
                invalidRoutingDecisions += 1;
            }
            else {
                absentRoutingDecisions += 1;
            }
            if (record.accountingScope === "internal") {
                internalRequests.add(requestId);
            }
            const parsed = {
                accountingScope: internalRequests.has(requestId)
                    ? "internal"
                    : "client",
                parentRequestId: stringValue(record.parentRequestId) ?? undefined,
                usageOwnerRequestId: stringValue(record.usageOwnerRequestId) ?? undefined,
                firstUsefulOutputMs: finiteNumber(record.firstUsefulOutputMs),
                timestamp: new Date(timestamp).toISOString(),
                status,
                durationMs: finiteNumber(record.responseTimeMs),
                account: stringValue(record.account) ?? "unknown",
                accountType: stringValue(record.accountType) ?? "unknown",
                model: stringValue(record.model),
                provider: stringValue(record.provider),
                inputIncludesCachedTokens: typeof record.inputIncludesCachedTokens === "boolean"
                    ? record.inputIncludesCachedTokens
                    : typeof record.path === "string" &&
                        record.path.includes("/backend-api/codex/"),
                inputTokens: finiteNumber(record.inputTokens),
                outputTokens: finiteNumber(record.outputTokens),
                cacheReadTokens: finiteNumber(record.cacheReadTokens),
                cacheCreationTokens: finiteNumber(record.cacheCreationTokens),
                cacheCreation1hTokens: finiteNumber(record.cacheCreation1hTokens),
                // Read as a boolean only. An absent flag means observed, which keeps
                // every record written before the flag existed counting as it did.
                // The merge below drops undefined from the later record, so a
                // headers-only first record cannot erase a flag the body record sets.
                cacheReadTokensObserved: typeof record.cacheReadTokensObserved === "boolean"
                    ? record.cacheReadTokensObserved
                    : undefined,
                cacheCreationTokensObserved: typeof record.cacheCreationTokensObserved === "boolean"
                    ? record.cacheCreationTokensObserved
                    : undefined,
                errorType: stringValue(record.errorType),
                errorCode: stringValue(record.errorCode),
                routingDecision,
            };
            // A request may be logged twice: once when the response headers are
            // known, and again when a streamed body finishes and its token counts
            // become available (the Codex engine does this). Merge rather than
            // replace, so the later usage-bearing record cannot drop an errorType
            // the first one carried, and vice versa.
            const previous = finalRequests.get(requestId);
            finalRequests.set(requestId, previous
                ? {
                    ...previous,
                    ...Object.fromEntries(Object.entries(parsed).filter(([, value]) => value !== null && value !== undefined)),
                    ...(previous.status >= 400 && status < 400
                        ? { status: previous.status }
                        : {}),
                    // Attribute the request to when it was first seen. A late
                    // completion record must not move it out of the window that
                    // admitted it.
                    timestamp: previous.timestamp,
                }
                : parsed);
        }, () => {
            malformedLines += 1;
        });
    }
    // Old lifecycle records described transport EOF as success even when the
    // final request recorded a semantic failure. Reconcile, and expose every
    // disagreement instead of letting a choice of input file change the answer.
    let finalOutcomeConflicts = 0;
    for (const key of Object.keys(terminalOutcomes)) {
        delete terminalOutcomes[key];
    }
    for (const [requestId, record] of terminalRecords) {
        const final = finalRequests.get(requestId);
        const recorded = stringValue(record.terminalOutcome) ?? "unknown";
        const auxiliaryTransport = auxiliaryRequests.has(requestId)
            ? (stringValue(record.transportOutcome) ?? recorded)
            : null;
        const resolved = final
            ? final.status === 499 || final.errorType === "client_cancelled"
                ? "client_cancelled"
                : final.errorType?.includes("stream") ||
                    terminalStreamErrors.has(requestId)
                    ? "stream_error"
                    : final.status >= 400 || final.errorType
                        ? "handler_error"
                        : "completed"
            : auxiliaryTransport && !conflictedRequests.has(requestId)
                ? auxiliaryTransport === "completed" ||
                    auxiliaryTransport === "bodyless"
                    ? finiteNumber(record.responseStatus) === null
                        ? "unknown"
                        : Number(record.responseStatus) >= 400
                            ? "handler_error"
                            : auxiliaryTransport
                    : auxiliaryTransport
                : conflictedRequests.has(requestId) ||
                    recorded === "completed" ||
                    recorded === "bodyless"
                    ? "unknown"
                    : recorded;
        if (final &&
            recorded !== resolved &&
            !(recorded === "bodyless" && resolved === "completed")) {
            finalOutcomeConflicts += 1;
        }
        if (!internalRequests.has(requestId)) {
            increment(terminalOutcomes, resolved);
            const errorType = stringValue(record.errorType);
            const errorCode = stringValue(record.errorCode);
            if (errorType) {
                increment(lifecycleErrorTypes, errorType);
            }
            if (errorCode) {
                increment(lifecycleErrorCodes, errorCode);
            }
        }
    }
    let capturesIndexed = 0;
    let truncatedCaptures = 0;
    let writeFailures = 0;
    let invalidPaths = 0;
    const referencedArtifacts = new Set();
    const bodiesRoot = resolve(logsDir, "bodies");
    for (const filePath of debugFiles) {
        linesRead += await readJsonLines(filePath, (record) => {
            const timestamp = observeTimestamp("debug", record);
            if (timestamp === null || timestamp < sinceMs || timestamp > untilMs) {
                return;
            }
            if (record.type !== "body_capture") {
                return;
            }
            capturesIndexed += 1;
            truncatedCaptures += record.bodyTruncated === true ? 1 : 0;
            writeFailures += record.bodyWriteFailed === true ? 1 : 0;
            const bodyPath = stringValue(record.bodyPath);
            if (!bodyPath) {
                return;
            }
            if (bodyPath.includes("\0") || bodyPath.split(/[\\/]/).includes("..")) {
                invalidPaths += 1;
                return;
            }
            const resolvedBodyPath = resolve(logsDir, bodyPath);
            if (!isContainedPath(bodiesRoot, resolvedBodyPath)) {
                invalidPaths += 1;
                return;
            }
            referencedArtifacts.add(resolvedBodyPath);
        }, () => {
            malformedLines += 1;
        });
    }
    const artifactPaths = [...referencedArtifacts];
    let canonicalBodiesRoot = null;
    let bodiesRootUnsafe = false;
    try {
        const bodiesRootStat = await lstat(bodiesRoot);
        if (bodiesRootStat.isDirectory() && !bodiesRootStat.isSymbolicLink()) {
            canonicalBodiesRoot = await realpath(bodiesRoot);
        }
        else {
            bodiesRootUnsafe = true;
        }
    }
    catch {
        // A missing body directory means every lexically valid reference is absent.
    }
    let artifactsPresent = 0;
    let artifactsMissing = 0;
    for (let offset = 0; offset < artifactPaths.length; offset += ARTIFACT_STAT_CONCURRENCY) {
        const presence = await Promise.all(artifactPaths
            .slice(offset, offset + ARTIFACT_STAT_CONCURRENCY)
            .map((artifactPath) => bodiesRootUnsafe
            ? Promise.resolve("invalid")
            : inspectBodyArtifact(artifactPath, canonicalBodiesRoot)));
        artifactsPresent += presence.filter((value) => value === "present").length;
        artifactsMissing += presence.filter((value) => value === "missing").length;
        invalidPaths += presence.filter((value) => value === "invalid").length;
    }
    const artifactsReferenced = artifactsPresent + artifactsMissing;
    const finalSummary = summarizeFinalRequests(finalRequests, terminalStreamErrors, attemptsByRequest, accounts);
    const routingSummary = summarizeRouting(finalRequests);
    const streamComplete = (stream) => {
        const range = observedRanges[stream];
        return range.from !== null && range.from <= sinceMs;
    };
    return {
        generatedAt: new Date(nowMs).toISOString(),
        since: new Date(sinceMs).toISOString(),
        until: new Date(untilMs).toISOString(),
        logsDir,
        files: {
            lifecycle: lifecycleFiles.length,
            requests: requestFiles.length,
            attempts: attemptFiles.length,
            debug: debugFiles.length,
        },
        coverage: {
            lifecycle: accepted.size + headers.size + firstChunks.size + terminal.size > 0,
            finalRequests: finalRequests.size > 0 || terminalStreamErrors.size > 0,
            attempts: totalAttempts > 0,
            attemptLatency: attemptLatency.length > 0,
            cacheUsage: finalSummary.cache.requestsWithUsage > 0,
            routingDecisions: routingSummary.totalRecords > 0,
            comparableRequestAttempts: streamComplete("requests") && streamComplete("attempts"),
        },
        dataQuality: {
            linesRead,
            malformedLines,
            unsupportedLifecycleLines,
            lifecycleSequenceGaps,
            lifecycleSequenceDuplicates,
            conflictingLifecycleDuplicates,
            duplicateAttempts,
            finalOutcomeConflicts,
            acceptedWithoutFinal: [...accepted].filter((id) => !finalRequests.has(id) && !auxiliaryRequests.has(id)).length,
            terminalWithoutFinal: [...terminal].filter((id) => !finalRequests.has(id) && !auxiliaryRequests.has(id)).length,
            streams: Object.fromEntries(Object.entries(observedRanges).map(([stream, range]) => [
                stream,
                {
                    observedFrom: range.from === null ? null : new Date(range.from).toISOString(),
                    observedTo: range.to === null ? null : new Date(range.to).toISOString(),
                    startsAtOrBeforeRequestedWindow: range.from !== null && range.from <= sinceMs,
                    completeWindow: range.from !== null && range.from <= sinceMs,
                },
            ])),
            bodyArtifacts: {
                capturesIndexed,
                artifactsReferenced,
                artifactsPresent,
                artifactsMissing,
                invalidPaths,
                writeFailures,
                truncatedCaptures,
            },
            routingDecisions: {
                valid: validRoutingDecisions,
                invalid: invalidRoutingDecisions,
                absent: absentRoutingDecisions,
            },
        },
        runtime,
        lifecycle: {
            unconfirmedAtWorkerExit: [...accepted].flatMap((requestId) => {
                const workerProcessInstanceId = acceptedWorkers.get(requestId);
                const exit = workerProcessInstanceId
                    ? workerExits.get(workerProcessInstanceId)
                    : undefined;
                if (!exit ||
                    !workerProcessInstanceId ||
                    conflictedWorkerExits.has(workerProcessInstanceId) ||
                    terminal.has(requestId) ||
                    conflictedRequests.has(requestId)) {
                    return [];
                }
                return [
                    {
                        requestId,
                        workerProcessInstanceId,
                        at: String(exit.at),
                        workerExitCode: finiteNumber(exit.workerExitCode),
                        workerExitSignal: stringValue(exit.workerExitSignal),
                        // A provider final cannot prove the client received the entire body.
                        providerFinalRecorded: finalRequests.has(requestId),
                    },
                ];
            }),
            accepted: [...accepted].filter((id) => !internalRequests.has(id)).length,
            internalAccepted: [...accepted].filter((id) => internalRequests.has(id))
                .length,
            auxiliaryRequests: [...accepted].filter((id) => auxiliaryRequests.has(id) && !internalRequests.has(id)).length,
            headers: [...headers].filter((id) => !internalRequests.has(id)).length,
            firstChunks: [...firstChunks].filter((id) => !internalRequests.has(id))
                .length,
            terminal: [...terminal].filter((id) => !internalRequests.has(id)).length,
            internalTerminal: [...terminal].filter((id) => internalRequests.has(id))
                .length,
            internalUnsettled: [...accepted].filter((id) => internalRequests.has(id) && !terminal.has(id)).length,
            unsettled: [...accepted].filter((requestId) => !internalRequests.has(requestId) && !terminal.has(requestId)).length,
            terminalOutcomes,
            errorTypes: lifecycleErrorTypes,
            errorCodes: lifecycleErrorCodes,
        },
        requests: finalSummary.requestTotals,
        attempts: {
            total: totalAttempts,
            errors: totalAttemptErrors,
            errorTypes: attemptErrorTypes,
            errorCodes: attemptErrorCodes,
            transportScopes: attemptTransportScopes,
        },
        rateLimits: {
            attemptRateLimits,
            transient: transientRateLimits,
            quota: quotaRateLimits,
            unclassified: unclassifiedRateLimits,
        },
        latencyMs: {
            headers: summarizeLatency(verifiedLatencies(headersLatencyByRequest)),
            firstChunk: summarizeLatency(verifiedLatencies(firstChunkLatencyByRequest)),
            firstUsefulOutput: summarizeLatency([...finalRequests.values()].flatMap((record) => record.accountingScope === "internal" ||
                record.firstUsefulOutputMs === null
                ? []
                : [record.firstUsefulOutputMs])),
            terminal: summarizeLatency(verifiedLatencies(terminalLatencyByRequest)),
            finalRequest: summarizeLatency(finalSummary.finalRequestLatency),
            attempt: summarizeLatency(attemptLatency),
            singleAttemptDelta: summarizeLatency(finalSummary.singleAttemptDelta),
        },
        cache: finalSummary.cache,
        routing: routingSummary,
        accounts: [...accounts.values()].sort((left, right) => right.attempts - left.attempts),
    };
}