/**
 * Cache Trace Debugging (OpenClaw-inspired)
 *
 * Logs every stage of prompt construction for debugging
 * cache hit/miss issues and context building.
 *
 * Enable via CACHE_TRACE=true environment variable.
 */
import { logger } from '../utils/logger.js';
/**
 * Cache trace debugger.
 * Records timing and token counts at each stage of prompt construction.
 */
export class CacheTrace {
    enabled;
    entries = [];
    stageStartTimes = new Map();
    sessionId;
    constructor(sessionId = 'default') {
        this.enabled = process.env.CACHE_TRACE === 'true' || process.env.CACHE_TRACE === '1';
        this.sessionId = sessionId;
    }
    /**
     * Record a trace entry for a completed stage.
     */
    trace(stage, metadata) {
        if (!this.enabled)
            return;
        const entry = {
            stage,
            timestamp: Date.now(),
            tokenCount: metadata?.tokenCount,
            messageCount: metadata?.messageCount,
            metadata,
        };
        const startTime = this.stageStartTimes.get(stage);
        if (startTime) {
            entry.durationMs = Date.now() - startTime;
            this.stageStartTimes.delete(stage);
        }
        this.entries.push(entry);
        logger.debug(`[CacheTrace:${this.sessionId}] ${stage}`, {
            ...metadata,
            durationMs: entry.durationMs,
        });
    }
    /**
     * Mark the start of a stage (for duration tracking).
     */
    start(stage) {
        if (!this.enabled)
            return;
        this.stageStartTimes.set(stage, Date.now());
    }
    /**
     * End a stage and record the trace with duration.
     */
    end(stage, metadata) {
        this.trace(stage, metadata);
    }
    /**
     * Get all trace entries.
     */
    getEntries() {
        return [...this.entries];
    }
    /**
     * Get a summary of the trace.
     */
    getSummary() {
        if (!this.enabled || this.entries.length === 0) {
            return 'Cache trace: disabled or no entries';
        }
        const lines = [];
        lines.push(`Cache Trace [${this.sessionId}] — ${this.entries.length} stages`);
        lines.push('');
        const firstTs = this.entries[0].timestamp;
        for (const entry of this.entries) {
            const relativeMs = entry.timestamp - firstTs;
            const duration = entry.durationMs ? ` (${entry.durationMs}ms)` : '';
            const tokens = entry.tokenCount ? ` [${entry.tokenCount} tokens]` : '';
            const msgs = entry.messageCount ? ` [${entry.messageCount} msgs]` : '';
            lines.push(`  +${relativeMs}ms  ${entry.stage}${duration}${tokens}${msgs}`);
        }
        const totalMs = this.entries[this.entries.length - 1].timestamp - firstTs;
        lines.push('');
        lines.push(`  Total: ${totalMs}ms`);
        return lines.join('\n');
    }
    clear() {
        this.entries = [];
        this.stageStartTimes.clear();
    }
    isEnabled() {
        return this.enabled;
    }
}
//# sourceMappingURL=cache-trace.js.map