"use strict";
/**
 * Compaction event tracking plugin.
 *
 * Tracks context compaction events (compact_boundary system messages)
 * including pre-compaction token counts per phase.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompactionTrackerPlugin = void 0;
const debug_1 = require("../../../utils/debug");
const agent_interface_1 = require("../../agent-interface");
class CompactionTrackerPlugin {
    name = 'compactions';
    phaseCompactions = 0;
    phasePreTokens = [];
    totalCompactions = 0;
    phaseSnapshots = [];
    currentPhase = 'setup';
    onMessage(message, ctx, store) {
        if (message.type !== 'system' || message.subtype !== 'compact_boundary') {
            return;
        }
        const preTokens = message.compact_metadata?.pre_tokens ?? 0;
        const trigger = message.compact_metadata?.trigger ?? 'unknown';
        this.phaseCompactions++;
        this.totalCompactions++;
        this.phasePreTokens.push(preTokens);
        (0, debug_1.logToFile)(`${agent_interface_1.AgentSignals.BENCHMARK} [COMPACTION] Context compacted during "${ctx.currentPhase}" (trigger: ${trigger}, pre_tokens: ${preTokens})`);
        store.set('compactions', this.getData());
    }
    onPhaseTransition(fromPhase, toPhase, _ctx, store) {
        this.phaseSnapshots.push({
            phase: fromPhase,
            compactions: this.phaseCompactions,
            preTokens: [...this.phasePreTokens],
        });
        this.currentPhase = toPhase;
        this.phaseCompactions = 0;
        this.phasePreTokens = [];
        store.set('compactions', this.getData());
    }
    onFinalize(_resultMessage, _totalDurationMs, _ctx, store) {
        this.phaseSnapshots.push({
            phase: this.currentPhase,
            compactions: this.phaseCompactions,
            preTokens: [...this.phasePreTokens],
        });
        store.set('compactions', this.getData());
    }
    getData() {
        return {
            phaseCompactions: this.phaseCompactions,
            phasePreTokens: [...this.phasePreTokens],
            totalCompactions: this.totalCompactions,
            phaseSnapshots: [...this.phaseSnapshots],
        };
    }
}
exports.CompactionTrackerPlugin = CompactionTrackerPlugin;
