import { EventEmitter } from 'events';
import { type TraceProcessorQueryOptions } from './traceProcessorSqlWorker';
import { type TraceProcessorRamBudgetStats } from './traceProcessorRamBudget';
export declare function getBundledTraceProcessorPath(): string;
export declare function getPrebuiltTraceProcessorPlatformKey(): string | undefined;
export declare function getPrebuiltTraceProcessorPath(): string | undefined;
export declare function getBackendBinTraceProcessorPath(): string;
export declare function getUserTraceProcessorPath(): string;
export declare function isTraceProcessorPathPlaceholder(value: string): boolean;
export declare function getTraceProcessorPath(): string;
export declare function supportsTraceProcessorCorsOriginsFlag(binaryPath?: string): boolean;
export declare function isTraceProcessorReadyMessage(text: string): boolean;
export declare function isFatalTraceProcessorListenFailure(text: string): boolean;
/**
 * Kill all orphan trace_processor_shell processes.
 * This should be called at startup to clean up processes from previous runs.
 */
export declare function killOrphanProcessors(): number;
export interface QueryResult {
    columns: string[];
    rows: any[][];
    durationMs: number;
    error?: string;
}
export interface TraceProcessor {
    id: string;
    traceId: string;
    status: 'initializing' | 'ready' | 'busy' | 'error';
    activeQueries: number;
    getRuntimeStats(): TraceProcessorRuntimeStats;
    query(sql: string, options?: TraceProcessorQueryOptions): Promise<QueryResult>;
    queryRaw(body: Buffer, options?: TraceProcessorQueryOptions): Promise<Buffer>;
    queryHealth(timeoutMs?: number): Promise<TraceProcessorHealthProbeResult>;
    checkHealth(options?: TraceProcessorHealthCheckOptions): Promise<TraceProcessorHealthStatus>;
    destroy(): void;
}
export interface TraceProcessorHealthProbeResult {
    ok: boolean;
    durationMs: number;
    error?: string;
    detail?: string;
}
export interface TraceProcessorHealthCheckOptions {
    queryTimeoutMs?: number;
    rpcAcceptTimeoutMs?: number;
}
export interface TraceProcessorHealthStatus {
    ok: boolean;
    checkedAt: string;
    processorId: string;
    traceId: string;
    httpPort: number;
    liveness: TraceProcessorHealthProbeResult;
    rpcAccept: TraceProcessorHealthProbeResult;
    queryResponsive: TraceProcessorHealthProbeResult;
}
export interface TraceProcessorRuntimeStats {
    kind: 'owned_process' | 'external_rpc';
    processorId: string;
    processorKey?: string;
    traceId: string;
    leaseId?: string;
    leaseMode?: 'shared' | 'isolated' | string;
    status: 'initializing' | 'ready' | 'busy' | 'error';
    activeQueries: number;
    httpPort: number;
    pid?: number;
    rssBytes: number | null;
    startupRssBytes?: number | null;
    peakRssBytes?: number | null;
    lastRssSampleAt?: number | null;
    rssSampleSource: 'procfs' | 'ps' | 'unavailable' | 'external';
    rssSampleError?: string;
    sqlWorker?: {
        running: boolean;
        queuedP0: number;
        queuedP1: number;
        queuedP2: number;
        usesWorkerThread: boolean;
    };
}
export interface TraceProcessorCreateOptions {
    processorKey?: string;
    leaseId?: string;
    leaseMode?: 'shared' | 'isolated' | string;
}
/**
 * A working Trace Processor that uses trace_processor_shell in HTTP mode.
 *
 * This implementation:
 * 1. Starts trace_processor_shell with --httpd flag
 * 2. Loads the trace file once at initialization
 * 3. Executes queries via HTTP requests (fast, no reload)
 * 4. Properly cleans up the process on destroy
 */
export declare class WorkingTraceProcessor extends EventEmitter implements TraceProcessor {
    id: string;
    traceId: string;
    status: 'initializing' | 'ready' | 'busy' | 'error';
    private process;
    private tracePath;
    private readonly processorKey;
    private readonly leaseId?;
    private readonly leaseMode;
    private _httpPort;
    /** Get the HTTP port this processor is listening on */
    get httpPort(): number;
    private isDestroyed;
    private serverReady;
    private _activeQueries;
    private readonly sqlWorker;
    private startupRssBytes;
    private peakRssBytes;
    private lastRssBytes;
    private lastRssSampleAt;
    private lastRssSampleSource;
    private lastRssSampleError;
    private _criticalModulesLoaded;
    private _criticalModulesLoadPromise;
    private _criticalModulesLoadFailures;
    private static readonly MAX_STDLIB_LOAD_RETRIES;
    /** Number of in-flight queries. Factory uses this to avoid evicting busy processors. */
    get activeQueries(): number;
    getRuntimeStats(): TraceProcessorRuntimeStats;
    private sampleRss;
    constructor(traceId: string, tracePath: string, options?: TraceProcessorCreateOptions);
    initialize(): Promise<void>;
    /**
     * Start trace_processor_shell HTTP server
     */
    private startHttpServer;
    query(sql: string, options?: TraceProcessorQueryOptions): Promise<QueryResult>;
    queryRaw(body: Buffer, options?: TraceProcessorQueryOptions): Promise<Buffer>;
    queryHealth(timeoutMs?: number): Promise<TraceProcessorHealthProbeResult>;
    checkHealth(options?: TraceProcessorHealthCheckOptions): Promise<TraceProcessorHealthStatus>;
    private checkProcessLiveness;
    private enqueueHttpQuery;
    /**
     * Load critical stdlib modules one at a time in the background.
     * With only 3 Tier-0 modules this completes in a few seconds,
     * well within the configured query timeout per module.
     */
    private _loadCriticalModulesSequentially;
    /**
     * Execute SQL query via HTTP
     */
    private executeHttpQuery;
    private preloadModules;
    /**
     * Preload critical modules used by frequently hit analysis paths.
     */
    preloadCriticalPerfettoModules(): Promise<{
        loaded: string[];
        failed: string[];
    }>;
    /**
     * Preload all Perfetto stdlib modules to make additional views/tables available.
     */
    preloadAllPerfettoModules(): Promise<{
        loaded: string[];
        failed: string[];
    }>;
    destroy(): void;
}
/**
 * Factory for creating trace processors
 */
type ManagedTraceProcessor = WorkingTraceProcessor | ExternalRpcProcessor;
export declare class TraceProcessorFactory {
    private static processors;
    private static externalProcessorsByPort;
    private static maxProcessors;
    static create(traceId: string, tracePath: string, options?: TraceProcessorCreateOptions): Promise<WorkingTraceProcessor>;
    static get(processorKey: string): ManagedTraceProcessor | undefined;
    static remove(processorKey: string): boolean;
    static cleanup(): void;
    static getStats(): {
        count: number;
        traceIds: string[];
        processorKeys: string[];
        processors: TraceProcessorRuntimeStats[];
        ramBudget: TraceProcessorRamBudgetStats;
    };
    /**
     * Create a processor that connects to an existing external HTTP RPC endpoint.
     * This is used when the frontend is already connected to a trace_processor via HTTP RPC.
     * We don't start a new process, we just create a wrapper that queries the existing one.
     */
    static createFromExternalRpc(traceId: string, port: number): Promise<ExternalRpcProcessor>;
}
/**
 * A lightweight processor that connects to an external trace_processor HTTP RPC endpoint.
 * Unlike WorkingTraceProcessor, this doesn't start a new process.
 */
export declare class ExternalRpcProcessor extends EventEmitter implements TraceProcessor {
    id: string;
    traceId: string;
    status: 'initializing' | 'ready' | 'busy' | 'error';
    private _httpPort;
    private _activeQueries;
    private readonly sqlWorker;
    private _criticalModulesLoaded;
    private _criticalModulesLoadPromise;
    private _criticalModulesLoadFailures;
    private static readonly MAX_STDLIB_LOAD_RETRIES;
    get httpPort(): number;
    /** Number of in-flight or queued queries. Factory uses this to avoid unsafe eviction. */
    get activeQueries(): number;
    getRuntimeStats(): TraceProcessorRuntimeStats;
    constructor(traceId: string, port: number);
    query(sql: string, options?: TraceProcessorQueryOptions): Promise<QueryResult>;
    queryRaw(body: Buffer, options?: TraceProcessorQueryOptions): Promise<Buffer>;
    queryHealth(timeoutMs?: number): Promise<TraceProcessorHealthProbeResult>;
    checkHealth(options?: TraceProcessorHealthCheckOptions): Promise<TraceProcessorHealthStatus>;
    /**
     * Load critical stdlib modules one at a time in the background.
     * With only 3 Tier-0 modules this completes in a few seconds.
     */
    private _loadCriticalModulesSequentially;
    private enqueueRawQuery;
    /** Low-level query without stdlib lazy-load. Used by factory for connectivity checks. */
    _execRaw(sql: string, options?: TraceProcessorQueryOptions): Promise<QueryResult>;
    destroy(): void;
}
export {};
//# sourceMappingURL=workingTraceProcessor.d.ts.map