"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentStateMachine = void 0;
/**
 * SmartPerfetto Agent State Machine
 *
 * @deprecated Not used by the Claude/OpenAI SDK runtime architecture.
 *
 * 状态机核心组件，负责：
 * 1. 管理 Agent 生命周期状态转换
 * 2. 检查点持久化和恢复
 * 3. 迭代计数器管理
 * 4. 事件记录和回放
 */
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const events_1 = require("events");
const types_1 = require("../types");
// 默认持久化路径
const DEFAULT_PERSIST_PATH = path.join(process.cwd(), 'agent-state');
// 状态转换规则定义
const STATE_TRANSITIONS = {
    [types_1.AgentPhase.IDLE]: [types_1.AgentPhase.PLANNING, types_1.AgentPhase.FAILED],
    [types_1.AgentPhase.PLANNING]: [types_1.AgentPhase.EXECUTING, types_1.AgentPhase.AWAITING_USER, types_1.AgentPhase.FAILED],
    [types_1.AgentPhase.EXECUTING]: [types_1.AgentPhase.EVALUATING, types_1.AgentPhase.AWAITING_USER, types_1.AgentPhase.FAILED],
    [types_1.AgentPhase.EVALUATING]: [types_1.AgentPhase.REFINING, types_1.AgentPhase.COMPLETED, types_1.AgentPhase.AWAITING_USER, types_1.AgentPhase.FAILED],
    [types_1.AgentPhase.REFINING]: [types_1.AgentPhase.EXECUTING, types_1.AgentPhase.EVALUATING, types_1.AgentPhase.COMPLETED, types_1.AgentPhase.AWAITING_USER, types_1.AgentPhase.FAILED], // 添加 completed，允许从 refining 直接完成
    [types_1.AgentPhase.AWAITING_USER]: [types_1.AgentPhase.PLANNING, types_1.AgentPhase.EXECUTING, types_1.AgentPhase.EVALUATING, types_1.AgentPhase.REFINING, types_1.AgentPhase.COMPLETED, types_1.AgentPhase.FAILED],
    [types_1.AgentPhase.COMPLETED]: [], // 终态
    [types_1.AgentPhase.FAILED]: [types_1.AgentPhase.IDLE], // 可以重新开始
};
/**
 * Agent 状态机实现
 *
 * 核心职责：
 * - 状态转换管理和验证
 * - 检查点创建和恢复
 * - 持久化存储
 * - 事件钩子
 *
 * @deprecated This class is no longer used in the Agent-Driven architecture (v5.0).
 * The new architecture uses AgentRuntime with strategy/hypothesis executors
 * which manage their own lifecycle internally. Retained for potential future integration
 * with session persistence. Will be removed in v6.0.
 */
class AgentStateMachine extends events_1.EventEmitter {
    constructor(config) {
        super();
        this.config = {
            ...config,
            persistPath: config.persistPath || DEFAULT_PERSIST_PATH,
            autoSave: config.autoSave ?? true,
            autoSaveIntervalMs: config.autoSaveIntervalMs || 5000,
        };
        // 初始化状态
        this.state = {
            sessionId: config.sessionId,
            traceId: config.traceId,
            phase: types_1.AgentPhase.IDLE,
            checkpoints: new Map(),
            iterationCounters: new Map(),
            currentStageIndex: 0,
            stageResults: new Map(),
            events: [],
            createdAt: Date.now(),
            updatedAt: Date.now(),
        };
        // 确保持久化目录存在
        this.ensurePersistPath();
        // 启动自动保存
        if (this.config.autoSave) {
            this.startAutoSave();
        }
    }
    // ==========================================================================
    // 状态查询
    // ==========================================================================
    get sessionId() {
        return this.state.sessionId;
    }
    get traceId() {
        return this.state.traceId;
    }
    get phase() {
        return this.state.phase;
    }
    get currentStageIndex() {
        return this.state.currentStageIndex;
    }
    get isCompleted() {
        return this.state.phase === types_1.AgentPhase.COMPLETED;
    }
    get isFailed() {
        return this.state.phase === types_1.AgentPhase.FAILED;
    }
    get isAwaitingUser() {
        return this.state.phase === types_1.AgentPhase.AWAITING_USER;
    }
    get canResume() {
        return (this.state.phase !== types_1.AgentPhase.COMPLETED &&
            this.state.phase !== types_1.AgentPhase.IDLE &&
            this.state.checkpoints.size > 0);
    }
    // ==========================================================================
    // 状态转换
    // ==========================================================================
    /**
     * 触发状态转换
     */
    transition(event) {
        const newPhase = this.getNextPhase(event);
        if (!this.isValidTransition(newPhase)) {
            throw new Error(`Invalid state transition: ${this.state.phase} -> ${newPhase} (event: ${event.type})`);
        }
        const oldPhase = this.state.phase;
        // 记录事件
        this.state.events.push({
            ...event,
            timestamp: event.timestamp || Date.now(),
        });
        // 更新状态
        this.state.phase = newPhase;
        this.state.updatedAt = Date.now();
        // 触发钩子
        this.emit('phaseChange', { from: oldPhase, to: newPhase, event });
        this.emit(`phase:${newPhase}`, event);
        // 自动持久化
        if (this.config.autoSave) {
            this.persist().catch((err) => {
                console.error('[StateMachine] Auto-save failed:', err);
            });
        }
    }
    /**
     * 根据事件类型确定下一个状态
     */
    getNextPhase(event) {
        switch (event.type) {
            case types_1.StateEventType.START_ANALYSIS:
                return types_1.AgentPhase.PLANNING;
            case types_1.StateEventType.INTENT_UNDERSTOOD:
            case types_1.StateEventType.PLAN_CREATED:
                return types_1.AgentPhase.EXECUTING;
            case types_1.StateEventType.STAGE_COMPLETED:
                return types_1.AgentPhase.EVALUATING;
            case types_1.StateEventType.EVALUATION_COMPLETE:
                return event.payload?.passed ? types_1.AgentPhase.COMPLETED : types_1.AgentPhase.REFINING;
            case types_1.StateEventType.NEEDS_REFINEMENT:
                // 从 refining 转回 executing 进行下一轮迭代
                return this.state.phase === types_1.AgentPhase.REFINING ? types_1.AgentPhase.EXECUTING : types_1.AgentPhase.REFINING;
            case types_1.StateEventType.CIRCUIT_TRIPPED:
                return types_1.AgentPhase.AWAITING_USER;
            case types_1.StateEventType.USER_RESPONDED:
                return event.payload?.nextPhase || types_1.AgentPhase.EXECUTING;
            case types_1.StateEventType.ANALYSIS_COMPLETE:
                return types_1.AgentPhase.COMPLETED;
            case types_1.StateEventType.ERROR_OCCURRED:
                return types_1.AgentPhase.FAILED;
            default:
                return this.state.phase;
        }
    }
    /**
     * 验证状态转换是否合法
     */
    isValidTransition(newPhase) {
        if (newPhase === this.state.phase) {
            return true; // 保持当前状态
        }
        return STATE_TRANSITIONS[this.state.phase].includes(newPhase);
    }
    // ==========================================================================
    // 检查点管理
    // ==========================================================================
    /**
     * 创建检查点
     */
    checkpoint(stageId, stageResult, findings = []) {
        const checkpointId = `${this.state.sessionId}_${stageId}_${Date.now()}`;
        const checkpoint = {
            id: checkpointId,
            stageId,
            timestamp: Date.now(),
            phase: this.state.phase,
            agentState: this.serializeState(),
            stageResults: Array.from(this.state.stageResults.values()),
            findings,
            canResume: true,
        };
        // 保存阶段结果
        this.state.stageResults.set(stageId, stageResult);
        // 保存检查点
        this.state.checkpoints.set(checkpointId, checkpoint);
        this.state.updatedAt = Date.now();
        // 触发事件
        this.emit('checkpoint', checkpoint);
        // 持久化
        this.persist().catch((err) => {
            console.error('[StateMachine] Checkpoint save failed:', err);
        });
        return checkpoint;
    }
    /**
     * 获取最新的检查点
     */
    getLatestCheckpoint() {
        let latest;
        let latestTime = 0;
        for (const checkpoint of this.state.checkpoints.values()) {
            if (checkpoint.timestamp > latestTime) {
                latestTime = checkpoint.timestamp;
                latest = checkpoint;
            }
        }
        return latest;
    }
    /**
     * 获取指定检查点
     */
    getCheckpoint(checkpointId) {
        return this.state.checkpoints.get(checkpointId);
    }
    /**
     * 从检查点恢复
     */
    restoreFromCheckpoint(checkpoint) {
        this.state.phase = checkpoint.phase;
        this.state.currentStageIndex = this.getStageIndexFromId(checkpoint.stageId);
        // 恢复阶段结果
        this.state.stageResults.clear();
        for (const result of checkpoint.stageResults) {
            this.state.stageResults.set(result.stageId, result);
        }
        this.state.updatedAt = Date.now();
        this.emit('restored', { checkpoint });
    }
    getStageIndexFromId(stageId) {
        // 从阶段结果中推断索引
        const stages = Array.from(this.state.stageResults.keys());
        const index = stages.indexOf(stageId);
        return index >= 0 ? index : 0;
    }
    // ==========================================================================
    // 迭代计数器
    // ==========================================================================
    /**
     * 增加迭代计数
     */
    incrementIteration(key) {
        const current = this.state.iterationCounters.get(key) || 0;
        const newCount = current + 1;
        this.state.iterationCounters.set(key, newCount);
        this.state.updatedAt = Date.now();
        return newCount;
    }
    /**
     * 获取迭代计数
     */
    getIterationCount(key) {
        return this.state.iterationCounters.get(key) || 0;
    }
    /**
     * 重置迭代计数
     */
    resetIterationCount(key) {
        this.state.iterationCounters.set(key, 0);
        this.state.updatedAt = Date.now();
    }
    // ==========================================================================
    // 序列化和持久化
    // ==========================================================================
    /**
     * 序列化当前状态
     */
    serializeState() {
        return {
            query: '', // 需要外部提供
            traceId: this.state.traceId,
            intent: undefined, // 需要外部提供
            plan: undefined, // 需要外部提供
            expertResults: [],
            iterationCount: this.getTotalIterations(),
            metadata: {
                currentStageIndex: this.state.currentStageIndex,
                phase: this.state.phase,
            },
        };
    }
    /**
     * 获取总迭代次数
     */
    getTotalIterations() {
        let total = 0;
        for (const count of this.state.iterationCounters.values()) {
            total += count;
        }
        return total;
    }
    /**
     * 持久化状态到磁盘
     */
    async persist() {
        const filePath = this.getStatePath();
        // 转换 Map 为可序列化对象
        const serializable = {
            ...this.state,
            checkpoints: Object.fromEntries(this.state.checkpoints),
            iterationCounters: Object.fromEntries(this.state.iterationCounters),
            stageResults: Object.fromEntries(this.state.stageResults),
        };
        await fs.promises.writeFile(filePath, JSON.stringify(serializable, null, 2), 'utf-8');
    }
    /**
     * 从磁盘恢复状态
     */
    async restore() {
        const filePath = this.getStatePath();
        try {
            const content = await fs.promises.readFile(filePath, 'utf-8');
            const data = JSON.parse(content);
            // 恢复 Map
            this.state = {
                ...data,
                checkpoints: new Map(Object.entries(data.checkpoints || {})),
                iterationCounters: new Map(Object.entries(data.iterationCounters || {})),
                stageResults: new Map(Object.entries(data.stageResults || {})),
            };
            this.emit('restored', { fromFile: filePath });
            return true;
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                return false; // 文件不存在，正常情况
            }
            throw error;
        }
    }
    /**
     * 删除持久化状态
     */
    async cleanup() {
        const filePath = this.getStatePath();
        try {
            await fs.promises.unlink(filePath);
        }
        catch (error) {
            if (error.code !== 'ENOENT') {
                throw error;
            }
        }
    }
    /**
     * 获取状态文件路径
     */
    getStatePath() {
        return path.join(this.config.persistPath, `${this.state.sessionId}.json`);
    }
    /**
     * 确保持久化目录存在
     */
    ensurePersistPath() {
        if (!fs.existsSync(this.config.persistPath)) {
            fs.mkdirSync(this.config.persistPath, { recursive: true });
        }
    }
    // ==========================================================================
    // 自动保存
    // ==========================================================================
    startAutoSave() {
        this.autoSaveTimer = setInterval(() => {
            this.persist().catch((err) => {
                console.error('[StateMachine] Auto-save failed:', err);
            });
        }, this.config.autoSaveIntervalMs);
    }
    stopAutoSave() {
        if (this.autoSaveTimer) {
            clearInterval(this.autoSaveTimer);
            this.autoSaveTimer = undefined;
        }
    }
    // ==========================================================================
    // 事件历史
    // ==========================================================================
    /**
     * 获取事件历史
     */
    getEventHistory() {
        return [...this.state.events];
    }
    /**
     * 获取阶段结果
     */
    getStageResults() {
        return Array.from(this.state.stageResults.values());
    }
    /**
     * 获取指定阶段结果
     */
    getStageResult(stageId) {
        return this.state.stageResults.get(stageId);
    }
    // ==========================================================================
    // 生命周期
    // ==========================================================================
    /**
     * 重置状态机
     */
    reset() {
        this.stopAutoSave();
        this.state = {
            sessionId: this.config.sessionId,
            traceId: this.config.traceId,
            phase: types_1.AgentPhase.IDLE,
            checkpoints: new Map(),
            iterationCounters: new Map(),
            currentStageIndex: 0,
            stageResults: new Map(),
            events: [],
            createdAt: Date.now(),
            updatedAt: Date.now(),
        };
        if (this.config.autoSave) {
            this.startAutoSave();
        }
        this.emit('reset');
    }
    /**
     * 销毁状态机
     */
    destroy() {
        this.stopAutoSave();
        this.removeAllListeners();
    }
    // ==========================================================================
    // 静态工厂方法
    // ==========================================================================
    /**
     * 创建新的状态机
     */
    static create(sessionId, traceId) {
        return new AgentStateMachine({
            sessionId,
            traceId,
        });
    }
    /**
     * 从持久化恢复状态机
     */
    static async load(sessionId, traceId, persistPath) {
        const machine = new AgentStateMachine({
            sessionId,
            traceId,
            persistPath,
        });
        const restored = await machine.restore();
        return restored ? machine : null;
    }
    /**
     * 列出所有可恢复的会话
     */
    static async listRecoverableSessions(persistPath) {
        const dir = persistPath || DEFAULT_PERSIST_PATH;
        try {
            const files = await fs.promises.readdir(dir);
            return files
                .filter((f) => f.endsWith('.json'))
                .map((f) => f.replace('.json', ''));
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                return [];
            }
            throw error;
        }
    }
}
exports.AgentStateMachine = AgentStateMachine;
exports.default = AgentStateMachine;
//# sourceMappingURL=stateMachine.js.map