/*
 * 文件定位：Agent 执行引擎 / 执行核心逻辑。
 * 核心目的：承载 state Engine 这一能力位点。
 * 能力要求1：需要把文件名表达的能力落实成清晰的类型、输入输出和最小行为。
 * 能力要求2：如果后续发现语义不足，应优先补接口契约，而不是把逻辑散落到相邻文件。
 * 边界：只服务 agentCore 内核，不写上层产品逻辑。
 * 对接：需要被 runtime.execEngine 拉起，并和 mainLoop、stateEngine、事件暴露、工具调用策略接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
const allowedTransitions = ["start", "wait", "reply", "complete", "interrupt", "fail", "reset"];
const transitionTable = {
    idle: { start: "running", fail: "failed" },
    running: { wait: "waiting", reply: "replying", complete: "completed", interrupt: "interrupted", fail: "failed" },
    waiting: { start: "running", interrupt: "interrupted", fail: "failed" },
    replying: { complete: "completed", fail: "failed" },
    completed: { reset: "idle" },
    interrupted: { reset: "idle" },
    failed: { reset: "idle" },
};
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function isAgentExecutionStateTransition(value) {
    return allowedTransitions.includes(value);
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary, stateSafe: true },
        events: ["agentCore.execution.state.rejected"],
    };
}
function cleanTrace(trace) {
    return {
        correlationId: trace?.correlationId?.trim() || undefined,
        callerId: trace?.callerId?.trim() || undefined,
    };
}
export function advanceAgentExecutionState(request) {
    if (request === undefined || isBlank(request.sessionId)) {
        return failure("MISSING_SESSION_ID", "stateEngine requires a sessionId before changing execution state", "input");
    }
    const sessionId = (request.sessionId ?? "").trim();
    const currentSessionId = request.current?.sessionId.trim();
    if (currentSessionId !== undefined && currentSessionId !== sessionId) {
        return failure("SESSION_MISMATCH", "stateEngine current snapshot belongs to a different session", "input");
    }
    if (isBlank(request.transition)) {
        return failure("MISSING_TRANSITION", "stateEngine requires an explicit transition intent", "input");
    }
    const transition = (request.transition ?? "").trim();
    if (!isAgentExecutionStateTransition(transition)) {
        return failure("INVALID_TRANSITION", `execution transition ${transition} is not supported by stateEngine`, "input");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "state transition was rejected by contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "state transition was rejected by governance", "governance");
    }
    const previousPhase = request.current?.phase ?? "idle";
    const nextPhase = transitionTable[previousPhase][transition];
    if (nextPhase === undefined) {
        return failure("INVALID_TRANSITION", `stateEngine cannot apply ${transition} while execution phase is ${previousPhase}`, "state-machine");
    }
    return {
        ok: true,
        state: {
            sessionId,
            phase: nextPhase,
            revision: (request.current?.revision ?? 0) + 1,
            reason: request.reason?.trim() || undefined,
            trace: cleanTrace(request.trace ?? request.current?.trace),
            mutable: false,
            unsafeSideEffects: false,
        },
        previousPhase,
        transition,
        events: [`agentCore.execution.state.${previousPhase}.${transition}.${nextPhase}`],
    };
}
